我正在使用Django,Gunicorn,Nginx和Docker创建一个个人网站。当我执行时:
gunicorn --chdir personal-website --bind :8000 personal_website.wsgi:application
输出为:
[arturocuya@localhost personalwebsite]$ gunicorn --chdir personal-website --bind :8000 personal_website.wsgi:application
[2018-09-09 11:49:02 -0500] [5161] [INFO] Starting gunicorn 19.6.0
[2018-09-09 11:49:02 -0500] [5161] [INFO] Listening at: http://0.0.0.0:8000 (5161)
[2018-09-09 11:49:02 -0500] [5161] [INFO] Using worker: sync
[2018-09-09 11:49:02 -0500] [5165] [INFO] Booting worker with pid: 5165
而且有效(有点,静态文件的配置尚未完成)
问题是,当我用sudo docker-compose up
运行Docker容器时,我得到了502 Bad Gateway
我怀疑问题出在我如何使用端口,但我不太了解应该怎么做。
这是我的文件夹结构
.
├── config
│ └── nginx
│ └── conf.d
│ └── local.conf
├── docker-compose.yml
├── Dockerfile
└── personal-website
└── manage.py
Dockerfile
# Start from an official image
FROM python:3.6
# The following is an arbitrary location choice
RUN mkdir -p /opt/services/personalwebsite/src
WORKDIR /opt/services/personalwebsite/src
# Copy the project code
COPY . /opt/services/personalwebsite/src
# Install dependencies
RUN pip install django gunicorn Pillow
# Expose Port 8000
EXPOSE 8000
# Define the default command to run when starting the container
CMD ["gunicorn", "--chdir", "personal-website", "--bind", ":8000", "personal_website.wsgi:application"]
docker-compose.yml
version: '3'
services:
personalwebsite:
build: .
volumes:
- .:/opt/services/personalwebsite/src
networks:
- nginx_network
nginx:
image: nginx:1.13
ports:
- 8000:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
depends_on:
- personalwebsite
networks:
- nginx_network
networks:
nginx_network:
driver: bridge
config / nginx / conf.d / local.conf
# first we declare our upstream server, which is our Gunicorn application
upstream personalwebsite_server {
# docker will automatically resolve this to the correct address
# because we use the same name as the service: "personalwebsite"
server personalwebsite:8000;
}
# now we declare our main server
server {
listen 80;
server_name localhost;
location / {
# everything is passed to Gunicorn
proxy_pass http://personalwebsite;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
我还确保在Django项目的settings.py
内修改ALLOWED_HOSTS
ALLOWED_HOSTS = ['127.0.0.1', '149.248.5.164', '0.0.0.0']
修改1:
正如评论中有人建议的那样,我使用sudo docker-compose exec nginx bash
访问了Nginx容器,然后执行了curl personalwebsite:8000
。我遇到了DISALLOWED HOST
错误,所以我在personalwebsite
的允许主机中添加了settings.py
,然后尝试再次卷曲,输出的是页面的HTML,很好。
这似乎在容器内起到了作用,因为输出是我页面的HTML。但是后来我做了sudo docker-compose up
,又得到了502 Bad Gateway
。确切的输出是:
[arturocuya@localhost personalwebsite]$ sudo docker-compose up
[sudo] password for arturocuya:
Starting personalwebsite_personalwebsite_1 ... done
Starting personalwebsite_nginx_1 ... done
Attaching to personalwebsite_personalwebsite_1, personalwebsite_nginx_1
personalwebsite_1 | [2018-09-10 02:07:12 +0000] [1] [INFO] Starting gunicorn 19.9.0
personalwebsite_1 | [2018-09-10 02:07:12 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
personalwebsite_1 | [2018-09-10 02:07:12 +0000] [1] [INFO] Using worker: sync
personalwebsite_1 | [2018-09-10 02:07:12 +0000] [10] [INFO] Booting worker with pid: 10
nginx_1 | 172.26.0.1 - - [10/Sep/2018:02:07:17 +0000] "GET / HTTP/1.1" 502 576 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36" "-"
nginx_1 | 2018/09/10 02:07:17 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.26.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://172.26.0.2:80/", host: "0.0.0.0:8000"
nginx_1 | 2018/09/10 02:07:18 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.26.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.26.0.2:80/favicon.ico", host: "0.0.0.0:8000", referrer: "http://0.0.0.0:8000/"
nginx_1 | 172.26.0.1 - - [10/Sep/2018:02:07:18 +0000] "GET /favicon.ico HTTP/1.1" 502 576 "http://0.0.0.0:8000/" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36" "-"
答案 0 :(得分:1)
您的Nginx配置应为
$data = Modul::select('name as title', 'start_date as start', 'end_date as end'->get();
您的Nginx服务是否正在容器内运行?
请在Nginx容器中单击以下命令,让我知道输出
upstream personalwebsite {
server personalwebsite:8000;
}
# now we declare our main server
server {
listen 80;
server_name localhost;
location / {
# everything is passed to Gunicorn
proxy_pass http://personalwebsite;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
答案 1 :(得分:0)
这很容易理解,您的proxy_pass
网址和upstream
关键字应该相同
要深入研究Nginx上游配置,请通过Link