Error 310. Too many redirects with nginx + rails 3

2019-09-07 06:05发布

问题:

Hello please review my nginx configuration below. When I try to go to my homepage http://mydomain.com I get the following error. When I look at the redirects using Chrome developer tools I see that http://mydomain.com is redirecting to https://mydomain.com and back and forth. I looked at my source code but I can't find any redirect there. I am using ssl_requirement plugin.

Any help is greatly appreciated.

Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

Below is my nginx configuration file

server {
    listen 80;
    server_name www.mydomain.com;
    rewrite ^/(.*) http://mydomain.com/$1 permanent;
}

server {
    listen 80;

    server_name mydomain.com;

    access_log /var/www/mydomain/current/log/access.log;
    root /var/www/mydomain/current/public;

    passenger_enabled on;
    passenger_use_global_queue on;

    location ~ /\.ht {
       deny all;
    }
}

server {
    listen 443;

    ssl on;
    ssl_certificate /home/ubuntu/ssl-cert/nopassphrase.pem;
    ssl_certificate_key /home/ubuntu/ssl-cert/nopassphrase.key;

    server_name www.mydomain.com;
    rewrite ^/(.*) http://mydomain.com/$1 permanent;
}

server {
    listen 443;

    ssl on;
    ssl_certificate /home/ubuntu/ssl-cert/nopassphrase.pem;
    ssl_certificate_key /home/ubuntu/ssl-cert/nopassphrase.key;

    server_name mydomain.com;

    access_log /var/www/mydomain/current/log/access.log;
    root /var/www/mydomain/current/public;

    location ~* \.(ico|jpg|gif|png|css|js|swf|html)$ {
      if (-f $request_filename) {
        expires max;
        break;
      }
    }
    passenger_enabled on;
    passenger_use_global_queue on;

    location ~ /\.ht {
       deny all;
    }
}

回答1:

You config is redirecting to itself.

You can put the http and https in the same server block. The certificate should be a chained cert is it is not a self-signed one.

server {
    listen 80;
    server_name www.mydomain.com;
    rewrite ^/(.*) http://mydomain.com/$1 permanent;
}

server {
    listen 80;
    listen 443 ssl;
    server_name mydomain.com;
    ssl_certificate /home/ubuntu/ssl-cert/nopassphrase.pem;
    ssl_certificate_key /home/ubuntu/ssl-cert/nopassphrase.key;

    location ~* \.(ico|jpg|gif|png|css|js|swf|html)$ {
        if (-f $request_filename) {
            expires max;
            break;
        }
    }

    access_log /var/www/mydomain/current/log/access.log;
    root /var/www/mydomain/current/public;

    passenger_enabled on;
    passenger_use_global_queue on;

    location ~ /\.ht {
        deny all;
    }
}