我知道的第一种方法是对 $args 参数使用正则表达式,如下所示:
if ($args ~ "^(\w+)=") {
或者第二种方法是像这样使用方便的 $is_args :
if ($is_args != "") {
请记住,在 两种 样式中,您需要在 if 和左括号之间放置一个空格; "if (" 不是 "if(" 以及右括号和左大括号后面的空格;") {" 而不是 "){"。
使用上面第一种样式的完整示例,nginx.conf:
location ~^/static/photos/.* {
include /etc/nginx/proxy.conf;
if ($args ~ "^(\w+)=") {
rewrite ^/static/photos/(.*)$ /DynamicPhotoQualitySwitch/photos/$1 break;
expires 7d;
proxy_pass http://foofoofoo.com;
}
}
使用上面第二种样式的完整示例,nginx.conf:
location ~^/static/photos/.* {
include /etc/nginx/proxy.conf;
if ($is_args != "") {
rewrite ^/static/photos/(.*)$ /DynamicPhotoQualitySwitch/photos/$1 break;
expires 7d;
proxy_pass http://foofoofoo.com;
}
}
请注意,proxy.conf 包含在 if 语句之外。
版本:
[nginx@hip1 ~]$ nginx -v
nginx version: nginx/1.2.6
还有一些关于 $args 和 $is_args 变量的信息:
http://nginx.org/en/docs/http/ngx_http_core_module.html
阅读文档总是有用的,我刚刚发现 $query_string 和 $args 是一样的,所以我上面有 $args 的地方,你也可以根据文档使用 $query_string。
重要
但需要注意的是,If can be Evil!
因此,要么彻底测试,要么使用上面链接中提供的建议,以类似于此处提供的示例的方式更改位置语句中的 URL,例如:
location ~^/static/photos/.* {
error_page 418 = @dynamicphotos;
recursive_error_pages on;
if ($is_args != "") {
return 418;
}
# Your default, if no query parameters exist:
...
}
location @dynamicphotos {
# If query parameters are present:
rewrite ^/static/photos/(.*)$ /DynamicPhotoQualitySwitch/photos/$1 break;
expires 7d;
include /etc/nginx/proxy.conf;
proxy_pass http://foofoofoo.com;
}