我正在尝试在docker上运行django(在gunicorn上)和nginx。不幸的是,我在运行Bad Request (400)
之后仍然遇到docker-compose up -d --build
错误。帮助。
我尝试更改目录,目录名,卷,网络和公开端口无济于事。我还尝试在server_name
文件中添加和删除nginx.conf
。
在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中,我运行makemigrations
和migrate
,并确认已创建数据库表。
我希望访问localhost
或127.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" "-"
答案 0 :(得分:0)
可能您需要将my_server
添加到允许的主机(与NGINX配置中的上游相同):
ALLOWED_HOSTS = ['my_server']
答案 1 :(得分:0)
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;
}
}
答案 2 :(得分:0)
在尝试实施upinder kumar的解决方案时,我偶然发现了该解决方案。我将在此处为将来遇到此问题的其他人添加:
nginx.conf
不能同时拥有
proxy_set_header Host $http_host;
和
proxy_set_header Host $host;
删除其中一个解决了问题。