【发布时间】: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。
谢谢
【问题讨论】:
如何在 nginx 配置中重写 https 上的文件名?
例如我有
/public/abc.html /public/abc-s.html
但我希望访问https://mydomain/abc.html 的用户加载abc-s.html,而http://mydomain/abc-s.html 加载abc.html。
谢谢
【问题讨论】:
这可以通过一系列正则表达式来完成;示例:
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 上测试。
【讨论】: