![](/img/trans.png)
[英]Nginx and Unicorn - (Connection refused) while connecting to upstream
[英]Docker nginx reverse proxy returns 502 bad gateway “connection refused while connecting to upstream”
我正在尝试将一个容器中的nginx反向代理设置为运行我的应用程序的另一个容器。 这是我的nginx.conf:
daemon off;
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
upstream appserver {
server app:3000;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://appserver/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}
}
我的docker-compose.yml看起来像这样:
version: '3'
services:
db:
image: postgres
redis:
image: redis
web:
build: ./web
image: web
ports:
- "8080:80"
app:
build: ./app
image: app
command: puma
volumes:
- ./app:/app
expose:
- "3000"
ports:
- "3000:3000"
depends_on:
- db
- redis
- web
反向代理的Dockerfile仅复制配置文件并启动nginx服务。 这是我的问题:
在主机浏览器上访问localhost:8080时,nginx返回502 Bad Gateway。 日志显示“ web_1 | 2017/02/26 22:55:15 [错误] 12#12:* 1连接到上游时,connect()失败(111:连接被拒绝),客户端:172.25.0.1,服务器:localhost,请求:“ GET / HTTP / 1.1”,上游:“ http://127.0.53.53:3000/ ”,主机:“ localhost:8080” web_1 | 172.25.0.1--[26 / Feb / 2017:22:55: 15 +0000]“ GET / HTTP / 1.1” 502 576“-”“ Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,like Gecko)Chrome / 56.0.2924.87 Safari / 537.36”“-” “
现在,我立即想到nginx无法访问我的应用程序容器,但是,在“网络”容器中运行“ curl app:3000”会返回正确的响应。 转发端口后,我也可以直接在端口3000上访问该应用程序。 所以我觉得问题出在我的nginx.conf上,它是如何尝试访问该资源的。 一段时间以来,我一直在撞墙。 有任何想法吗?
重写docker-compose.yml可以通过确保在nginx反向代理之前启动应用程序来解决此问题。
version: '3'
services:
db:
image: postgres
redis:
image: redis
web:
build: ./web
image: web
ports:
- "8080:80"
depends_on:
- app
app:
build: ./app
image: app
command: puma
volumes:
- ./app:/app
expose:
- "3000"
ports:
- "3000:3000"
depends_on:
- db
- redis
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.