Nginx子域太多重定向

时间:2018-04-26 14:15:57

标签: django ssl nginx

我目前为https://www.example.comhttp://sub.example.com设置了Django + Gunicorn + Nginx设置。请注意,主域名具有ssl而子域名不具有。

这适用于以下两个nginx配置。首先是www.example.com

upstream example_app_server {
  server unix:/path/to/example/gunicorn/gunicorn.sock fail_timeout=0;
}

server {
 listen 80;
 server_name www.example.com;

 return 301 https://www.example.com$request_uri;
}

server {
    listen   443 ssl;
    server_name www.example.com;

    if ($host = 'example.com') {
      return 301 https://www.example.com$request_uri;
    }

    ssl_certificate       /etc/nginx/example/cert_chain.crt;
    ssl_certificate_key   /etc/nginx/example/example.key;
    ssl_session_timeout   1d;
    ssl_session_cache     shared:SSL:50m;
    ssl_protocols         TLSv1.1 TLSv1.2;
    ssl_ciphers           'ciphers removed to save space in post';
    ssl_prefer_server_ciphers   on;

    client_max_body_size 4G;

    access_log            /var/log/nginx/www.example.com.access.log;
    error_log             /var/log/nginx/www.example.com.error.log info;

    location /static {
      autoindex on;
      alias /path/to/example/static;
    }

    location /media {
      autoindex on;
      alias /path/to/example/media;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://example_app_server;
            break;
        }
    }
}

接下来是sub.example.com

upstream sub_example_app_server {
  server unix:/path/to/sub_example/gunicorn/gunicorn.sock fail_timeout=0;
}

server {
    listen   80;
    server_name sub.example.com;
    client_max_body_size 4G;
    access_log            /var/log/nginx/sub.example.com.access.log;
    error_log             /var/log/nginx/sub.example.com.error.log info;

    location /static {
      autoindex on;
      alias /path/to/sub_example/static;
    }

    location /media {
      autoindex on;
      alias /path/to/sub_example/media;
    }

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      if (!-f $request_filename) {
        proxy_pass http://sub_example_app_server;
        break;
      }
    }
}

如上所述,这一切都有效。我现在要做的是在子域上使用ssl。为此目的,我有第二个ssl证书,该证书已使用此子域的域注册表激活。

我已经更新了sub.example.com上面的原始nginx配置,其格式与example.com完全相同,但指向相关的ssl证书/密钥等:

upstream sub_example_app_server {
  server unix:/path/to/sub_example/gunicorn/gunicorn.sock fail_timeout=0;
}

server {
 listen 80;
 server_name sub.example.com;

 return 301 https://sub.example.com$request_uri;
}

server {
    listen   443 ssl;
    server_name sub.example.com;

    if ($host = 'sub.example.com') {
      return 301 https://sub.example.com$request_uri;
    }

    ssl_certificate       /etc/nginx/sub_example/cert_chain.crt;
    ssl_certificate_key   /etc/nginx/sub_example/example.key;
    ssl_session_timeout   1d;
    ssl_session_cache     shared:SSL:50m;
    ssl_protocols         TLSv1.1 TLSv1.2;
    ssl_ciphers           'ciphers removed to save space in post';
    ssl_prefer_server_ciphers   on;

    client_max_body_size 4G;

    access_log            /var/log/nginx/sub.example.com.access.log;
    error_log             /var/log/nginx/sub.example.com.error.log info;

    location /static {
      autoindex on;
      alias /path/to/sub_example/static;
    }

    location /media {
      autoindex on;
      alias /path/to/sub_example/media;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://sub_example_app_server;
            break;
        }
    }
}

我没有使用我的域注册/ dns更改任何内容,因为在为子域添加ssl之前,所有内容都已正常工作。不确定是否有需要改变的东西?

浏览到http://sub.example.com时,我被重定向到https://sub.example.com,因此该部分似乎正在运行。但是,网站未加载,浏览器错误为:This page isn't working. sub.example.com redirected you too many times. ERR_TOO_MANY_REDIRECTS

https://www.example.com仍在使用。

我的nginx或gunicorn日志中没有任何错误。我只能猜测我在sub.example.com nginx配置中配置错误。

1 个答案:

答案 0 :(得分:2)

ssl服务器配置中的部分:

if ($host = 'sub.example.com') { return 301 sub.example.com$request_uri } 

是问题所在。该规则将始终被触发。删除它应该消除太多的重定向错误。