大网数据广告活动
服务器运维问答

Rewrite request from POST to GET with nginx

· 52CE 我爱测

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 m…

问题原文

原帖链接 · 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
    }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-09-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。