我使用 nginx 作为代理,使用 certbot 作为我的 SSL。我可以使用我的裸域通过 HTTPS 获取主页和子页面,但是添加
www.
会导致它给我 nginx 欢迎页面。下面是我当前的 /etc/nginx/sites-available/django
文件,它也在 /etc/nginx/sites-enabled/django
: 中进行了符号链接
server {
server_name www.example.com;
return 301 http://example.com$request_uri;
}
server {
server_name example.com;
# root /var/www/html;
location / {
# index new.html;
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
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 {
# server_name www.example.com;
## root /var/www/html;
# location / {
## index new.html;
# proxy_pass http://127.0.0.1:8000;
# proxy_set_header Host $host;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection upgrade;
# proxy_set_header Accept-Encoding gzip;
# }
#
# listen [::]:443 ssl ipv6only=on; # managed by Certbot
# listen 443 ssl; # managed by Certbot
# ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
# ssl_certificate_key /etc/letsencrypt/live/www.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 = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by me
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
如配置所示,我最初尝试为
www.
域设置一个单独的服务器块,但这也没有用。目前使用此配置,我在任何子页面 (https://www.example.com/example
) 和主域上的 nginx 欢迎页面上收到 404 错误。有什么想法吗?