【发布时间】:2021-02-12 19:38:25
【问题描述】:
我正在尝试将example.com/.* 下的所有内容重定向到www.example.com/..。通过调整 Nginx 中启用站点的设置。
我已设法使其仅适用于主页,但是当我尝试访问 example.com/es/ 之类的子文件夹时,它不会重定向到 www.example.com/es/。
这些是我当前的设置:
server {
server_name example.com www.example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /core/static {
alias /home/andrea/agency/core/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://www.$host$request_uri;
} # managed by Certbot
listen 80;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
我虽然 $request_uri 部分会处理 non-www 域下的所有内容,但它似乎不起作用。
您是否有任何暗示为什么它的行为不符合预期?非常感谢!
【问题讨论】:
-
您的测试可能被浏览器缓存和 HSTS 标头的存在弄糊涂了。但是您应该使用涵盖所有情况的解决方案,例如this answer