django - Nginx 子域重定向太多

标签 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 将您重定向了太多次。 ERR_TOO_MANY_REDIRECTS

https://www.example.com 仍在工作。

我的 nginx 或 gunicorn 日志中没有任何错误。我只能猜测我在 sub.example.com nginx 配置中配置了一些不正确的东西。

最佳答案

ssl 服务器配置部分:

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

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

关于django - Nginx 子域重定向太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50045314/

相关文章:

python - Django ModelView 的 "fields"属性不起作用?

django - 在 virtualenv 中创建项目时没有名为 django.core 的模块

wordpress - Woocommerce REST API https - 无法连接

amazon-web-services - AWS Certificate Manager 证书对控制台中的 AWS Beanstalk 不可见

mysql - nginx 上的 PHP-FPM 无法连接到外部 MySQL

javascript - 使用 django 在 HTML 中显示 mpld3 图表

python - Django 中 Raw 的 MySQL SET 语法

php - 如何将 php session 绑定(bind)到用户 SSL 证书

authentication - 有没有办法在 nginx 中使用多个 auth_request 指令?

node.js - 是否可以为两个/三个不同的域(别名)使用相同的 node.js 服务器?