英文:
Nginx HTTPS Redirect from website.com to https://www.website.com
问题
以下是翻译好的部分:
我正在使用nginx来托管一个网站,并使用certbot创建了`www.website.com`的HTTPS证书。以下是当前的配置:
server {
root /var/www/website/html;
index index.html index.htm index.nginx-debian.html;
server_name website www.website.com website.com;
location / {
try_files $uri $uri/ =404;
}
listen [::]:443 ssl ipv6only=on; # 由Certbot管理
listen 443 ssl; # 由Certbot管理
ssl_certificate /etc/letsencrypt/live/www.website.com/fullchain.pem; # 由Certbot管理
ssl_certificate_key /etc/letsencrypt/live/www.website.com/privkey.pem; # 由Certbot管理
include /etc/letsencrypt/options-ssl-nginx.conf; # 由Certbot管理
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # 由Certbot管理
}
server {
if ($host = www.website.com) {
return 301 https://$host$request_uri;
} # 由Certbot管理
listen 80;
listen [::]:80;
server_name website www.website.com website.com;
return 404; # 由Certbot管理
}
这对于从`www.website.com`重定向到`https://www.website.com`有效。然而,当我尝试连接到`website.com`时,我收到一个404错误。
我想知道如何正确设置重定向,以使`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:
server {
root /var/www/website/html;
index index.html index.htm index.nginx-debian.html;
server_name website www.website.com website.com;
location / {
try_files $uri $uri/ =404;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.website.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.website.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.website.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name website www.website.com website.com;
return 404; # managed by Certbot
}
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
解决了这个问题:
server {
server_name website.com;
rewrite ^(.*) https://www.website.com$1 permanent;
}
此外,当我重启nginx时,我注意到在nginx.service
启动时出现了一些警告:
nginx.service:
Feb 24 00:42:04 webserver.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 24 00:42:04 webserver.com nginx[3321]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
Feb 24 00:42:04 webserver.com nginx[3321]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 24 00:42:04 webserver.com nginx[3321]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 24 00:42:05 webserver.com nginx[3322]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
Feb 24 00:42:05 webserver.com systemd[1]: Started The nginx HTTP and reverse proxy server.
我从配置文件的server_name
变量中移除了website.com
,然后再次重启,警告消失了。以下是现在的配置:
server {
server_name website.com;
rewrite ^(.*) https://www.website.com$1 permanent;
}
server {
root /var/www/website/html;
index index.html index.htm index.nginx-debian.html;
server_name website www.website.com;
location / {
try_files $uri $uri/ =404;
}
listen [::]:443 ssl ipv6only=on; # 由Certbot管理
listen 443 ssl; # 由Certbot管理
ssl_certificate /etc/letsencrypt/live/www.website.com/fullchain.pem; # 由Certbot管理
ssl_certificate_key /etc/letsencrypt/live/www.website.com/privkey.pem; # 由Certbot管理
include /etc/letsencrypt/options-ssl-nginx.conf; # 由Certbot管理
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # 由Certbot管理
}
server {
if ($host = www.website.com) {
return 301 https://$host$request_uri;
} # 由Certbot管理
listen 80;
listen [::]:80;
server_name website www.website.com;
return 404; # 由Certbot管理
}
英文:
Managed to figure it out by using rewrite
:
server {
server_name website.com;
rewrite ^(.*) https://www.website.com$1 permanent;
}
Additionally, when I restarted nginx, I noticed some warnings on the startup of nginx.service
:
nginx.service:
Feb 24 00:42:04 webserver.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 24 00:42:04 webserver.com nginx[3321]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
Feb 24 00:42:04 webserver.com nginx[3321]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 24 00:42:04 webserver.com nginx[3321]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 24 00:42:05 webserver.com nginx[3322]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
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:
server {
server_name website.com;
rewrite ^(.*) https://www.website.com$1 permanent;
}
server {
root /var/www/website/html;
index index.html index.htm index.nginx-debian.html;
server_name website www.website.com;
location / {
try_files $uri $uri/ =404;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.website.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.website.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.website.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name website www.website.com;
return 404; # managed by Certbot
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论