I've followed the instructions from here on setting up nginx to redirect all non-www requests into www but I keep getting ERR_TOO_MANY_REDIRECTS in my browser when I try to hit any page.
My goal is twofold:
- All requests that don't have www should be redirected to www
- All requests that aren't HTTPS should be redirected to HTTPS
My nginx config looks like this:
upstream mywebsite_proxy { server unix:/home/deploy/mywebsite/tmp/sockets/puma.sock; } server { listen 80; listen [::]:80; listen 443 default_server ssl; server_name www.mywebsite.com; if ($scheme = http) { return 301 https://$server_name$request_uri; } location / { proxy_pass http://mywebsite_proxy; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ ^/(robots.txt|sitemap.xml.gz)/ { root /home/deploy/mywebsite/public; } } Notice that there isn't any reference to SSL certificates. I'm using Cloudflare with SSL enabled and HTTPS seemed to just work right out the gate when my config looked like the one below. The non-www to www and non http to https redirects obviously didn't work though...
upstream mywebsite_proxy { server unix:/home/deploy/mywebsite/tmp/sockets/puma.sock; } server { listen 80; listen 443; server_name www.mywebsite.com mywebsite.com; root /home/deploy/mywebsite/public; location / { proxy_pass http://mywebsite_proxy; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ ^/(robots.txt|sitemap.xml.gz)/ { root /home/deploy/mywebsite/public; } }