英文:
HTTPS url not redirecting properly (Nginx)
问题
我有一个不包含www的网站域名,我正在努力使用HTTPS获取适当的重定向。
HTTP请求可以正确重定向
即 http://www.website.com -> https://website.com 这是我想要的
然而,HTTPS请求没有正确重定向,导致重复的页面。
即 https://www.website.com -> https://www.website.com 但应该转到 https://website.com
我认为这可能是我的Nginx sites_enabled脚本的问题,但我无法弄清楚。
server {
listen 80;
server_name website.com www.website.com;
return 301 $scheme://website.com$request_uri;
}
server {
listen 443 ssl;
server_name website.com www.website.com;
ssl_certificate /etc/nginx/ssl/website.crt;
ssl_certificate_key /etc/nginx/ssl/website.key;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/website/django_webApp/webApp;
}
location /staticfiles/ {
root /home/website/django_webApp/webApp;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
英文:
I have a website domain that does not contain www. and I am struggling to get proper redirects using HTTPS.
HTTP requests do get properly redirected
i.e. http://www.website.com -> https://website.com which is what I want
However HTTPS request do not get properly redirected which results in a duplicate page.
i.e. https://www.website.com -> https://www.website.com but should be going to https://website.com
I do believe it is an issue with my Nginx sites_enabled script but I cannot figure it out.
server {
listen 80;
server_name website.com www.website.com;
return 301 $scheme://website.com$request_uri;
}
server {
listen 443 ssl;
server_name website.com www.website.com;
ssl_certificate /etc/nginx/ssl/website.crt;
ssl_certificate_key /etc/nginx/ssl/website.key;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/website/django_webApp/webApp;
}
location /staticfiles/ {
root /home/website/django_webApp/webApp;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
答案1
得分: 0
Think through the logic. Right now you have a redirect from HTTP on either website.com or www.website.com to http at website.com, followed by a block listening at both of those domains. If you're on port 80, you don't want to redirect to $scheme
, you want to redirect to https.
server {
listen 80;
server_name website.com www.website.com;
return 301 https://website.com$request_uri;
}
If you're at https://www.website.com, you want to redirect to website.com
server {
listen 443 ssl;
server_name www.website.com;
ssl_certificate /etc/nginx/ssl/website.crt;
ssl_certificate_key /etc/nginx/ssl/website.key;
return 301 $scheme://website.com$request_uri;
}
Followed by your last "real" server block:
server {
listen 443 ssl;
server_name website.com;
// everything else
}
This unfortunately could result in more than one redirect, but is the easiest code to understand. More options are discussed over here.
英文:
Think through the logic. Right now you have a redirect from HTTP on either website.com or www.website.com to http at website.com, followed by a block listening at both of those domains. If you're on port 80, you don't want to redirect to $scheme
, you want to redirect to https.
server {
listen 80;
server_name website.com www.website.com;
return 301 https://website.com$request_uri;
}
If you're at https://www.website.com, you want to redirect to website.com
server {
listen 443 ssl;
server_name www.website.com;
ssl_certificate /etc/nginx/ssl/website.crt;
ssl_certificate_key /etc/nginx/ssl/website.key;
return 301 $scheme://website.com.com$request_uri;
}
Followed by your last "real" server block:
server {
listen 443 ssl;
server_name website.com;
// everything else
}
This unforunately could result in more than one redirect, but is the easiest code to understand. More options are discussed [over here](https://serverfault.com/questions/258378/remove-www-and-redirect-to-https-with-nginx).
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论