Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx configuration leads to endless redirect loop

So I've looked at every sample configuration I could find and yet every time I try and view a page that requires ssl, I end up in an redirect loop. I'm running nginx/0.8.53 and passenger 3.0.2.

Here's the ssl config

server  {   listen 443 default ssl;   server_name <redacted>.com www.<redacted>.com;   root /home/app/<redacted>/public;   passenger_enabled on;   rails_env production;     ssl_certificate      /home/app/ssl/<redacted>.com.pem;   ssl_certificate_key  /home/app/ssl/<redacted>.key;    proxy_set_header  X-Real-IP  $remote_addr;   proxy_set_header  X_FORWARDED_PROTO https;   proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;   proxy_set_header  Host $http_host;   proxy_set_header  X-Url-Scheme $scheme;   proxy_redirect    off;   proxy_max_temp_file_size 0;    location /blog {     rewrite ^/blog(/.*)?$ http://blog.<redacted>.com/$1 permanent;   }    location ~* \.(js|css|jpg|jpeg|gif|png)$ {     if (-f $request_filename) {       expires      max;       break;     }   }    error_page   500 502 503 504  /50x.html;   location = /50x.html {     root   html;   } } 

Here's the non-ssl config

server  {   listen 80;   server_name <redacted>.com www.<redacted>.com;   root /home/app/<redacted>/public;   passenger_enabled on;   rails_env production;      location /blog {     rewrite ^/blog(/.*)?$ http://blog.<redacted>.com/$1 permanent;   }    location ~* \.(js|css|jpg|jpeg|gif|png)$ {     if (-f $request_filename) {       expires      max;       break;     }   }    error_page   500 502 503 504  /50x.html;   location = /50x.html {     root   html;   } } 

Let me know if there's any additional info I can give to help diagnose the issue.

like image 957
brianthecoder Avatar asked Jan 06 '11 15:01

brianthecoder


People also ask

What is nginx permanent redirect?

Permanent redirects such as NGINX 301 Redirect simply makes the browser forget the old address entirely and prevents it from attempting to access that address anymore. These redirects are very useful if your content has been permanently moved to a new location, like when you change domain names or servers.

What is infinite redirect loop?

What is an Infinite / Looped Redirect? An infinite / looped redirect is a group of 2 or more redirects strung together in a chain that never resolve to a URL that breaks the redirect chain with a non-redirecting HTTP status.

How do you redirect all HTTP traffic to https within nginx?

Redirect HTTP to HTTPS version for Specified domain in Nginx Server_name domain-name.com www.domain-name.com – it specifies the domain names. So, replace it with your website domain name that you want to redirect. Return 301 https://domain-name.com$request_uri – it moves the traffic to the HTTPS version of the site.

What is return 301 in nginx?

301 Permanent Redirection Permanent redirection will inform the browser that the website is no longer available with the old URL and this information should be updated and this point to the new URL we published.


1 Answers

It's your line here:

  listen 443 default ssl; 

change it to:

listen 443; ssl on; 

This I'll call the old style. Also, that along with

              proxy_set_header X_FORWARDED_PROTO https;               proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;               proxy_set_header  Host $http_host;               proxy_set_header  X-Url-Scheme $scheme;               proxy_redirect    off;               proxy_max_temp_file_size 0; 

did the trick for me. I see now i am missing the real IP line you have, but so far, this got rid of my infinite loop problem with ssl_requirement and ssl_enforcer.

like image 133
pjammer Avatar answered Oct 22 '22 14:10

pjammer