如何在nginx配置中重写https上的文件名?
例如我有
/public/abc.html /public/abc-s.html
但我希望用户访问https://mydomain/abc.html
加载abc-s.html
,而http://mydomain/abc-s.html
加载abc.html
。
THX
答案 0 :(得分:0)
这可以通过一系列正则表达式来完成;例如:
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上测试。