【问题标题】:How to return Django project's error messages when I use nginx in production mode在生产模式下使用 nginx 时如何返回 Django 项目的错误消息
【发布时间】:2020-07-27 04:36:15
【问题描述】:

我开发了 Django 项目并将其部署到 Amazon 的免费层 EC2 服务。一切都很好,除了没有返回的错误消息。我正在生产模式下使用该项目。

上图说明[控制台日志]:

  1. 成功的请求和响应 - 它是针对现有 url 进行的

  2. 故意向不存在的 url 发出第二次请求,但未收到任何响应。

我想得到至少 404 响应,我遇到的问题是服务器没有任何响应。当我在服务器上运行它时,我看到它正在将结果记录到服务器。

问题: 出现问题时如何返回 Django 生成的响应。 额外信息:这些错误消息和响应是在 djangorestframework's 内置模板中生成的。

额外细节:

如果我遗漏了什么,请告诉我。

【问题讨论】:

  • 请更好/完整地解释您的设置,因为您在下面提到您使用 docker。 nnginx 在哪里运行?
  • run 命令触发:gunicorn app.wsgi:application --bind 0.0.0.0:8000 用于 NGINX 中的 proxy_pass。

标签: django nginx response message


【解决方案1】:

大脑在疲倦时会做一些非常有趣的事情。感谢@iklinac。他是对的,更好的是我会正确使用django-cors-headers。它已经安装并在 heroku 上运行,当我搬到亚马逊 aws 时,我认为任何事情都与 NGINX 相关。

做笔记。

  1. pip install django-cors-headers

  2. 确保它在您安装的应用程序中。

INSTALLED_APPS = [

    ...
    'corsheaders',
    ...
 ]
  1. 您还需要添加一个中间件类来监听响应:# 我错过了
MIDDLEWARE = [  # Or MIDDLEWARE_CLASSES on Django < 1.10
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]
  1. 授权进行跨站点 HTTP 请求的源列表
CORS_ORIGIN_WHITELIST = [
    "https://example.com",
    "https://sub.example.com",
    "http://localhost:8080",
    "http://127.0.0.1:9000"
]

然后,您可以根据需要调整和使用其他一些东西。

最终我已将我的 nginx.conf 更改为以下

upstream hello_django {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass http://hello_django;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /staticfiles/ {
        alias /home/app/web/staticfiles/;
    }

    location /mediafiles/ {
        alias /home/app/web/mediafiles/;
    }

}

编码愉快。) 归功于 testdriven.iodjango-cors-headers

【讨论】:

    【解决方案2】:

    您是代理传递请求,它们没有正确获取 add_header,您应该在添加标头后代理传递

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PATCH, PUT, DELETE';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
    
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PATCH, PUT, DELETE';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range, Authorization';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
     }
    
    
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    

    其他方法是将django-cors-headers 添加到您的应用程序中

    【讨论】:

    • 我已经尝试了你的建议,一切都和以前一样。并且不返回错误消息和响应
    • 是的,当我部署在 heroku 上时它确实可以工作,但我猜现在它不工作了。我正在使用 Docker 进行部署,我应该共享我的文件吗?
    • 我们不应该返回任何类型的响应,例如 location / {...} 中的变量
    • 如果你使用 django-cors-headers,你不需要 nginix conf 中的任何头文件
    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 2018-04-21
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2020-06-14
    • 1970-01-01
    相关资源
    最近更新 更多