語境
我正在嘗試建立一個本地開發環境,但我正在努力讓它作業。我希望 docker 和 docker compose 將 nginx 作為反向代理和 postgresql 資料庫運行
專案結構
my-app/
├─ server/ (fastify node server)
├─ client/ (sveltekit dev server)
├─ nginx/
│ ├─ Dockerfile
│ ├─ default.conf
├─ docker-compose.yml
我希望唯一的先決條件是 Node、git 和 docker。我要能夠克隆專案,啟動docker compose,然后分別啟動兩個專案
先前的研究
我已經通讀了 nginx 初學者指南和幾篇 nginx 檔案,但我發現他們的檔案相當難以理解。我還發現了一些指南,它們讓我的客戶端和服務器在 docker 容器中運行,并在 docker compose 中設定為單獨的服務。這不是最終目標,但不幸的是,我也沒有讓它們發揮作用。
這個問題似乎很接近,我發現它很有幫助。但它仍然無法正常作業。
首先,我只是想讓一切都在 http://localhost:4000 上作業,以消除一些復雜性
這是我迄今為止最好的嘗試:
##########################
# docker-compose.yml
##########################
version: '3.9'
services:
reverse-proxy:
build: ./nginx
ports:
- 8080:8080
##########################
# nginx/Dockerfile
##########################
FROM nginx:1.21.3
COPY ./default.conf /etc/nginx/conf.d/default.conf/
##########################
# nginx/default.conf
##########################
server {
listen 8080;
location / {
proxy_pass http://host.docker.internal:8080;
}
location /api/ {
proxy_pass http://host.docker.internal:3000;
}
}
通過此設定,我可以點擊 http://localhost:3000/api/ 并從服務器獲取我的 json 回應,但是當我嘗試點擊 http://localhost:8080/api/ 時,我得到了標準的 nginx 502回應和 nginx 沒有成功到達我的節點服務器。nginx 日志給了我這個錯誤:
2021/11/16 12:38:39 [error] 34#34: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET /api/ HTTP/1.1", upstream: "http://192.168.65.2:3000/api/", host: "localhost:8080"
我不知道上面的 IP 地址從哪里來,但是如果我在本地點擊http://192.168.65.2:3000/api/,那么瀏覽器就會不停地旋轉。
最終目標
目前,我正在尋找一個配置正確docker-compose.yml且相對簡單的 nginx 檔案,該檔案被映射到默認值。HTTPS 和自定義 URL 確實是后來的問題。現在,我只想能夠在 docker 容器中訪問我的 nginx 反向代理,并正確路由到我的兩個本地運行的節點服務器(一個是 API 服務器,一個是 sveltekit 開發服務器 - 都不在 docker 容器內)。
預先感謝您的任何幫助!
編輯(在 Robert-Jan Kuyper 建議的更改之后)
我現在基本上是這樣運行的:
##########################
# docker-compose.yml
##########################
version: '3.8'
services:
nginx:
image: nginx:1.19.4
depends_on:
- api
volumes:
- ./nginx/default.local.conf:/etc/nginx/conf.d/default.conf
ports:
- '8080:8080'
api:
build: ./api
hostname: api
command: node src/app.js
volumes:
- ./server/src:/app/src
##########################
# nginx/default.conf
##########################
upstream api_server {
server api:3000;
}
server {
listen 8080;
location /api/ {
proxy_pass http://api_server;
}
}
##########################
# api/Dockerfile
##########################
FROM node:16.9.0
# docker workdir
WORKDIR /home/usr/app
# copy files
COPY . .
# build the app
RUN npm ci
# expose at port 3000
EXPOSE 3000
CMD ["node", "src/app.js"]
不幸的是,它仍然失敗了:
[error] 29#29: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /api/ HTTP/1.1", upstream: "http://172.18.0.2:3000/api/", host: "localhost:8080"
uj5u.com熱心網友回復:
您必須牢記以下幾點:
- Docker compose 默認使用服務名稱作為容器間網路的主機名
- Nginx 需要先知道上游
- 你不需要在 Dockerfile 中使用 Nginx,而是直接使用它
docker-compose.yml。并將您的組態檔掛載到容器中。 - 您需要對前端和后端進行 dockerize 以將它們連接到 nginx
- 始終在您的 docker 容器上
0.0.0.0而不是127.0.0.1或localhost在其內部公開,請參閱此答案。
我使用 nginx、docker 和 docker-compose 的示例存盤庫:https : //gitlab.com/datails/api。
示例default.conf:
upstream api_server {
server api:3000;
}
upstream frontend {
server frontend:3000;
}
server {
listen 80;
location /api {
proxy_pass http://api_server;
}
location / {
proxy_pass http://frontend/;
}
}
例子 docker-compose.yml
version: '3.8'
services:
nginx:
image: nginx:1.19.4
depends_on:
- server
- frontend
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- '8080:80'
然后確保您的前端 dockerized 并frontend在您的docker -compose 中作為服務呼叫,例如:
version: '3.8'
services:
nginx:
image: nginx:1.19.4
depends_on:
- server
- frontend
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
ports:
- '8080:80'
frontend:
build: ./frontend
command: npm run start
volumes:
- ./frontend/src:/home/usr/app/src
api:
build: ./api
command: npm run start
volumes:
- ./api/src:/home/usr/app/src
請注意,您不需要埠,因為您使用容器間通信。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/360693.html
