![为什么我的 Nginx www 重定向不起作用?](https://linux22.com/image/692157/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%88%91%E7%9A%84%20Nginx%20www%20%E9%87%8D%E5%AE%9A%E5%90%91%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%EF%BC%9F.png)
我有这样的配置:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.fr$request_uri;
}
server {
listen 443 ssl;
root /var/www/example_v2/;
index index.php index.html index.htm;
server_name example.com www.example.com;
return 301 https://example.fr$request_uri;
}
当我访问 时example.com
,它会正确地将我重定向到example.fr
,但当我访问 时www.example.com
,它却没有。我的 DNSexample.com
和www.example.com
指向同一个 IP。
可能出了什么问题?
编辑:卷曲
➜ ~ curl -L -I example.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.1
Date: Thu, 20 Oct 2016 11:50:38 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://example.fr/
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Thu, 20 Oct 2016 11:50:38 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 40538
Connection: keep-alive
Vary: Accept-Encoding
Strict-Transport-Security: max-age=31536000
➜ ~ curl -L -I www.example.com
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Thu, 20 Oct 2016 11:50:53 GMT
Content-Type: text/html
Content-Length: 176
Last-Modified: Thu, 15 Sep 2016 16:27:25 GMT
Connection: keep-alive
ETag: "57dacbed-b0"
Accept-Ranges: bytes
答案1
根据您发布的配置,nginx 运行正常。
以下块应该是实现您要做的事情的一种方法:
server {
listen 80;
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/ssl.crt;
ssl_certificate_key /path/to/server.key;
return 301 https://example.fr$request_uri;
}
server {
listen 80;
listen 443 ssl;
server_name www.example.com;
ssl_certificate /path/to/ssl.crt;
ssl_certificate_key /path/to/server.key;
return 301 https://www.example.fr$request_uri;
}
我的经验是,如果在转发服务器块中不包含适当的 SSL 配置,将导致https://example.com和https://www.example.com无需重定向就会导致浏览器信任警告。