gpt4 book ai didi

django - Nginx 子域重定向太多

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:29 24 4
gpt4 key购买 nike

我目前有一个适用于 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com