nginx为PUT或DELETE返回405(Method Not Allowed)

时间:2014-03-06 17:01:48

标签: nginx

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

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

这个问题大致相同:How do I allow a PUT file request on Nginx server?但答案并没有解决我的问题,这让我觉得这与我在设置中使用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/;
        }
}

我试图使用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"

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

1 个答案:

答案 0 :(得分:0)

您可以在配置文件中添加此句子

dav_methods PUT DELETE MKCOL COPY MOVE;

具体的详细参考资料nginx文件http://nginx.org/en/docs/http/ngx_http_dav_module.html