我正在尝试将容器中的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连接()失败(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 ,像Gecko)Chrome / 56.0.2924.87 Safari / 537.36“” - “”
现在马上我认为nginx无法访问我的app容器,但是,在“web”容器中运行“curl app:3000”会返回正确的响应。转发端口时,我也可以直接在端口3000上访问应用程序。所以我觉得问题在于我的nginx.conf如何尝试访问该资源。我一直在撞墙上撞了一会儿。有什么想法吗?
答案 0 :(得分:0)
重写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