2011-11-24 157 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 

回答