- 服务器上有多个 compose 项目(inventory-app / Track)都使用 backend 服务名 - nginx 中 proxy_pass http://backend:8000 会随机解析到 track_backend_prod 导致 /api/ 请求 404 - 改为使用固定容器名 inventory_api_prod:8000
60 lines
1.7 KiB
Nginx Configuration File
60 lines
1.7 KiB
Nginx Configuration File
# --- HTTP 重定向到 HTTPS ---
|
||
server {
|
||
listen 80;
|
||
server_name _; # 匹配所有域名/IP
|
||
|
||
# 将所有 HTTP 请求强制跳转到 HTTPS
|
||
return 301 https://$host$request_uri;
|
||
}
|
||
|
||
# --- HTTPS 服务器配置 ---
|
||
server {
|
||
listen 443 ssl;
|
||
server_name _;
|
||
|
||
# 1. SSL 证书配置
|
||
ssl_certificate /etc/nginx/ssl/nginx.crt;
|
||
ssl_certificate_key /etc/nginx/ssl/nginx.key;
|
||
|
||
# SSL 优化配置 (可选)
|
||
ssl_protocols TLSv1.2 TLSv1.3;
|
||
ssl_ciphers HIGH:!aNULL:!MD5;
|
||
|
||
# 允许上传大文件
|
||
client_max_body_size 20M;
|
||
|
||
# 开启 Gzip
|
||
gzip on;
|
||
gzip_min_length 1k;
|
||
gzip_comp_level 6;
|
||
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript image/jpeg image/gif image/png;
|
||
|
||
# 2. 前端 Vue 页面
|
||
location / {
|
||
root /usr/share/nginx/html;
|
||
index index.html index.htm;
|
||
try_files $uri $uri/ /index.html;
|
||
}
|
||
|
||
# 3. 后端 API 接口代理
|
||
# ★ 使用容器名 inventory_api_prod 而非服务名 backend,
|
||
# 避免与其他 compose 项目的 backend 服务 DNS 冲突(track_backend_prod)
|
||
location /api/ {
|
||
proxy_pass http://inventory_api_prod:8000;
|
||
|
||
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; # 告诉后端这是 HTTPS 请求
|
||
|
||
proxy_connect_timeout 300s;
|
||
proxy_read_timeout 300s;
|
||
proxy_send_timeout 300s;
|
||
}
|
||
|
||
# 4. 图片资源托管
|
||
location /uploads/ {
|
||
alias /usr/share/nginx/html/uploads/;
|
||
expires 30d;
|
||
}
|
||
} |