nginx:从http://*.example.comredirect到https://example.com,显示奇怪的行为

我有一个SSL证书只适用于example.com(不适用于子域名),因此想要将http://example.com和http://www.example.comredirecthttps://example.com nginx的。 我的configuration文件启动如下:

server { # This should catch all non-HTTPS requests to example.com and *.example.com listen 80; server_name example.com www.example.com; access_log off; return 301 https://example.com$request_uri; } server { listen 443 ssl; # Actual server config starts here... 

但是,redirect不能按要求运行。 请求http://example.com正确导向到https://example.com ,但http://www.example.com将被redirect到https://www.example.com ,由于我的证书产生SSL错误对子域无效。 我知道没有可能从无效的HTTPSredirect到有效的HTTPS,但我至less希望实现从HTTP到HTTPS的redirect,因为用户仍然倾向于inputwww。 在浏览器中。

当我wget --spider www.example.com ,它返回正确的结果:

 Spider mode enabled. Check if remote file exists. --2015-03-19 02:22:47-- http://www.example.com/ Resolving www.example.com (www.example.com)... ***.***.***.*** Connecting to www.example.com (www.example.com) **** connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: https://example.com/ [following] Spider mode enabled. Check if remote file exists. --2015-03-19 02:22:47-- https://example.com/ ... 

但由于某种原因,浏览器最终要求https://www.example.com而不是https://example.com 。 我已经尝试从多个浏览器和电脑。 我的错误在哪里? configuration文件中的整个第一个服务器块似乎完全被忽略 – 如果我删除它,浏览器将显示完全相同的行为(禁用DNScaching),而wget --spider的输出会一致地更改。