无法将UWSGI容器与NGINX容器[Docker]连接

时间:2018-04-12 15:01:25

标签: docker nginx uwsgi

我自己也找不到自己的错误,任何人都可以帮助我。 因此,我想在不同的容器中使用https和uWSGI + Flask运行Nginx有很多原因。我做到了,但是uwsgi容器没有收到来自nginx容器的请求。

我的困惑:

N.B. - 我的服务器的IP地址只有11.11.11.1,仅用于建议。

Nginx Dockerfile:

FROM nginx:alpine

RUN apk add --no-cache openssl
RUN mkdir -p /etc/nginx/ssl/ \
        && cd /etc/nginx/ssl/ \
        && openssl req -newkey rsa:2048 -sha256 -nodes \
                -keyout cert.key \
                -x509 \
                -days 9999 \
                -out cert.pem \
                -subj "/C=US/ST=New York/L=Brooklyn/O=Me/CN=11.11.11.1"

ADD nginx.conf /etc/nginx/nginx.conf
ADD nginx.custom.conf /etc/nginx/conf.d/nginx.custom.conf

EXPOSE 443
EXPOSE 80

uwsgi Dockerfile

FROM alpine:3.7

ADD requirements.txt requirements.txt
# Install uWSGI
RUN apk add --no-cache uwsgi-python3 python3 \
        && export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.6/site-packages:/usr/lib/python3.6/site-packages \
        && pip3 install --no-cache-dir -r requirements.txt

EXPOSE 4000

ADD ./app /app
WORKDIR /app

CMD [ "uwsgi", "--thunder-lock", "--ini", "/app/uwsgi.ini"]

uwsgi.ini:

[uwsgi]
app_base = /app
chmod-socket = 777
socket = 0.0.0.0:4000
chdir = %(app_base)
wsgi-file = uwsgi.py
callable = app
master = true
buffer-size = 32768
processes = 4
max-requests = 1000
harakiri = 20
vauum = true
reload-on-as = 512
die-on-term = true
plugins = python3

uwsgi.py:

from bot.controllers import app

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=4000, debug=True, use_reloader=False)

nginx.conf:

upstream flaskapp {
   server 0.0.0.0:4000;
}

server {
    listen 80;
        listen 443 ssl;
        server_name 11.11.11.1;

        ssl on;
        ssl_protocols       SSLv3 TLSv1;
    ssl_certificate     /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/cert.key;

    location / {
        include /etc/nginx/uwsgi_params;
        uwsgi_pass flaskapp;
    }
}

docker-compose.yml:

version: '0.1'
services:
  app:
    build: .
    ports:
    - "4000:4000"
    links:
    - nginx
  nginx:
    image: nginx_ssl:5.0
    ports:
    - "443:443"

log:

app_1    | uwsgi socket 0 bound to TCP address 0.0.0.0:4000 fd 3
app_1    | uWSGI running as root, you can use --uid/--gid/--chroot options
app_1    | *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
app_1    | Python version: 3.6.3 (default, Nov 21 2017, 14:55:19)  [GCC 6.4.0]
app_1    | *** Python threads support is disabled. You can enable it with --enable-threads ***
app_1    | Python main interpreter initialized at 0x55e6e3cc5f40
app_1    | uWSGI running as root, you can use --uid/--gid/--chroot options
app_1    | *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
app_1    | your server socket listen backlog is limited to 100 connections
app_1    | your mercy for graceful operations on workers is 60 seconds
app_1    | mapped 507960 bytes (496 KB) for 4 cores
app_1    | *** Operational MODE: preforking ***
app_1    | Set the MIATA_SERVER_IP environment variable
app_1    | Set the MIATA_PUBLIC_CERT environment variable
app_1    | WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55e6e3cc5f40 pid: 1 (default app)
app_1    | uWSGI running as root, you can use --uid/--gid/--chroot options
app_1    | *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
app_1    | *** uWSGI is running in multiple interpreter mode ***
app_1    | spawned uWSGI master process (pid: 1)
app_1    | spawned uWSGI worker 1 (pid: 8, cores: 1)
app_1    | spawned uWSGI worker 2 (pid: 9, cores: 1)
app_1    | spawned uWSGI worker 3 (pid: 10, cores: 1)
app_1    | spawned uWSGI worker 4 (pid: 11, cores: 1)
nginx_1  | 2018/04/12 14:15:33 [error] 7#7: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: 11.11.11.1, request: "GET / HTTP/1.1", upstream: "uwsgi://0.0.0.0:4000", host: "77.37.214.6"
nginx_1  | 172.17.0.1 - - [12/Apr/2018:14:15:33 +0000] "GET / HTTP/1.1" 502 174 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30" "-"

我的问题是为什么NGINX无法连接到UWSGI?我错在哪里或者我没做错?

提前谢谢!

1 个答案:

答案 0 :(得分:2)

nginx容器应链接到 app 容器。在nginx上游,应该引用uwsgi容器。这将允许nginx引用uwsgi容器来代理请求。

nginx.conf

upstream flaskapp {
   server app:4000;
}

server {
    listen 80;
        listen 443 ssl;
        server_name 11.11.11.1;

        ssl on;
        ssl_protocols       SSLv3 TLSv1;
    ssl_certificate     /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/cert.key;

    location / {
        include /etc/nginx/uwsgi_params;
        uwsgi_pass flaskapp;
    }
}

搬运工-compose.yml

version: '2'
services:
  app:
    build: .
    ports:
    - "4000:4000"
  nginx:
    image: nginx_ssl:5.0
    ports:
    - "443:443"
    links:
    - app