我对nginx非常陌生(大约1天左右),我们正在使用它作为一些应用程序的反向代理。 我们将添加另一个将处理现有请求的一小部分的应用程序。 我正试图拦截那些碰到鳕鱼/文章API并在参数中包含item.id的url,然后把这些请求和它们的参数发送到这个我现在所没有准备好的新应用程序。
我想单独留下所有其他请求,即API不是“/ cod / acticles”,请求在参数中不包含item.id。
应该发送到新应用程序的示例url。
http://server/mdata/transfer/CUSTOMER/cod/articles?filter={"item.id":"ID00000000123"......}&limit=50&fields=[]&sort=.......
不应该发送的示例url。
http://server/mdata/transfer/customer/cod/articles?filter{"lang":"en_US","article.id":"123456"}&limit=50.... http://server/mdata/transfer/customer/cod/items?filter{"lang":"en_US","article.id":"123456"}&limit=50.... http://server/mdata/transfer/customer/doc/articles?filter{"lang":"en_US","article.id":"123456"}&limit=50....
注意:….只是意味着其他参数
以下是我目前在Nginx中的configuration。 它似乎做了“拦截”,但有一些东西看起来不对我。
首先,我相信使用“如果”并不理想,可能有更好的方法来做到这一点。 但是我没有成功的匹配参数部分。
其次,nginx访问日志显示3个匹配url的请求。 X2301响应和1200响应。 这是预期的吗? 如果是这样,有一个更好的方法来做到这一点,只有一个请求,因为它似乎我已经创build了Nginx的附加负载。
#existing config for all /mdata/transfer/* APIs location /mdata/transfer/ { add_header Access-Control-Allow-Origin *; allow all; proxy_pass http://mds; } #static page which acts as a stub end point for testing. location /proxyapp { root /opt/www; } #new config where I try to intercept /mdata/transfer/customer/cod/articles API with item.id contained in the arguments. location ~ /mdata/transfer/customer/cod/articles { set $cst_proxy mds/mdata/transfer/customer/cod/articles; if ($args ~ "item.id") { set $cst_proxy localhost/proxyapp; } proxy_pass http://$cst_proxy$is_args$args; proxy_redirect off; }
解决它与下面的代码
upstream proxy-app { server 127.0.0.1:4000; } upstream mds { server 127.0.0.1:1234; } location /mdata/transfer/CUSTOMER/cod/articles { add_header Access-Control-Allow-Origin *; set $cst_proxy mds; if ($args ~ "item.id") { set $cst_proxy proxy-app; } proxy_pass http://$cst_proxy; }