我正在尝试将我的后端服务器映射到http://api:4000
,my-domain.site
其中api
是我的后端服务在 docker compose 中的名称。
我运行 docker compose up 并且一切正常,但我只能访问我的后端服务器localhost
,而不能访问我想要的域my-domain.site
。
以下是我的docker-compose.yml
文件和 nginx 配置文件:
docker-compose.yml
nginx:
image: nginx:alpine
container_name: nginx
restart: always
ports:
- "80:80"
- "443:443"
command: nginx -g "daemon off;"
volumes:
- ./conf.d:/etc/nginx/conf.d
- ./hosts:/etc/hosts
depends_on:
- api
api:
container_name: backend
build:
context: ./backend
dockerfile: Dockerfile
command: npm start
ports:
- "4000:4000"
conf.d/default.conf
server {
listen 80;
server_name my-domain.site;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://api:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}