【问题标题】:nginx rewrite file name on secure connectionnginx在安全连接上重写文件名
【发布时间】:2013-07-08 06:26:40
【问题描述】:

如何在 nginx 配置中重写 https 上的文件名?

例如我有

/public/abc.html /public/abc-s.html

但我希望访问https://mydomain/abc.html 的用户加载abc-s.html,而http://mydomain/abc-s.html 加载abc.html

谢谢

【问题讨论】:

    标签: http https nginx rewrite


    【解决方案1】:

    这可以通过一系列正则表达式来完成;示例:

      set $hasdashs u; # if we don't match .html we don't want to do a rewrite
      if ($uri ~* "^(.*)\.html$") { # are we looking at a .html page? If so, get the base name
        set $hasdashs n;
        set $shorturi "$1";
      }
      if ($uri ~ "^(.*)-s\.html$") { # are we looking at a secure page? Get the base name without -s
        set $hasdashs y;
        set $shorturi "$1";
      }
      set $schemecheck "$scheme$hasdashs";
    
      if ($schemecheck = "httpy") { #we're using http and looking at a secure page
        rewrite . "${shorturi}.html" redirect;
      }
      if ($schemecheck = "httpsn") { #we're using https and looking at an insecure page
        rewrite . "${shorturi}-s.html" redirect;
      }
    

    请注意,这必须在服务器块中,而不是在配置的位置块中而不是。在 NGINX 1.2.1 上测试。

    【讨论】:

      猜你喜欢
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2021-08-12
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多