【发布时间】:2021-02-20 18:57:05
【问题描述】:
我的目标是将example.com 重定向到www.example.com,而不将any 子域重定向到www。
这就是我所拥有的:
server {
listen 443;
server_name example.com;
return 301 https://www.$server_name$request_uri;
}
使用这种配置,每个子域(例如:foo.example.com)都会被重定向到www.example.com,而不仅仅是没有子域的主域(example.com),正如我所愿。
我尝试清理缓存(也可以从其他浏览器执行),结果相同。
编辑:
server_name 与子域匹配的证据是,如果我将重定向 url 更改为:https://www.$host$request_uri,则:
foo.example.com 被重定向到 www.foo.example.com。
【问题讨论】:
-
此服务器块也是处理任何与任何 server_name 不匹配的请求的默认服务器块。见how nginx processes a request。您需要定义一个包罗万象的服务器。见this answer。
标签: ubuntu nginx subdomain virtualhost ubuntu-19.10