如何在 nginx 配置中重写 https 上的文件名?
例如我有
/public/abc.html /public/abc-s.html
但我想要访问https://mydomain/abc.html
load的用户abc-s.html
,而http://mydomain/abc-s.html
load 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 上测试。