2017-02-24 63 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了解更多信息。

相关问题