Nginx HTTPS 从 website.com 重定向到 https://www.website.com

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

Nginx HTTPS Redirect from website.com to https://www.website.com

问题

以下是翻译好的部分:

  1. 我正在使用nginx来托管一个网站,并使用certbot创建了`www.website.com`HTTPS证书。以下是当前的配置:
  2. server {
  3. root /var/www/website/html;
  4. index index.html index.htm index.nginx-debian.html;
  5. server_name website www.website.com website.com;
  6. location / {
  7. try_files $uri $uri/ =404;
  8. }
  9. listen [::]:443 ssl ipv6only=on; # 由Certbot管理
  10. listen 443 ssl; # 由Certbot管理
  11. ssl_certificate /etc/letsencrypt/live/www.website.com/fullchain.pem; # 由Certbot管理
  12. ssl_certificate_key /etc/letsencrypt/live/www.website.com/privkey.pem; # 由Certbot管理
  13. include /etc/letsencrypt/options-ssl-nginx.conf; # 由Certbot管理
  14. ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # 由Certbot管理
  15. }
  16. server {
  17. if ($host = www.website.com) {
  18. return 301 https://$host$request_uri;
  19. } # 由Certbot管理
  20. listen 80;
  21. listen [::]:80;
  22. server_name website www.website.com website.com;
  23. return 404; # 由Certbot管理
  24. }
  25. 这对于从`www.website.com`重定向到`https://www.website.com`有效。然而,当我尝试连接到`website.com`时,我收到一个404错误。
  26. 我想知道如何正确设置重定向,以使`website.com`重定向到`https://www.website.com`
英文:

I'm using nginx to host a website, and have used certbot to create a HTTPS certificate for www.website.com. The following is the config currently:

  1. server {
  2. root /var/www/website/html;
  3. index index.html index.htm index.nginx-debian.html;
  4. server_name website www.website.com website.com;
  5. location / {
  6. try_files $uri $uri/ =404;
  7. }
  8. listen [::]:443 ssl ipv6only=on; # managed by Certbot
  9. listen 443 ssl; # managed by Certbot
  10. ssl_certificate /etc/letsencrypt/live/www.website.com/fullchain.pem; # managed by Certbot
  11. ssl_certificate_key /etc/letsencrypt/live/www.website.com/privkey.pem; # managed by Certbot
  12. include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  13. ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  14. }
  15. server {
  16. if ($host = www.website.com) {
  17. return 301 https://$host$request_uri;
  18. } # managed by Certbot
  19. listen 80;
  20. listen [::]:80;
  21. server_name website www.website.com website.com;
  22. return 404; # managed by Certbot
  23. }

This works for redirects from www.website.com to https://www.website.com. However, when I try and connect to website.com, I get a 404 error.

I am wondering how to correctly set up a redirect so that website.com redirects to https://www.website.com.

答案1

得分: 0

我成功地通过使用rewrite解决了这个问题:

  1. server {
  2. server_name website.com;
  3. rewrite ^(.*) https://www.website.com$1 permanent;
  4. }

此外,当我重启nginx时,我注意到在nginx.service启动时出现了一些警告:

  1. nginx.service:
  2. Feb 24 00:42:04 webserver.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
  3. Feb 24 00:42:04 webserver.com nginx[3321]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
  4. Feb 24 00:42:04 webserver.com nginx[3321]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  5. Feb 24 00:42:04 webserver.com nginx[3321]: nginx: configuration file /etc/nginx/nginx.conf test is successful
  6. Feb 24 00:42:05 webserver.com nginx[3322]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
  7. Feb 24 00:42:05 webserver.com systemd[1]: Started The nginx HTTP and reverse proxy server.

我从配置文件的server_name变量中移除了website.com,然后再次重启,警告消失了。以下是现在的配置:

  1. server {
  2. server_name website.com;
  3. rewrite ^(.*) https://www.website.com$1 permanent;
  4. }
  5. server {
  6. root /var/www/website/html;
  7. index index.html index.htm index.nginx-debian.html;
  8. server_name website www.website.com;
  9. location / {
  10. try_files $uri $uri/ =404;
  11. }
  12. listen [::]:443 ssl ipv6only=on; # 由Certbot管理
  13. listen 443 ssl; # 由Certbot管理
  14. ssl_certificate /etc/letsencrypt/live/www.website.com/fullchain.pem; # 由Certbot管理
  15. ssl_certificate_key /etc/letsencrypt/live/www.website.com/privkey.pem; # 由Certbot管理
  16. include /etc/letsencrypt/options-ssl-nginx.conf; # 由Certbot管理
  17. ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # 由Certbot管理
  18. }
  19. server {
  20. if ($host = www.website.com) {
  21. return 301 https://$host$request_uri;
  22. } # 由Certbot管理
  23. listen 80;
  24. listen [::]:80;
  25. server_name website www.website.com;
  26. return 404; # 由Certbot管理
  27. }
英文:

Managed to figure it out by using rewrite:

  1. server {
  2. server_name website.com;
  3. rewrite ^(.*) https://www.website.com$1 permanent;
  4. }

Additionally, when I restarted nginx, I noticed some warnings on the startup of nginx.service:

  1. nginx.service:
  2. Feb 24 00:42:04 webserver.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
  3. Feb 24 00:42:04 webserver.com nginx[3321]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
  4. Feb 24 00:42:04 webserver.com nginx[3321]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  5. Feb 24 00:42:04 webserver.com nginx[3321]: nginx: configuration file /etc/nginx/nginx.conf test is successful
  6. Feb 24 00:42:05 webserver.com nginx[3322]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
  7. Feb 24 00:42:05 webserver.com systemd[1]: Started The nginx HTTP and reverse proxy server.

I removed website.com from the server_name variables in my config file, restarted again, and the warnings were gone. The following is the config now:

  1. server {
  2. server_name website.com;
  3. rewrite ^(.*) https://www.website.com$1 permanent;
  4. }
  5. server {
  6. root /var/www/website/html;
  7. index index.html index.htm index.nginx-debian.html;
  8. server_name website www.website.com;
  9. location / {
  10. try_files $uri $uri/ =404;
  11. }
  12. listen [::]:443 ssl ipv6only=on; # managed by Certbot
  13. listen 443 ssl; # managed by Certbot
  14. ssl_certificate /etc/letsencrypt/live/www.website.com/fullchain.pem; # managed by Certbot
  15. ssl_certificate_key /etc/letsencrypt/live/www.website.com/privkey.pem; # managed by Certbot
  16. include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  17. ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  18. }
  19. server {
  20. if ($host = www.website.com) {
  21. return 301 https://$host$request_uri;
  22. } # managed by Certbot
  23. listen 80;
  24. listen [::]:80;
  25. server_name website www.website.com;
  26. return 404; # managed by Certbot
  27. }

huangapple
  • 本文由 发表于 2023年2月24日 04:51:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550201.html
匿名

发表评论

匿名网友

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

确定