Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have recently migrated my rails application from using HTTP request to HTTPS.

My application URL is as follows: https://testmate.persistent.co.in

I have mode all the required configurations in the nginx.conf file

My nginx.conf file looks as follows:

# start the http module where we config http access.
http {
       ...

       server {            
         listen 443;

         ssl on;
         ssl_certificate certificate.pem;
         ssl_certificate_key server.key;

         ssl_protocols SSLv3;

         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_redirect off;
         proxy_max_temp_file_size 0;
         proxy_set_header X-Forwarded-Ssl on;

         # doc root
         root /var/www/TestMate/current/public/;

         passenger_enabled on;
         passenger_use_global_queue on;

         rails_env production;

         # vhost specific access log
         access_log  logs/production.access.log  main;
         client_max_body_size  10M;

         if (-f $document_root/maintenance.html){
             rewrite  ^(.*)$  /maintenance.html last;
             break;
         }

         location ~* ^.+\.(jpg|jpeg|flv|gif|css|png|js|ico|html|swf|favicon\.ico|robots\.txt)$ {
             access_log   off;
             expires      365d;
         }

         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
           root   html;
         }

       }        

     server {
         # port to listen on. Can also be set to an IP:PORT
         listen       80;

         # sets the domain[s] that this vhost server requests for

         # doc root
         root /var/www/TestMate/current/public/;

         passenger_enabled on;
         passenger_use_global_queue on;

         rails_env production;

         # vhost specific access log
         access_log  logs/production.access.log  main;
         client_max_body_size  10M;

         if (-f $document_root/maintenance.html){
              rewrite  ^(.*)$  /maintenance.html last;
              break;
         }

         location ~* ^.+\.(jpg|jpeg|flv|gif|css|png|js|ico|html|swf|favicon\.ico|robots\.txt)$ {
            access_log   off;
            expires      365d;
         }

      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
      root   html;
    }
}


}

If I access the above URL within intranet everything works fine.

But whenever I try to access it from outside network, it causes a infinite loop of redirect request.

It works fine if I completely remove the server block for port 80. But there are some parts of my application which do not require HTTPS check.

Following in my nginx production.access.log file output which goes in a loop:

15/Feb/2012:18:53:02 +05308.301 10.78.0.21 - - 302 "GET / HTTP/1.0" "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1" "http_x_forwarded_for" 100 "-"

Following is my application production.log file output which also goes in a loop:

Started GET "/" for 66.249.6.106 at 2012-02-15 18:25:28 +0530
  Processing by as */*
  Redirected to https://testmate.persistent.co.in/
  Completed 302 Found in 1ms

Any idea why this is happening?

share|improve this question
    
I don't see any significant rewrites in the nginx config you've pasted. Could it be elsewhere? Could there be something in the app that's issuing the redirects, not nginx? –  cjc Feb 15 '12 at 14:12
    
Yeah, the application is redirecting, not the web server. –  Shane Madden Feb 15 '12 at 16:39
    
The application is redirecting because the request received from nginx is coming as HTTP and not HTTPS. We have added the nginx configurations for setting X-Forwarded-Proto header to HTTPS but it is not added to the request object. –  tejas Feb 16 '12 at 6:08

1 Answer 1

Add the following below proxy_set_header X-Forwarded-Ssl on;

set $https_enabled on;

It may help, solve a similar problem once for me.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.