当我在生产模式下使用Nginx时如何返回Django项目的错误消息

问题描述 投票:1回答:1

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

docker-compose.prod.yml

version: '3.7'

services:
  web:
    build: 
      context: .
      dockerfile: Dockerfile.prod
    command: gunicorn app.wsgi:application --bind 0.0.0.0:8000
    volumes: 
      - static_volume:/home/user/web/staticfiles/
      - media_volume:/home/user/web/media/
    ports:
      - 8000
    env_file:
      - ./.env.prod
    depends_on:
      - db
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db

  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/user/web/staticfiles/
      - media_volume:/home/user/web/media/
    ports:
      - 80:80
    depends_on:
      - web
volumes:
  postgres_data:
  static_volume:
  media_volume:

这是我的nginx.conf

upstream app {
    server web:8000;
}

server {

    listen 80; 

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

     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;
     }
     if ($request_method = GET) {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PATCH, PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range, Authorization';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
     if ($request_method = POST) {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PATCH, PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range, Authorization';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }

     ...
    }

    location /static/ {
        alias /home/user/web/staticfiles/;
    }

    location /media/ {
        alias /home/user/web/media/;
   }
}

The results of my responses

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

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

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

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

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

更多详细信息:

djangorestframeworkDiagram Explanation

发出请求的示例Vue.js代码。

    async get_contact_us_created() {
      const link =
        "http://ec2-18-191-33-108.us-east-2.compute.amazonaws.com/";
      try {
        console.log("before");
        let res = await axios.post(link + "api/contact-us/", {
          name: "Madiyor",
          phone_number: "+998937869412",
          message: "It is awesome"
        });
        console.log("after");
        console.log(res.data);
        console.log(res.status);
      } catch (e) {
        console.log(e);
      }

让我知道我是否想念任何东西。

django nginx response message
1个回答
0
投票

您是代理传递请求,他们没有正确获取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添加到您的应用程序中>>

© www.soinside.com 2019 - 2024. All rights reserved.