Reputation: 465
I can'f find my mistake myself, could anyone help me please.
So, I want to run Nginx with https and uWSGI+Flask in different containers for many reasons. I did it, but uwsgi
container doesn't get request from nginx
container.
My confing:
N.B.
- IP address of my server has a 11.11.11.1 for only example propose.
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" "-"
My question is why NGINX can't connect to the UWSGI? Where I did mistake or what I didn't do?
Thank you in advance!
Upvotes: 2
Views: 2356
Reputation: 4078
The nginx container should link to the app container. In the nginx upstream, the uwsgi container should be referenced. This will allow nginx to reference the uwsgi container to proxy the requests.
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;
}
}
docker-compose.yml
version: '2'
services:
app:
build: .
ports:
- "4000:4000"
nginx:
image: nginx_ssl:5.0
ports:
- "443:443"
links:
- app
Upvotes: 3