nginx 为 PUT 或 DELETE 返回 405(不允许的方法)

don*_*ume 5 nginx

我使用 nginx 来提供静态页面,但将请求传递给一个 API 到 Tornado 应用程序,我想处理 GET、POST、PUT 和 DELETE 请求。

GET 和 POST 请求没问题,但 PUT 和 DELETE 请求被拒绝,并显示“405: Method Not Allowed”

这个问题问的差不多:如何在 Nginx 服务器上允许 PUT 文件请求?但答案并没有解决我的问题,这让我认为这与我在设置中使用 proxy_pass 相关。

这是我的 nginx 服务器配置:

upstream TornadoAPI {
        server 127.0.0.1:8000;
}

server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location /<<static url>>/ {
                root /var/www;
                index index.html;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /<<API url>>/ {
                dav_methods PUT DELETE;
                dav_access all:r;
                proxy_pass http://TornadoAPI/api/;
        }
}
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用 HttpDavModule 指令(尽管我认为我的应用程序不符合 HttpDav 的条件 - 我无意让用户编写文件)但没有走运。我已经通过检查 nginx -V 确认了该模块的存在。

这是 nginx access.log 的示例输出:

<<IP address>> - - [06/Mar/2014:16:29:57 +0000] "PUT /<<API url>>/<<resource>> HTTP/1.1" 405 87 "<<ngix server root url>>" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"
Run Code Online (Sandbox Code Playgroud)

有人可以建议我还能做些什么来接受 PUT 和 DELETE 方法吗?

小智 0

你可以在你的配置文件中添加这句话

dav_methods PUT DELETE MKCOL COPY MOVE;

具体详细参考nginx文档http://nginx.org/en/docs/http/ngx_http_dav_module.html