[英]Nginx can't find upstream host in multi-container docker compose setup and also host can't be found on client:3000
[英]Nginx can't find upstream host in multi-container docker compose setup
我正在本地用docker-compose運行一個多docker容器,這些容器是React前端“客戶端”,Nodejs應用“ api”和位於兩個前面的Nginx代理。 我一段時間以來一直在使用docker-compose設置
version: '3'
services:
client:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- /usr/app/node_modules
- ./client:/usr/app
api:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /usr/app/node_modules
- ./server:/usr/app
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '8080:80'
我的Nginx設置如下
upstream client {
server client:3000;
}
upstream api {
server api:5000;
}
server {
listen 80;
server_name _;
location / {
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
proxy_pass http://client;
}
location /api {
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}
最近,當我嘗試啟動容器時,出現以下錯誤:
nginx_1 | 2019/08/08 18:11:12 [emerg] 1#1: host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2
nginx_1 | nginx: [emerg] host not found in upstream "client:3000" in /etc/nginx/conf.d/default.conf:2
知道Nginx為什么找不到上游嗎?
我試圖添加到nginx安裝塊的鏈接,如下所示:
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
links:
- client:client
- api:api
ports:
- '8080:80'
我也嘗試了“ depends_on”而不是鏈接。 添加鏈接后,nginx不再抱怨並以代碼0退出。但是當我訪問localhost:8080時,它會提供301重定向到https:// localhost 。
任何幫助或指示將不勝感激!!!
您應該檢查服務的名稱。 Docker compose將在名為[YOUR_PROJECT_NAME] _api_1的pod中啟動您的服務api。 僅啟動api和client並檢查docker ps
輸出。 您應該查看Pod名稱列表。
在較新的docker_compose語法版本中,您可以使用link_external將[YOUR_PROJECT_NAME] _api_1映射到api。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.