2011-11-24 152 views
2

我正在使用nginx将来自客户端的请求代理到J​​SON数据提供程序后端。如何替换其中一个请求参数(如果存在),但不满足正则表达式^\d{1,2}$如何替换http请求参数值

现在我在这一点上:

location/{ 
     if ($arg_limit !~ "^\d{1,2}$") { 
      rewrite   ^(.*)$ $1?limit=10 break; 
     } 

     proxy_pass http://data_provider; 
    } 

它的工作原理,当没有定义limit的说法,但发生双limit的参数,如果在请求中定义的一个。

要求的行为:

# no limit arg, add one with default value 
/path?key=1 → /path?limit=10&key=1 

# arg value satisfies regexp, don't rewrite 
/path?key=1&limit=50 → /path?key=1&limit=50 

# arg value doesn't satisfies regexp, replace with default 
/path?key=1&limit=5000 → /path?limit=10&key=1 

参数顺序并不重要,当然。

nginx/1.0.10 
configure arguments: 
    --with-ipv6 --with-pcre --with-http_ssl_module 
    --with-http_addition_module --with-http_xslt_module 
    --with-http_geoip_module --with-http_gzip_static_module 
    --with-http_secure_link_module 

回答