2017-02-19 89 views
1

我設置了一個nginx web服務器來支持多個虛擬主機。遵循最佳實踐,我希望將任何http://請求重定向到相應的https://。如何配置nginx重定向到https ...除了一個目錄

這很簡單,但我想要有一個例外:/.well-known/下的任何文件的任何請求都應該作爲http,而不使用https重定向。任何使用LetsEncrypt的人都會將「.well-known」目錄識別爲LetsEncrypt查找驗證文件的位置。這些必須通過HTTP提供

到目前爲止,我有「默認」,看起來像一個配置文件:

server { 
    listen 80 default_server; 
    listen [::]:80 default_server ipv6only=on; 
    root /var/www/default/www; 
    index index.php index.html index.htm; 
    location ^~ /.well-known/ { 
     allow all; 
    } 
    location/{ 
     return 301 https://$host$request_uri; 
    } 
} 

然後,對於每個虛擬主機,我有一個單獨的文件看起來像:

server { 
    listen 443 ssl; 
    server_name myexamplevirtualhost.com; 
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    # lots more SSL-related stuff 
    location/{ 
     try_files $uri $uri/ =404; 
    } 
} 

server { 
    listen 80; 
    server_name myexamplevirtualhost.com; 
    location/{ 
     return 301 https://$server_name$request_uri; 
    } 
    location /.well-known/ { } 
} 

如果我請求,例如,http://myexamplevirtualhost.com/,我會重定向到https://myexamplevirtualhost.com/ - 這正是我想要的。同樣,對https://myexamplevirtualhost.com/的直接請求按預期工作。

但是,如果我嘗試:http://myexamplevirtualhost.com/.well-known/foo123,而不是服務器只是提供http://myexamplevirtualhost.com/.well-known/foo123(這是目標),它會重定向到https://myexamplevirtualhost.com/.well-known/foo123

我試了很多不同的東西 - 改變位置規則的順序等 - 但我仍然無法得到我想要的行爲。

我在做什麼錯?

+0

您是否擁有HTTP嚴格傳輸安全性? –

+0

是的,單個域的服務器塊包括: add_header嚴格傳輸安全最大年齡= 15768000 嗯。這可能是我的問題。 – slamci

回答

1

如果您啓用了HSTS(HTTP嚴格傳輸安全性),您將無法爲某些頁面「關閉」https,因爲https將被強制用於整個域。您可能可以使用一些錯綜複雜的HTTP 302重定向設置來完成這項工作,但我不確定您爲什麼要這麼做。如果您的.well-known目錄通過HTTPS提供服務,那麼讓我們加密將無法更新證書。

+0

我相信使用HSTS確實是問題,正如你和理查德史密斯所發現的一樣。 – slamci