| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- # =============================================================================
- # 游铛铛 admin 前端 — 生产环境 Nginx 示例
- # 部署机:125.122.32.114
- #
- # 用法 A(推荐,与 Debian/Ubuntu 默认结构一致):
- # 只取下方 server { ... } 块,保存为
- # /etc/nginx/sites-available/youdangdang-admin
- # ln -sf /etc/nginx/sites-available/youdangdang-admin /etc/nginx/sites-enabled/
- # 主 nginx.conf 需保留 http { include sites-enabled/*; }
- #
- # 用法 B:整机仅此站点时,可将本文件作为 /etc/nginx/nginx.conf 使用(已含 events/http)。
- #
- # 前置条件:
- # 1. 将 npm run build 生成的 dist 放到 root 指向的目录(默认 /var/www/youdangdang-admin/dist)
- # 2. 本机已启动 backend-admin(uvicorn 监听 127.0.0.1:9999 或 0.0.0.0:9999)
- # 3. admin 打包时 VITE_API_BASE_URL 为空(同源,仅请求 /admin/...,无跨域)
- # =============================================================================
- user www-data;
- worker_processes auto;
- error_log /var/log/nginx/error.log warn;
- pid /run/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 65;
- client_max_body_size 50m;
- gzip on;
- gzip_vary on;
- gzip_min_length 1024;
- gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
- # 若与本机其它站点冲突,可改为 include /etc/nginx/conf.d/*.conf; 并只放 server 块到单独文件
- server {
- listen 80;
- listen [::]:80;
- server_name 125.122.32.114;
- # 修改为服务器上 admin/dist 的实际路径
- root /var/www/youdangdang-admin/dist;
- index index.html;
- # ---------- 管理端 API:同域反代,浏览器无跨域 ----------
- # 请求路径保持不变转发到 FastAPI(/admin/...)
- location /admin/ {
- proxy_pass http://127.0.0.1:9999;
- proxy_http_version 1.1;
- 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;
- proxy_connect_timeout 60s;
- proxy_send_timeout 60s;
- proxy_read_timeout 60s;
- # 长连接可选(SSE/WebSocket 时按需打开)
- # proxy_set_header Connection "";
- }
- # 无尾部斜杠的 /admin 可选重定向(与前端路由一致即可)
- location = /admin {
- return 301 /admin/;
- }
- # ---------- 后端健康检查、Swagger(按需保留;不需要可注释) ----------
- location = /health {
- proxy_pass http://127.0.0.1:9999/health;
- 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;
- }
- location /docs {
- proxy_pass http://127.0.0.1:9999;
- 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;
- }
- location /redoc {
- proxy_pass http://127.0.0.1:9999;
- 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;
- }
- location = /openapi.json {
- proxy_pass http://127.0.0.1:9999/openapi.json;
- proxy_set_header Host $host;
- }
- # ---------- SPA:前端路由 ----------
- location / {
- try_files $uri $uri/ /index.html;
- }
- # 静态资源缓存(可选)
- location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
- expires 7d;
- add_header Cache-Control "public, immutable";
- try_files $uri =404;
- }
- }
- }
|