[英]NGINX docker-compose - Host not found in upstream nuxt:3000
我正在嘗試在 EC2 實例上配置已部署的應用程序,當它在 ec2 公共 IP 上啟動時我無法訪問該應用程序。我檢查了安全組並允許所有入站流量到端口只是為了看看我是否可以到達django的主頁或管理頁面。
說我的 ec2 IP 地址是 34.245.202.112 我怎么 map 我的應用程序所以 nginx 服務
前端(nuxt)位於 34.245.202.112
后端(django)位於 34.245.202.112/admin
API(DRF) 位於 34.245.202.112/api
當我嘗試這樣做時,我從 nginx 得到的錯誤是
nginx | 2020-11-14T14:15:35.511973183Z 2020/11/14 14:15:35 [emerg] 1#1: host not found in upstream "nuxt:3000" in /etc/nginx/conf.d/autobets_nginx.conf:9
這是我的配置
docker-compose
版本:“3.4”
services:
db:
restart: always
image: postgres
volumes:
- pgdata:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "5432:5432"
expose:
- 5432
networks:
- random_name
django:
container_name: django
build:
context: ./backend
env_file: .env
environment:
- DEBUG=True
command: >
sh -c "./wait-for-it.sh db:5432 &&
./autobets/manage.py collectstatic --no-input &&
./autobets/manage.py makemigrations &&
./autobets/manage.py migrate --no-input &&
./autobets/manage.py runserver_plus 0.0.0.0:8001
"
- "8001"
volumes:
- ./backend:/app
depends_on:
- db
restart: on-failure
nginx:
image: nginx
container_name: nginx
ports:
- "80:80"
restart: always
depends_on:
- nuxt
- django
volumes:
- ./nginx/conf:/etc/nginx/conf.d
- ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
- ./backend/static:/static
networks:
- random_name
nuxt:
build:
context: ./frontend
environment:
- API_URI=http://django:8001/api
command: sh -c "npm install && npm run dev"
volumes:
- ./frontend:/app
ports:
- "3000:3000"
depends_on:
- django
networks:
- random_name
volumes:
pgdata:
networks:
random_name:
nginx.conf
# the upstream component nginx needs to connect to
upstream django {
ip_hash;
server django:8001;
}
upstream nuxt {
ip_hash;
server nuxt:3000;
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 34.245.202.112; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static/ {
alias /static/;
}
# Finally, send all non-media requests to the Django server.
location / {
proxy_pass django;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
}
}
看看這個最小的例子:
server {
listen 80;
listen 8000; # from you config, remove if unnecessary
server_name 34.245.202.112;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
location / {
# 'the frontend(nuxt) at 34.245.202.112'
# This is the default route. Requests get here when there's no
# better match to go.
proxy_pass http://nuxt:3000;
}
location /api/ {
# This location will trigger when location in URI begins with '/api/'
# e.g. http://yourserver.org/api/v1/hello/world
proxy_pass http://django:8001;
}
location /admin/ {
# exactly as /api/
proxy_pass http://django:8001;
}
location /static/ {
# same as /api/ but local files instead of proxy
alias /static/;
}
}
從示例中可以看出,每個位置都有一個 URI 前綴。 NGINX 將針對傳入的 HTTP 請求中的位置測試所有這些“前綴”,找到最佳匹配。 一旦找到最佳匹配,NGINX 將執行您在塊中編寫的任何內容。 在上面的示例中,所有以/api/
或/django/
go 開頭的請求都發送到django
后端。 以/static/
開頭的請求是從本地文件提供的。 其他一切都轉到nuxt
后端。
我不確定我是否理解你的意圖,可能是因為我缺少你編輯的原始配置,所以你必須從這里開始。 請記住,您不限於位置的 URI 前綴(您可以使用正則表達式或完全匹配)並且您可以使用嵌套位置。 從 NGINX 查看這個很棒的初學者指南,了解更多http://nginx.org/en/docs/beginners_guide.html 。
更新:在這里查看其他答案后,我雖然欠標題中問題的答案,而不僅僅是基本配置。 host not found in upstream
的原因是您沒有指定resolver
指令。 在upstream
塊中使用 DNS 名稱時是必要的,對於 Docker 中的 NGINX,您可以使用: resolver 127.0.0.11 ipv6=off;
. 將它放在http
塊中,即server
塊之外。
“127.0.0.11”是 Docker DNS。它解析服務和容器名稱以及“正常”DNS 記錄(因為這是使用主機的 DNS 配置)。 您不必為服務分配別名或設置container_name
,因為服務名稱本身就是 DNS 記錄。 它解析為該服務的所有容器。 在我發布的基本配置中不需要使用resolver
,因為我沒有使用upstream
塊。
您在 docker-compose 文件的network
塊中缺少alias
部分。 您定義的別名將自動更新容器的/etc/hosts
文件,因此您的 nginx 容器將知道nuxt
主機。
services:
nuxt:
networks:
some-network:
aliases:
- nuxt
更多信息在這里。 ctrl-f 別名: https://docs.docker.com/compose/compose-file/
docker-compose 文件中沒有定義容器名稱“nuxt”,因此無法通過 nginx 容器解析主機名。 嘗試通過將 container_name:nuxt 添加到 docker-compose 文件中的 nuxt 服務來修復 nginx 錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.