Nginx – docker nginx proxy: error ERR_TOO_MANY_REDIRECTS

dockermagentonginxredirect

I am using the jwilder/nginx-proxy docker container to proxy a magento container (using nginx) with SSL. I was able to set up the containers and run the magento installation routine with an SSL connection.
After completing the installation I get a ERR_TOO_MANY_REDIRECTS error message whenever I try to acccess the magento frontend or backend.

This seems to be weird since there is obviously no problem during installation, so I assume there must be something I am not getting yet. I assume that there are some reqrite rules which mess up the architecture, but I cannot figure out what's wrong.

This is my configuration of the magento nginx:

#
# The default server
#
server {

listen   443;

ssl on;

server_name example.com;

ssl_certificate        /etc/certs/example.com.crt;
ssl_certificate_key    /etc/certs/example.com.key;

#charset koi8-r;


root   /var/www;
index  index.html index.htm index.php;


location / {

    index index.html index.php; ## Allow a static html file to be shown first

    try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler

    expires 30d; ## Assume all files are cachable

}



## These locations would be hidden by .htaccess normally

location ^~ /app/                { deny all; }

location ^~ /includes/           { deny all; }

location ^~ /lib/                { deny all; }

location ^~ /media/downloadable/ { deny all; }

location ^~ /pkginfo/            { deny all; }

location ^~ /report/config.xml   { deny all; }

location ^~ /var/                { deny all; }



location /var/export/ { ## Allow admins only to view export folder

    auth_basic           "Restricted"; ## Message shown in login window

    auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword

    autoindex            on;

}



location  /. { ## Disable .htaccess and other hidden files

    return 404;

}



location @handler { ## Magento uses a common front handler

    rewrite / /index.php;

}



location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler

    rewrite ^(.*.php)/ $1 last;

    fastcgi_read_timeout 500;

}


# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

}

I added the following evironment variables to the magento docker container:

  • VIRTUAL_HOST example.com
  • VIRTUAL_PORT 443
  • VIRTUAL_PROTO https

Furthermore I added the ssl certificates to the nginx proxy as well as to the magento nginx configuration. So each container (nginx proxy and magento) contain this certificates.

The idea is that the nginx proxy forwards all requests from example.com to the magento container via an SSL connection.

The magento container does not expose any ports to the public but the nginx proxy links to the container (for security reasons).

I have been desperately working on this for several days now and greatly appreciate any hint. Could there be any issue with the certificates? E.g. if the forwarded request from the nginx proxy coontains a different server name and there cannot verify with the certificates on magento?! But if so, how could that be resolved?

Thanks
Peter

Best Answer

Execute this in your Magento 2.x installation directory and your site will be back to normal (with secure connection).

sudo php bin/magento setup:store-config:set --base-url-secure="https://example.com"