Configuration NGINX:给定自定义子域,location regex与$http#u user#u agent匹配';行不通 服务器{ 听80; 服务器名称^(?.+)\(测试)?网站\.com$; 位置^/event/(\d+)${ 代理传递头服务器; 代理设置头主机$http\U主机; 代理_重定向关闭; 代理集头X-Real-IP$remote\u addr; 代理集头X-Scheme$Scheme; 上的代理截获错误; #这就是问题所在 #此条件将打断整个位置块。 #如果我对If语句进行了注释,则默认的proxy_pass可以工作。 如果($http_user_agent~*iphone | android){ #proxy_pass确实有效,在开发机器上进行了测试 代理通行证http://frontends/userland/mobile/event/$1; 打破 } #如果启用上述条件,则此操作失败。 代理通行证http://frontends/userland/event/$1; } }
注意到服务器名称中的子域匹配器几乎是一个通配符 为什么if条件不起作用?如果我所做的是错误的,那么重写它的最佳方式是什么 Nginx版本:1.2.0请参阅和了解为什么Nginx中的“如果”可能是邪恶的 事物的继承方式通常不能遵循预期的形式,而“return”和“rewrite”(带有“last”)等指令是Nginx“if”块中唯一真正可靠的指令 我将尝试这样的方法:Configuration NGINX:给定自定义子域,location regex与$http#u user#u agent匹配';行不通 服务器{ 听80; 服务器名称^(?.+)\(测试)?网站\.com$; 位置^/event/(\d+)${ 代理传递头服务器; 代理设置头主机$http\U主机; 代理_重定向关闭; 代理集头X-Real-IP$remote\u addr; 代理集头X-Scheme$Scheme; 上的代理截获错误; #这就是问题所在 #此条件将打断整个位置块。 #如果我对If语句进行了注释,则默认的proxy_pass可以工作。 如果($http_user_agent~*iphone | android){ #proxy_pass确实有效,在开发机器上进行了测试 代理通行证http://frontends/userland/mobile/event/$1; 打破 } #如果启用上述条件,则此操作失败。 代理通行证http://frontends/userland/event/$1; } },configuration,nginx,Configuration,Nginx,注意到服务器名称中的子域匹配器几乎是一个通配符 为什么if条件不起作用?如果我所做的是错误的,那么重写它的最佳方式是什么 Nginx版本:1.2.0请参阅和了解为什么Nginx中的“如果”可能是邪恶的 事物的继承方式通常不能遵循预期的形式,而“return”和“rewrite”(带有“last”)等指令是Nginx“if”块中唯一真正可靠的指令 我将尝试这样的方法: server { listen 80; server_name ~^(?<custom>.+)\.(t
server {
listen 80;
server_name ~^(?<custom>.+)\.(test)?website\.com$;
location ~ ^/event/(\d+)$ {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_intercept_errors on;
# This is the problematic block
# This conditional breaks the whole location block.
# If I commented the if statement, the default proxy_pass works.
if ($http_user_agent ~* iphone|android) {
# the proxy_pass indeed works, tested on development machine
proxy_pass http://frontends/userland/mobile/event/$1;
break;
}
# When above if conditional is enabled, this fails.
proxy_pass http://frontends/userland/event/$1;
}
}
服务器{
听80;
服务器名称^(?.+)\(测试)?网站\.com$;
位置^/event/(\d+)${
代理传递头服务器;
代理设置头主机$http\U主机;
代理_重定向关闭;
代理集头X-Real-IP$remote\u addr;
代理集头X-Scheme$Scheme;
上的代理截获错误;
如果($http_user_agent~*iphone | android){
返回302http://frontends/userland/mobile/event/$1;
#应在浏览器中保留原始url的替代语法
#重写^http://frontends/userland/mobile/event/最后1元;
}
代理通行证http://frontends/userland/event/$1;
}
}
通过在if
条件下评估iphone | android
来覆盖来自^/event/(\d+)$
的PCRE捕获。因此,执行重写规则后,$1
变量为空
试着这样做:
server {
listen 80;
server_name ~^(?<custom>.+)\.(test)?website\.com$;
location ~ ^/event/(\d+)$ {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_intercept_errors on;
if ($http_user_agent ~* iphone|android) {
return 302 http://frontends/userland/mobile/event/$1;
# Alternative syntax which should keep original url in browser
#rewrite ^ http://frontends/userland/mobile/event/$1 last;
}
proxy_pass http://frontends/userland/event/$1;
}
}
这正是问题所在,匹配的用户代理浪费了位置捕获。谢谢。除了
set
指令,您还可以使用命名捕获:location~ ^/event/(?\d+)$
set $num $1;
if ($http_user_agent ~* iphone|android) {
proxy_pass http://frontends/userland/mobile/event/$num;
}
proxy_pass http://frontends/userland/event/$num;