2017-03-20 40 views
0

我在我的/etc/nginx/sites-enabled/example.com文件中有一個基本配置,該文件將HTTP流量重定向到站點的HTTPS版本。如何在域名在Nginx使用https時使用http子域名

server { 
    listen 80; 
    server_name example.com www.example.com; 
    rewrite^https://example.com$request_uri? permanent; 
} 

server { 
    listen 443; 
    server_name example.com; 
    ssl on; 
    ... rest of the site configuration 
} 

以上工作正常。當我訪問http://example.com時,我被重定向到https://example.com

我需要example.com上的子域,但它只需要HTTP。我創建了一個/etc/nginx/sites-enabled/sub.example.com文件與

server { 
    listen 80; 
    server_name sub.example.com; 

開始在第一,它似乎工作,但我注意到一個問題:

  1. 清除了瀏覽器緩存,並參觀http://sub.example.com。該網站出現沒有任何問題。
  2. 接下來,訪問http://example.com。瀏覽器被重定向到 https://example.com這很好。
  3. 返回並再次訪問http://sub.example.com這一次,瀏覽器被重定向到https://sub.example.com,瀏覽器抱怨缺少證書。

這是怎麼發生的?什麼是正確的方式來使用HTTP的子域,但HTTPS的域?

+3

你有HSTS嗎? - https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security –

+2

我99%確定你的HSTS頭裏有'includeSubDomains'指令 –

回答