与Docker一起使用Nginx反向代理
我正在尝试开发一个部署在Nginx上的分布式Angular应用程序,它应该连接到后端服务 docker-compose.yml:与Docker一起使用Nginx反向代理,docker,nginx,docker-compose,Docker,Nginx,Docker Compose,我正在尝试开发一个部署在Nginx上的分布式Angular应用程序,它应该连接到后端服务 docker-compose.yml: version: '3' services: backend_service_1: build: context: ./app dockerfile: Dockerfile ports: - "3001:5000" networks: - my-network frontend:
version: '3'
services:
backend_service_1:
build:
context: ./app
dockerfile: Dockerfile
ports:
- "3001:5000"
networks:
- my-network
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.3
ports:
- "3000:80"
networks:
- my-network
links:
- backend_service_1
networks:
my-network:
nginx.conf:
upstream backend {
server backend_service_1:3001;
}
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html/ki-poc;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /backend {
proxy_pass http://backend/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
我可以在localhost:3000
上访问该应用程序。我还可以使用浏览器从localhost:3001
上的后端服务获得响应。但是,当我尝试使用localhost:3000/backend
上的代理从后端服务获取响应时,我收到以下错误消息:
[error]5#5:*4 connect()在连接到上游时失败(111:连接被拒绝),客户端:172.20.0.1,服务器:localhost,请求:“GET/backend HTTP/1.1”,上游:http://172.20.0.2:3001/,主机:“localhost:3000”
您能告诉我,为什么对链接的后端容器的请求被拒绝吗?您应该使用nignx配置中容器的端口,而不是主机的端口
upstream backend {
server backend_service_1:5000;
}