Nginx在Docker外工作,但不在里面

问题描述 投票:0回答:2

我正在使用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"]

泊坞窗,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" "-"
django docker nginx gunicorn
2个回答
1
投票

你的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;
    }
}

您的Nginx服务是否在容器内运行?

请在Nginx容器中点击下面的命令,让我知道输出

curl -I localhost:80

0
投票

这很容易理解,你的proxy_pass URL和upstream关键字应该是一样的

对于深入潜入Nginx上游配置,请通过Link

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