首页 > 解决方案 > 使用 nginx 重定向特定端点、特定 http 方法

问题描述

我想仅重定向(或 404,等等)一个特定的端点(比如说,/my-path)和特定的方法(例如POST)。
我希望其他一切不受影响;也就是说,POST /my-path将进入 404,但GET /my-path仍然有效,并且POST /my-path/another-path也有效。

如何使用 nginx 实现这一点?

标签: nginx

解决方案


已解决:使用$request_method.

示例:使POST请求接收404但其他方法可以通过。

location /my-location {
    if ($request_method = POST) {
        return 404;  # or whatever special handling
    }
    # Normal case goes here
}

推荐阅读