HTTPS网址未正确重定向(Nginx)

huangapple go评论91阅读模式
英文:

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脚本的问题,但我无法弄清楚。

  1. server {
  2. listen 80;
  3. server_name website.com www.website.com;
  4. return 301 $scheme://website.com$request_uri;
  5. }
  6. server {
  7. listen 443 ssl;
  8. server_name website.com www.website.com;
  9. ssl_certificate /etc/nginx/ssl/website.crt;
  10. ssl_certificate_key /etc/nginx/ssl/website.key;
  11. location = /favicon.ico { access_log off; log_not_found off; }
  12. location /static/ {
  13. root /home/website/django_webApp/webApp;
  14. }
  15. location /staticfiles/ {
  16. root /home/website/django_webApp/webApp;
  17. }
  18. location / {
  19. include proxy_params;
  20. proxy_pass http://unix:/run/gunicorn.sock;
  21. }
  22. }
英文:

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.

  1. server {
  2. listen 80;
  3. server_name website.com www.website.com;
  4. return 301 $scheme://website.com$request_uri;
  5. }
  6. server {
  7. listen 443 ssl;
  8. server_name website.com www.website.com;
  9. ssl_certificate /etc/nginx/ssl/website.crt;
  10. ssl_certificate_key /etc/nginx/ssl/website.key;
  11. location = /favicon.ico { access_log off; log_not_found off; }
  12. location /static/ {
  13. root /home/website/django_webApp/webApp;
  14. }
  15. location /staticfiles/ {
  16. root /home/website/django_webApp/webApp;
  17. }
  18. location / {
  19. include proxy_params;
  20. proxy_pass http://unix:/run/gunicorn.sock;
  21. }
  22. }

答案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.

  1. server {
  2. listen 80;
  3. server_name website.com www.website.com;
  4. return 301 https://website.com$request_uri;
  5. }

If you're at https://www.website.com, you want to redirect to website.com

  1. server {
  2. listen 443 ssl;
  3. server_name www.website.com;
  4. ssl_certificate /etc/nginx/ssl/website.crt;
  5. ssl_certificate_key /etc/nginx/ssl/website.key;
  6. return 301 $scheme://website.com$request_uri;
  7. }

Followed by your last "real" server block:

  1. server {
  2. listen 443 ssl;
  3. server_name website.com;
  4. // everything else
  5. }

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.

  1. server {
  2. listen 80;
  3. server_name website.com www.website.com;
  4. return 301 https://website.com$request_uri;
  5. }

If you're at https://www.website.com, you want to redirect to website.com

  1. server {
  2. listen 443 ssl;
  3. server_name www.website.com;
  4. ssl_certificate /etc/nginx/ssl/website.crt;
  5. ssl_certificate_key /etc/nginx/ssl/website.key;
  6. return 301 $scheme://website.com.com$request_uri;
  7. }

Followed by your last "real" server block:

  1. server {
  2. listen 443 ssl;
  3. server_name website.com;
  4. // everything else
  5. }
  6. 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).
  7. </details>

huangapple
  • 本文由 发表于 2023年5月8日 02:35:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定