【问题标题】:django + nginx on docker bad request (400)django + nginx on docker bad request (400)
【发布时间】:2019-09-26 17:25:40
【问题描述】:

我正在尝试让 django(在 gunicorn 上)和 nginx 与 docker 一起运行。不幸的是,在运行docker-compose up -d --build 后,我不断收到Bad Request (400) 错误。帮助。

我尝试更改目录、目录名称、卷、网络和暴露的端口,但无济于事。我还尝试在我的nginx.conf 文件中添加和删除server_name

settings.py 我有:

DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

这是我的docker-compose.yml 文件:

version: '3.3'

services:
  web:
    build: ./app
    command: gunicorn my_server.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - ./app/:/usr/src/app/
    expose:
      - 8000
    environment:
      - DATABASE=${DATABASE}
      - SECRET_KEY=${SECRET}
      - SQL_DATABASE=${POSTGRES_USER}
      - SQL_ENGINE=django.db.backends.postgresql
      - SQL_HOST=${POSTGRES_HOST}
      - SQL_PASSWORD=${POSTGRES_PASSWORD}
      - SQL_PORT=${POSTGRES_PORT}
      - SQL_USER=${POSTGRES_USER}
      - SU_NAME=${SU_NAME}
      - SU_EMAIL=${SU_EMAIL}
      - SU_PASSWORD=${SU_PASSWORD}
    depends_on:
      - db
    logging:
      driver: "json-file"
      options:
        max-size: "100k"
        max-file: "20"

  db:
    image: postgres:11.2-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_USER=${POSTGRES_USER}
    logging:
      driver: "json-file"
      options:
        max-size: "100k"
        max-file: "20"

  nginx:
    build: ./nginx
    restart: unless-stopped
    volumes:
      - static_volume:/usr/src/app/assets
    ports:
      - 80:80
    depends_on:
      - web
    logging:
      driver: "json-file"
      options:
        max-size: "100k"
        max-file: "20"

volumes:
  static_volume:
  postgres_data:
    external: true

这是我的nginx.conf 文件:

upstream my_server {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass          http://my_server;
        proxy_set_header    Host                $http_host;
        proxy_set_header    Host                $host;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_redirect off;
    }

    location /assets/ {
        alias /usr/src/app/assets;
    }

    location  /robots.txt {
        alias  /var/www/html/robots.txt;
    }
}

在 django 的 Dockerfile 中,我运行 makemigrationsmigrate 并确认已创建数据库表。

我希望去localhost127.0.0.1 并在那里看到我的网站。相反,我收到此错误:Bad Request (400)

当我运行docker ps 时,我得到:

CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                NAMES
2dbc6ff7be78        my_server_nginx        "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp   my_server_nginx_1
a6173e017c93        my_server_web          "/usr/src/app/entryp…"   4 minutes ago       Up 4 minutes        8000/tcp             my_server_web_1
918e7bdae298        postgres:11.2-alpine   "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        5432/tcp             my_server_db_1

当我运行 docker logs my_server_nginx_1 时,我会得到这样的行:

172.18.0.1 - - [08/May/2019:22:25:07 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" "-"
172.18.0.1 - - [08/May/2019:22:25:07 +0000] "GET /favicon.ico HTTP/1.1" 400 37 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" "-"

【问题讨论】:

  • 你需要在docker中查看django日志。你可以这样做:docker exec -ti <container name> bash

标签: django docker nginx docker-compose gunicorn


【解决方案1】:

您可能需要将my_server 添加到允许的主机(与NGINX 配置中的上游相同):

ALLOWED_HOSTS = ['my_server']

【讨论】:

    【解决方案2】:
    Create socket file using gunicorn and configure in our virtual host.
    #############Gunicorn configuration###################
    
     [unit]
     Description=gunicorn daemon
    After=network.target
    
    [Service]
    User=root
    Group=nginx
    WorkingDirectory=/var/www/html/source
     ExecStart=/var/www/html/env_source/bin/gunicorn --workers 3 --bind 
    unix:/var/www/html/source/source.sock application_name.wsgi:application
    [Install]
    WantedBy=multi-user.target
    

    它会创建文件名 source.sock 只需在 nginx 虚拟主机中配置这个 sock 文件

    ###########nginx配置
        server {
        server_name example.com;
        listen 80;
        access_log /var/www/html//source/access.log;
        error_log /var/www/html/source/error.log;
        include /etc/nginx/default.d/*.conf;
    
        location /static/ {
                root /var/www/html/source/run;
        }
    
    
        location / {
                proxy_pass http://unix:/var/www/html/source/source.sock;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $host;
           }
      }
    

    【讨论】:

      【解决方案3】:

      在尝试实施 upinder kumar 的解决方案时,我偶然发现了该解决方案。我会在这里为将来遇到此问题的其他人添加:

      nginx.conf 不能同时拥有两者

      proxy_set_header    Host                $http_host;
      

      proxy_set_header    Host                $host;
      

      删除任何一个都解决了问题。

      【讨论】:

      • 我不知道为什么这是真的。如果有人可以编辑此解决方案并说明这将是惊人的原因:D
      猜你喜欢
      • 2014-03-20
      • 2013-06-05
      • 2021-08-27
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2021-03-23
      相关资源
      最近更新 更多