问题原文
原帖链接 · StasM · CC BY-SA 3.0
I've got a backend server that, for various reasons, only processes GET requests. This server is located behind nginx proxy (i.e. all access is done to nginx, which proxies it to the backend with proxy_pass). Is it possible to make nginx rewrite POST requests into GET requests, i.e. so that POST /foo with body content type application/x-www-form-urlencoded and body foo=bar would be proxied to GET /foo?foo=bar?
已采纳解答
原帖链接 · Falk Nisius · CC BY-SA 3.0
The small example is working for me with nginx 1.10.x at ubuntu 16.04 with nginx-extras (contains lua). It doesnt respect query arguments from the request, to merge them with the post body.
server {
...
server_name ...;
client_max_body_size 4k; # prevent too long post bodies
location / {
if ($request_method = POST ) {
access_by_lua '
ngx.req.read_body()
local data = ngx.req.get_body_data()
ngx.req.set_uri_args(data)
';
}
proxy_pass http://yourupstreamdestination;
proxy_method GET; # change method
include /etc/nginx/proxy_params.inc; # include some params
}
}
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
