我有几个上游,并且有兴趣proxy_pass
根据查询参数和主体 JSON 中的字段的组合来确定哪个请求。
我的问题本质上是:
server {
listen 80;
listen 443 ssl;
location / {
if ( $arg_operation = "someValue" ) { proxy_pass http://upstream1; }
# Here's where I'm not so sure; I want to do something like this
if ( $body_value ~ "/shibboleth: true/" ) { proxy_pass http://upstream2; }
}
}
或者,我听说您可以使用 lua/js 来做一些更自定义的事情,这可能适用于这里?
答案1
工作示例也许可以帮助到这里任何人: https://github.com/ericminio/learning-nginx/tree/master/payload_filter
map $request_body $matching {
default 'not_matching';
include /etc/nginx/conf.d/matching;
}
server {
default_type text/plain;
location /ping {
return 200 'pong';
}
location / {
proxy_set_header 'X-Matching' $matching;
proxy_pass http://localhost/routing;
}
location /routing {
try_files /unknown-file-to-trigger-redirect @_$http_x_matching;
}
location @_matching {
proxy_pass http://host.docker.internal:5015;
}
location @_not_matching {
proxy_pass http://host.docker.internal:5066;
}
}