我正在尝试使用 docker 设置我的前端、后端和数据库。以下是只有我的前端容器应该向后端发出请求而不是向公众发出请求的条件。我已经编写了一个 conf 文件,但我仍然能够使用 postman 访问后端。此外,我正在将前端构建文件移动到卷并将其安装到那里,/usr/share/nginx/html
然后前端容器将停止,文件将直接从 nginx 容器提供。
在主 nginx 容器路由到前端 nginx 服务的情况下,在前端容器中运行另一个 nginx 服务是否好?
默认配置文件
upstream backend_servers {
server backend:8000;
}
server {
listen 80;
location / {
root /usr/share/nginx/html/frontend;
try_files $uri $uri/ /index.html;
}
# Reverse proxy requests to the uWSGI server
location /api {
rewrite ^/api(.*) $1 break;
uwsgi_pass backend_servers;
include /etc/nginx/uwsgi_params;
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-Proto $scheme;
}
}
我的 Docker 撰写文件是
version: '3'
services:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.prod
volumes:
- app-data:/app/dist
backend:
build:
context: .
dockerfile: Dockerfile.prod
environment:
- DEBUG=0
- ALLOWED_HOSTS=nginx,localhost,127.0.0.1
- SECRET_KEY=my-secure-key
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
db:
image: postgres:latest
restart: always
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- db-data:/var/lib/postgresql/data
nginx:
build:
context: ./nginx
dockerfile: Dockerfile.prod
ports:
- "80:80"
volumes:
- app-data:/usr/share/nginx/html/frontend
depends_on:
- backend
volumes:
db-data:
app-data:
答案1
将前端、后端和 nginx 放在它们自己的桥接网络上:
networks:
backend:
driver: bridge
service:
frontend:
...
networks:
- backend