2016-01-21 109 views
1

关注我的配置文件:Nginx的 - 从HTTP重定向正确://到https://加www

server { 
    listen [::]:443 ipv6only=off ssl; 
    server_name www.example.com; 
    // ssl stuff 
    return 301 https://example.com$request_uri; 
} 

server { 
    listen [::]:80 ipv6only=off; 
    return 301 https://example.com$request_uri; 
} 

server { 
    listen [::]:443 ssl; 
    server_name example.com; 
    // php and ssl stuff 
} 

我不明白为什么http://www.example.com重定向到https://www.example.com再到https://example.com。如何将http://www.example.com直接重定向到https://example.com

+1

您是否启用了HSTS?如果是的话,那么第一次重定向是由您的浏览器直接完成的,没有任何网络交互,所以没有什么可担心的。 – Tom

+0

@Tom是的!很高兴知道!万分感谢。 – Joy

+0

所以我让你更新你的问题添加HSTS并验证我的答案:) – Tom

回答

0

启用HSTS后,第一次重定向直接由您的浏览器完成,无需任何网络交互。

1

NGINX配置从HTTP到HTTPS重定向没有WWW:

server { 
    listen 80 default_server; 
    listen [::]:80 default_server; 
    server_name example.com www.example.com; 
    return 301 https://example.com$request_uri; 
} 
server { 
    listen 443 default_server; 
    listen [::]:443 ssl http2 default_server; 
    server_name example.com www.example.com; 
    ##here-ssl-settings## 
    return 301 https://example.com$request_uri; 
}