Dockerized Nginx 反向代理和 Flask 应用程序,如何配置?
Dockerized Nginx reverse proxy and Flask app, how to config?
我在 Nginx 反向代理后面部署一个简单的 Flask 应用程序时遇到问题。该应用程序位于 https://docs.docker.com/compose/gettingstarted/
我正在尝试让该应用程序显示在 subdomain.example.com/flask
,但在当前配置下它不起作用。我缺少什么?
现在我的 docker-compose.yml
看起来像这样:
version: '3'
services:
nginx:
container_name: nginx
build:
context: ./nginx
volumes:
- ./config/:/etc/nginx/
- ./ssl-cert/:/etc/ssl/private/
depends_on:
- web
ports:
- 80:80
web:
build:
context: ./composetest
expose:
- 5000
redis:
image: "redis:alpine"
这是我的 Nginx default.conf
:
server {
listen 80;
server_name subdomain.example.com;
location /flask{
proxy_pass http://web:5000;
}
}
当我这样做时 docker-compose up --build
我没有从 Nginx 容器或其他容器中得到任何错误(显然忽略了与生产就绪相关的错误)。
这是我在 运行 上面提到的命令时看到的:
redis_1 | 1:C 16 May 2021 14:08:42.327 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 16 May 2021 14:08:42.335 # Redis version=6.2.3, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 16 May 2021 14:08:42.335 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 16 May 2021 14:08:42.336 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 16 May 2021 14:08:42.337 * Running mode=standalone, port=6379.
redis_1 | 1:M 16 May 2021 14:08:42.337 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 16 May 2021 14:08:42.337 # Server initialized
redis_1 | 1:M 16 May 2021 14:08:42.337 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 16 May 2021 14:08:42.338 * Ready to accept connections
web_1 | * Serving Flask app 'app.py' (lazy loading)
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production WSGI server instead.
web_1 | * Debug mode: off
web_1 | * Running on all addresses.
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | * Running on http://192.168.112.4:5000/ (Press CTRL+C to quit)
您需要为 nginx 服务设置 ports
指令。即使默认情况下端口由图像公开,它们也不会自动绑定到主机,如 ports
.
services:
nginx:
container_name: nginx
build:
context: ./nginx
volumes:
- ./config/:/etc/nginx/
- ./ssl-cert/:/etc/ssl/private/
depends_on:
- web
ports:
- "80:80"
之后,您应该可以在 http://localhost
访问 nginx
我认为一切都很好,你从你的 web 容器中暴露端口 5000 并且你在 nginx
容器的端口 80 上列出
您应该可以将端口 80 连接到您的本地计算机。
只需在 depends_on 下添加:重建测试,它应该可以正常工作
ports:
- "80:80"
添加子域:subdomain.example.com
也在您的本地计算机上的 /etc/hosts
下,
如果您使用 ubuntu
debian
机器。通过使用任何文本编辑器添加主机 vim
nano
...
127.0.0.1subdomain.example.com
从您的浏览器保存并提供服务。
我在 Nginx 反向代理后面部署一个简单的 Flask 应用程序时遇到问题。该应用程序位于 https://docs.docker.com/compose/gettingstarted/
我正在尝试让该应用程序显示在 subdomain.example.com/flask
,但在当前配置下它不起作用。我缺少什么?
现在我的 docker-compose.yml
看起来像这样:
version: '3'
services:
nginx:
container_name: nginx
build:
context: ./nginx
volumes:
- ./config/:/etc/nginx/
- ./ssl-cert/:/etc/ssl/private/
depends_on:
- web
ports:
- 80:80
web:
build:
context: ./composetest
expose:
- 5000
redis:
image: "redis:alpine"
这是我的 Nginx default.conf
:
server {
listen 80;
server_name subdomain.example.com;
location /flask{
proxy_pass http://web:5000;
}
}
当我这样做时 docker-compose up --build
我没有从 Nginx 容器或其他容器中得到任何错误(显然忽略了与生产就绪相关的错误)。
这是我在 运行 上面提到的命令时看到的:
redis_1 | 1:C 16 May 2021 14:08:42.327 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 16 May 2021 14:08:42.335 # Redis version=6.2.3, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 16 May 2021 14:08:42.335 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 16 May 2021 14:08:42.336 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 16 May 2021 14:08:42.337 * Running mode=standalone, port=6379.
redis_1 | 1:M 16 May 2021 14:08:42.337 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 16 May 2021 14:08:42.337 # Server initialized
redis_1 | 1:M 16 May 2021 14:08:42.337 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 16 May 2021 14:08:42.338 * Ready to accept connections
web_1 | * Serving Flask app 'app.py' (lazy loading)
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production WSGI server instead.
web_1 | * Debug mode: off
web_1 | * Running on all addresses.
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | * Running on http://192.168.112.4:5000/ (Press CTRL+C to quit)
您需要为 nginx 服务设置 ports
指令。即使默认情况下端口由图像公开,它们也不会自动绑定到主机,如 ports
.
services:
nginx:
container_name: nginx
build:
context: ./nginx
volumes:
- ./config/:/etc/nginx/
- ./ssl-cert/:/etc/ssl/private/
depends_on:
- web
ports:
- "80:80"
之后,您应该可以在 http://localhost
我认为一切都很好,你从你的 web 容器中暴露端口 5000 并且你在 nginx
容器的端口 80 上列出
您应该可以将端口 80 连接到您的本地计算机。
只需在 depends_on 下添加:重建测试,它应该可以正常工作
ports:
- "80:80"
添加子域:subdomain.example.com
也在您的本地计算机上的 /etc/hosts
下,
如果您使用 ubuntu
debian
机器。通过使用任何文本编辑器添加主机 vim
nano
...
127.0.0.1subdomain.example.com
从您的浏览器保存并提供服务。