基于request_method在nginx中设置proxy_pass
Set proxy_pass in nginx based on request_method
我有两个应用程序和一个 nginx 服务器。我想将 nginx 上的所有 GET 请求代理到 http://127.0.0.1:9101/ and proxy all other request methods to http://10.41.115.241:8000/
上的一个应用 运行
我尝试了几个选项,但 none 有效
我试过使用 limit_exempt
location /api/v1/executions {
error_page 502 = @apiError;
rewrite ^/api/(.*) / break;
proxy_pass http://127.0.0.1:9101/;
limit_except PUT POST DELETE {
proxy_pass http://10.41.115.241:8000/;
}
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Connection '';
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
#proxy_set_header Host $host;
}
我也试过if条件
location /api/v1/executions {
error_page 502 = @apiError;
rewrite ^/api/(.*) / break;
proxy_pass http://10.41.115.241:8000/;
if ($request_method = GET) {
proxy_pass http://127.0.0.1:9101/;
}
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Connection '';
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
#proxy_set_header Host $host;
}
但在这两种方式中我都遇到了这个错误
"proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /path/to/config
对于 NGINX 中的条件逻辑,始终尝试使用 map
而不是 if
。除非 map
绝对不可能。在您的特定情况下,这很容易。
创建一个变量 $backend
,它将根据请求方法保存您想要的 proxy_pass
值:
http {
map $request_method $backend {
default http://10.41.115.241:8000/;
GET http://127.0.0.1:9101/;
}
...
}
然后在你的配置中使用它:
location /api/v1/executions {
error_page 502 = @apiError;
rewrite ^/api/(.*) / break;
proxy_pass $backend;
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Connection '';
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
#proxy_set_header Host $host;
}
我有两个应用程序和一个 nginx 服务器。我想将 nginx 上的所有 GET 请求代理到 http://127.0.0.1:9101/ and proxy all other request methods to http://10.41.115.241:8000/
上的一个应用 运行我尝试了几个选项,但 none 有效 我试过使用 limit_exempt
location /api/v1/executions {
error_page 502 = @apiError;
rewrite ^/api/(.*) / break;
proxy_pass http://127.0.0.1:9101/;
limit_except PUT POST DELETE {
proxy_pass http://10.41.115.241:8000/;
}
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Connection '';
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
#proxy_set_header Host $host;
}
我也试过if条件
location /api/v1/executions {
error_page 502 = @apiError;
rewrite ^/api/(.*) / break;
proxy_pass http://10.41.115.241:8000/;
if ($request_method = GET) {
proxy_pass http://127.0.0.1:9101/;
}
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Connection '';
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
#proxy_set_header Host $host;
}
但在这两种方式中我都遇到了这个错误
"proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /path/to/config
对于 NGINX 中的条件逻辑,始终尝试使用 map
而不是 if
。除非 map
绝对不可能。在您的特定情况下,这很容易。
创建一个变量 $backend
,它将根据请求方法保存您想要的 proxy_pass
值:
http {
map $request_method $backend {
default http://10.41.115.241:8000/;
GET http://127.0.0.1:9101/;
}
...
}
然后在你的配置中使用它:
location /api/v1/executions {
error_page 502 = @apiError;
rewrite ^/api/(.*) / break;
proxy_pass $backend;
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Connection '';
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;
#proxy_set_header Host $host;
}