docker - Docker+Nginx 允许来自外部 docker 的上游 gunicorn
问题描述
我有一个正在运行的 docker-compose 容器Nginx
,我想在同一台机器上与Flask
+Gunicorn
在 docker 外部运行时一起使用。当我在Docker 内部运行时,一切都docker-compose.yml
links
通过. 我该如何解决这个错误?Flask
Gunicorn
Docker
我得到的错误是
2020/07/17 03:24:49 [error] 38#38: *1 connect() failed (111: Connection refused) while connecting to upstream, client: *.*.*.*, server: website.com, request: "GET / HTTP/2.0", upstream: "http://127.0.0.1:5000/", host: "website.com"
我正在gunicorn
运行gunicorn --bind 0.0.0.0:5000 wsgi:app
我的 nginxwebsite.conf
upstream hello_server {
#server 127.0.0.1:5000;
server 0.0.0.0:5000;
}
server {
listen 80;
# ...
location ^~ /static/ {
# Path of your static files
root /var/www;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://frontends;
}
}
}
我的docker-compose.yml
version: '3'
services:
web:
image: nginx:latest
ports:
- 80:80/tcp
- 443:443/tcp
解决方案
您可以使用HOST
IP,例如,上游在端口上运行5000
,您只需要指向frontend
HOST IP即可192.168.0.1
。
server {
listen 80;
location ^~ /static/ {
# Path of your static files
root /var/www;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://192.168.0.1:5000;
}
}
或带有上游变量
upstream gunicornapp {
server 192.168.43.84:3000;
}
server {
listen 80;
location ^~ /static/ {
# Path of your static files
root /var/www;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://gunicornapp;
}
}
推荐阅读
- font-awesome - 减少 Font Awsome Hamburger 内 3 个条的高度和宽度
- javascript - Django 简单验证码刷新音频源
- java - 有没有更好的方法来优化使用“ParameterizedPreparedStatementSetter”设置参数的代码?
- javascript - 如何在javascript中绕过或编辑计时器
- python-3.x - 如何以简单明了的方式正确地将数据集拆分为训练验证测试集?
- tensorflow - 如何从 TF 模型中加载 .ckpt、.meta 输出 ffile,输入预测数据并进行预测?
- android - 无法在 Android 应用中生成 FCM 通知
- python - Python - 解析特定 Excel 数据的更快方法
- javascript - 在 React 中提交表单后无法清除输入字段
- javascript - 加载图标并在 php 加载时从“shell_exec”结果中获取值