2017-02-24 60 views
1

我有兩個版本的網站與網址:http://example.comhttps://example.com如何在Nginx中將靜態文件請求重定向到https?

我想將所有請求重定向到靜態內容(以.html,.htm.js結尾的文件)到https版本的我的網站。

所以,我創建的規則:

location ~ "\.(htm|html|js|css|svg|png)$" { 
     return 307 https://example.com$request_uri; 
} 

有了這個規則的瀏覽器改變了我的網站的地址https://example.com

但我不想更改地址,我希望所有對靜態文件的請求,但不要index.html(我網站的主html)將被重定向到https版本。

如何添加AND NOT index.html之類的東西到正則表達式~ "\.(htm|html|js|css|svg|png)$"

回答

1

嘗試:

root /path/to/root; 

location = /index.html { 
} 

location ~ "\.(htm|html|js|css|svg|png)$" { 
    return 307 https://example.com$request_uri; 
} 

location =塊具有最高的優先級(順序並不重要)。

由於明確或隱含的index index.html聲明,URI /導致nginx查找/index.html。空的位置塊會導致靜態文件被提供,並且避免使用return 307

請參閱this document瞭解更多信息。

相關問題