【问题标题】:How to redirect static files requests to https in Nginx?如何在 Nginx 中将静态文件请求重定向到 https?
【发布时间】:2017-07-15 20:44:07
【问题描述】:

我有两个版本的网址:http://example.comhttps://example.com

我想将所有请求重定向到静态内容(以.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)$"

【问题讨论】:

    标签: regex redirect nginx


    【解决方案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 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2017-01-24
      • 2017-05-05
      • 1970-01-01
      • 2015-03-02
      • 2016-05-21
      • 2018-09-15
      • 2011-06-14
      • 2019-01-24
      • 1970-01-01
      相关资源
      最近更新 更多