Nginx将子域名foo重定向到www.foo到www.www.foo等



我刚刚将我的配置从常规证书更改为通配符证书。现在我的 nginx 行为不端。

# redirect http to https
server {
listen      80 default_server;
listen      [::]:80 default_server;
return      301 https://$host$request_uri;
}
# redirect naked to www
server {
listen      443 ssl http2;
listen      [::]:443 ssl http2;
server_name example.com;
include     ssl.conf;
return      301 https://www.$host$request_uri;
}
# serve subdomain www
server {
listen      443 ssl http2;
listen      [::]:443 ssl http2;
server_name www.example.com;
include     ssl.conf;
# ...
}
# serve subdomain mmm
server {
listen      443 ssl http2;
listen      [::]:443 ssl http2;
server_name mmm.example.com;
include     ssl.conf;
# ...
}
# ...etc.

上述方法有效,但对于不存在的子域(而不是返回 404(失败。因此,如果我尝试notexist.example.com它会将我重定向到www.notexist.example.com并给我一个证书警告。如果我单击确定,它将重定向到www.www.notexist.example.com,然后www.www.www.notexist.example.com等。

我做错了什么?

由于您想捕获所有不存在的子域,因此您需要在末尾添加一个额外的服务器块,标记为默认值 - 如listen 443 ssl default_server;这个块的server_name无关紧要 - 只要它与任何其他服务器块不匹配(所以你可以简单地使用server_name _;(

任何尚未由其他服务器块处理的域都将由默认处理 - 您可以重定向到您的规范域或只返回 404。

相关内容

最新更新