如何删除 post 请求 nginx 上的尾部斜杠

how to remove trailing slash on post request nginx

我正在尝试删除 http post 方法上的尾部斜线,当我尝试使用 rewrite ^/(.*)/$ / permanent; 重写 URL 时,它对我不起作用

如果 Http POST 以这些格式出现,上游应该以这种格式接收 /x/y

这是nginx的配置

  upstream backend {
            server 127.0.0.1:8778;
            # Number of idle keepalive connections per worker process.
            keepalive 35;
    }


    location /x/y  {        
                    limit_except POST {
                            deny all;
                    }
                    proxy_pass  http://backend;
                    proxy_buffering on;
                    include proxy.conf;
            }

这里的问题是当上游看到 URI 是这种格式时 /x/y/ 它拒绝了请求,正确的重写规则应该是什么,以便如果 http post 进来/x/y/x/y/ 这样的格式,上游应该总是看到 /x/y

permanent 将导致 rewrite 生成带有 301 响应的重定向。您需要的是在向上游发送之前对 URI 进行内部调整:

location /x/y {
    rewrite ^/(.*)/$ / break;
    ...
}

有关更多信息,请参阅 this document