为什么我的Nginx wwwredirect不工作?

我有这样的configuration:

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 ,它将我正确地redirect到example.fr ,但是当我去www.example.com ,它并没有。 example.comwww.example.com我的DNS转到相同的IP。

有什么可能会出错?

编辑:curl

 ➜ ~ 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 

根据你发布的configuration,nginx运行正常。

下面的块应该是一个方法来ahieve你想要做的事情:

 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; } 

我的经验是,在转发服务器块中不包括适当的SSLconfiguration将导致https://example.com和https://www.example.com导致无redirect的浏览器信任警告。