在nginx中端口80的服务器指令中,我想将所有请求重定向到https 如果用户代理不是机器人。我试过用这个:
...
location / {
if ($http_user_agent !~* (bot|spider|crawler|sniffer|facebook) ) {
return 301 https://host.com$request_uri;
}
include other-stuff.inc;
}
我认为nginx会停在return
上,但事实并非如此。它仍然处理include
(服务于普通网站)并且不重定向。 (如果我评论include
它会进行重定向。)
答案 0 :(得分:0)
也许如果你在返回后添加一个中断它可以工作,试试这个
if (whatever){
return 301 ...;
break;
}
# include and the rest
答案 1 :(得分:0)
我认为在你的包含中你有其他一些位置? 如果是这样,根据上下文,您可能会遇到“if”工作不同的问题。这解释为here
您可以尝试将其移至服务器配置:
server {
... server config
if ($http_user_agent !~* (bot|spider|crawler|sniffer|facebook) ) {
return 301 https://host.com$request_uri;
}
location / {
include other-stuff.inc;
}
}