【问题标题】:Django: How to pass nginx 405 error?Django:如何传递 nginx 405 错误?
【发布时间】:2016-01-19 14:34:32
【问题描述】:

我使用 nginx+gunicorn+supervisor 开发了 Django Web 应用程序。它运行良好。但是当我尝试在 django admin 中添加带有图片的新帖子时,我收到 405 错误。在开发人员服务器上工作正常。 我的 nginx 配置如下:

upstream app_server {

  server unix:/tmp/PMC.sock fail_timeout=0;
}

server {

    listen   80;
    server_name site.ru;

    access_log /home/venv/PMC/logs/nginx-access.log main;
    error_log /home/venv/PMC/logs/nginx-error.log debug;

    location /static/ {
        alias   /home/venv/PMC/static/;
        access_log off;
    }

    location /media/ {
        alias   /home/venv/PMC/media/;
        access_log off;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://app_server;
            break;
        }
    }
}

【问题讨论】:

  • 不确定这是 nginx 问题。你的 nginx 日志中是否有相应的错误?如果是这样的话。如果没有,请从您的应用程序中发布相关代码,尤其是将视图和 URL 路由器发布到您的 POST 请求所访问的 URL 的视图中。

标签: django nginx


【解决方案1】:

错误 405 是“不允许的方法”,这意味着在 Nginx 级别或使用视图装饰器时,您只允许某些方法,即只允许 GETsafe methods,可能不允许 POST。因此,当“获取”页面时没问题,但是当“发布”回来时,您会收到 405 错误。

注意:Nginx 有特殊的命令try_files 而不是if(!-f ...)。最好使用它,因为它提供了更好的性能和更安全的一点。

【讨论】:

    猜你喜欢
    • 2017-05-05
    • 1970-01-01
    • 2016-01-10
    • 2016-12-19
    • 2017-11-20
    • 2012-04-21
    • 2011-12-10
    • 2017-02-24
    • 2015-12-07
    相关资源
    最近更新 更多