1

I recently setup letsencrypt with my site, and the SSL works perfectly. However, when I set Nginx to redirect HTTPS to HTTPS, I get a redirect loop where when you visit the website and refresh the URL changes back and forth from example.com to https://example.com.

This is my configuration:

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;
    root /var/www;
    index index.html;        

    # ssl_certificate, etc...

    location / {
        try_files $uri $uri/ /index.html =404;
    }
}

What am I doing incorrectly here that causes the redirect loop?

2
  • 1
    What is the output of curl -LI http://example.com/some/path? (Substitute suitable values for example.com and the path)
    – muru
    Feb 6, 2016 at 4:28
  • I "fixed" it, turns out cloudflare was redirecting to non-https whenever you attempt to use https.
    – mosmo
    Feb 7, 2016 at 23:40

1 Answer 1

0

Try to add ssl on; under the line listen 443 ssl; and disable disable SSLv3

server {
listen      80;
server_name example.com www.example.com;
listen 443 ssl;
ssl on;
server_name example.com;
ssl_certificate     www.example.com.crt;
ssl_certificate_key www.example.com.key;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers         HIGH:!aNULL:!MD5;

location / {
  rewrite     ^(.*)   https://example.com$1 permanent;
}
...

Or you can also use return its way faster than rewrite in niginx location blocks:

location / {
  return ^(.*)   https://example.com$1 permanent;
}

Once SSL Done You can test your ssl certificate at https://www.ssllabs.com

To create chain certificate:

 cat www.example.com.crt bundle.crt > www.example.com.chained.crt

 ssl_certificate     www.example.com_chain.crt;
 ssl_certificate_key www.example.com.key;
2
  • 1
    Use return like OP does instead of the rewrite. See nginx pitfalls.
    – muru
    Feb 6, 2016 at 4:56
  • Yes correct it batter then rewrite ! Feb 6, 2016 at 5:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .