Redirect loop with Nginx SSL termination via Varnish/Apache On Wordpress

3.5k views Asked by At

I have a setup where Apache listens on port 8080 behind Varnish 4 on port 80, but my client needs ssl working on the site so I setup Nginx for SSL termination on port 443 using this guide.

Everything works fine at first on http, but on attempting to load the site on https, scripts required by the site won't load, So I decided to change the site url in Settings > General to an https url, on saving changes, I get a redirect loop error in Chrome.

I couldn't access the site's wordpress dashboard to change the url back so I had to do it via phpmyadmin. But now when the site is accessed via https, the site breaks cause the scripts it requires to render content aren't authenticated.

Someone else has the same issue here but it doesn't look like it was solved.

How can I have the site on just https without a redirect loop in Chrome?

2

There are 2 answers

2
Feyisayo Sonubi On

After hours of trying to get this to work, I finally found a fix, all I had to was add an HTTP_X_FORWARDED_PROTO in my wp-config.php just before changing the site url, this way:

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
     $_SERVER['HTTPS']='on';

One more thing I had to do was to add proxy_set_header X-Forwarded-Protocol $scheme; to my default file in /etc/nginx/sites-enabled/default so it looks like this:

server {                                                      
        ...                                                    
        location / {                                          
                 ...
            proxy_set_header X-Forwarded-Protocol $scheme;
          }
  }

Hope this helps someone out there.

0
Georgi Vasilev On

You can set this header in nginx:

proxy_set_header X-Forwarded-Proto $scheme;

and then in your apache vhost configuration:

SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on