Nginx重定向(非www到www)不能与Certbot一起使用 [英] Nginx redirect (non-www to www) not working with Certbot
问题描述
我有一个运行Python/Django/uWSGI/Nginx设置的网站.我还使用Certbot在我的网站上启用https.我从非www重定向到www(例如,从"example.com"重定向到"www.example.com")导致出现错误请求(400)"消息,即使我无法发现与Nginx/Certbot文档的任何差异.这是我的sites-available
Nginx代码的相关部分:
I have a website running with a Python/Django/uWSGI/Nginx setup. I also use Certbot to enable https on my site. My redirects from non-www to www (e.g. "example.com" to "www.example.com") result in a "Bad Request (400)" message even though I couldn't spot any deviations from the Nginx/Certbot documentation. Here is the relevant part of my sites-available
Nginx code:
server {
listen 80;
server_name example.com www.example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myname/example;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/activities.sock;
}
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
if ($scheme != "https") {
return 301 https://$host$request_uri;
} # managed by Certbot
}
我找到了类似的StackOverflow答案( Nginx:重定向非www访问https://a上的www ),但没有一种解决方案适合我.我同时拥有example.com和www.example.com的SSL证书.我还尝试根据该答案中的注释为example.com创建一个单独的443 ssl服务器块,但是它也不起作用.我的sites-available
和sites-enabled
代码是相同的.
I found a similar StackOverflow answer (Nginx: redirect non-www to www on https) but none of the solutions worked for me. I have SSL certificates for both example.com and www.example.com. I also tried creating a separate 443 ssl server block for example.com based on the comments in that answer but it didn't work either. My sites-available
and sites-enabled
code is the same.
我做错了什么?
推荐答案
似乎server_name转换为$ host变量后,会选择server_name列表中的第一个.让我知道是否可行.我目前无法对此进行测试.
It seems that server_name when translated to the $host variable selects the first in the list of server_name. Let me know if that works. I can't quite test this currently.
尝试将server_name交换为server_name www.example.com example.com;
,并将return 301 https://$host$request_uri;
更改为return 301 https://$server_name$request_uri;
Try swapping server_name to server_name www.example.com example.com;
as well as changing return 301 https://$host$request_uri;
to return 301 https://$server_name$request_uri;
server {
server_name www.example.com example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
# SSL CERT STUFF.
server_name example.com;
return 301 https://www.$server_name$request_uri;
}
server {
listen 443 ssl;
# SSL CERT STUFF.
server_name www.example.com;
# LOCATION STUFF
}
这篇关于Nginx重定向(非www到www)不能与Certbot一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!