Docker Flask 应用程序的 Nginx 反向代理的 502 错误网关
502 Bad Gateway with Nginx Reverse Proxy for Docker Flask App
我在为我的 docker 应用程序创建反向代理时遇到问题,它暴露了端口 5000。理想情况下,我希望能够使用子域。
这是我的 docker-compose.yaml 文件
version: "2"
services:
nginx:
image: nginx:1.15-alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
restart: always
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
restart: always
db:
image: mysql:5.7
ports:
- "32000:3306"
environment:
MYSQL_DATABASE: drinks
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
MYSQL_USER: my_user
MYSQL_PASSWORD: password_here
volumes:
- my-datavolume:/var/www/mysql
app:
restart: always
build: ./
links:
- db
ports:
- "5000:5000"
volumes:
my-datavolume:
这是我的 DockerFile
FROM python:3.6-alpine
# create a new user named marty
RUN adduser -D marty
# current directory where the app will be installed
WORKDIR /home/marty
# copy file from the machine to the container file system
COPY app app
COPY requirements.txt requirements.txt
# run a virtual env and install requirements
RUN python3 -m venv venv
CMD source ./venv/bin/activate
RUN apk add --no-cache --virtual .pynacl_deps build-base python3-dev libffi-dev openssl-dev
RUN pip install -r requirements.txt
# The EXPOSE instruction indicates the ports on which a container # # will listen for connections
# Since Flask apps listen to port 5000 by default, we expose it
EXPOSE 5000
# Run app.py when the container launches
CMD python app/app.py
最后,这是我的 nginx 配置
server {
listen 80;
server_name api.example.com;
location / {
return 301 https://$host$request_uri;
}
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
}
server {
listen 443 ssl;
server_name api.example.com;
location / {
proxy_pass http://0.0.0.0:5000;
}
ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
出于某种原因,我在导航到 api.example.com
时一直收到 502 Bad gateway
我也已经为 api 创建了 A 记录。和 www.api.
问题出在 Centos 7 和 Selinux 上。我通过切换到 Debian 并永远发誓不再使用 Centos 来解决这个解决方案。
我在为我的 docker 应用程序创建反向代理时遇到问题,它暴露了端口 5000。理想情况下,我希望能够使用子域。
这是我的 docker-compose.yaml 文件
version: "2"
services:
nginx:
image: nginx:1.15-alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./data/nginx:/etc/nginx/conf.d
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
restart: always
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
restart: always
db:
image: mysql:5.7
ports:
- "32000:3306"
environment:
MYSQL_DATABASE: drinks
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
MYSQL_USER: my_user
MYSQL_PASSWORD: password_here
volumes:
- my-datavolume:/var/www/mysql
app:
restart: always
build: ./
links:
- db
ports:
- "5000:5000"
volumes:
my-datavolume:
这是我的 DockerFile
FROM python:3.6-alpine
# create a new user named marty
RUN adduser -D marty
# current directory where the app will be installed
WORKDIR /home/marty
# copy file from the machine to the container file system
COPY app app
COPY requirements.txt requirements.txt
# run a virtual env and install requirements
RUN python3 -m venv venv
CMD source ./venv/bin/activate
RUN apk add --no-cache --virtual .pynacl_deps build-base python3-dev libffi-dev openssl-dev
RUN pip install -r requirements.txt
# The EXPOSE instruction indicates the ports on which a container # # will listen for connections
# Since Flask apps listen to port 5000 by default, we expose it
EXPOSE 5000
# Run app.py when the container launches
CMD python app/app.py
最后,这是我的 nginx 配置
server {
listen 80;
server_name api.example.com;
location / {
return 301 https://$host$request_uri;
}
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
}
server {
listen 443 ssl;
server_name api.example.com;
location / {
proxy_pass http://0.0.0.0:5000;
}
ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
出于某种原因,我在导航到 api.example.com
时一直收到502 Bad gateway
我也已经为 api 创建了 A 记录。和 www.api.
问题出在 Centos 7 和 Selinux 上。我通过切换到 Debian 并永远发誓不再使用 Centos 来解决这个解决方案。