Files
aiot-platform-ui/apps/web-antd/nginx.conf
lzh 7773b1dc88
All checks were successful
Web UI CI/CD / build-and-deploy (push) Successful in 3m40s
fix: 修复后端接口连接不上问题
2025-12-18 14:20:06 +08:00

76 lines
2.6 KiB
Nginx Configuration File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 配置 DNS 解析器(使用 Docker 内置 DNS
resolver 127.0.0.11 valid=30s;
server {
listen 80;
server_name localhost; # 通配符,匹配所有域名(适用于反向代理场景)
# 开启 gzip 压缩
gzip on;
gzip_min_length 1k;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
root /usr/share/nginx/html;
index index.html;
# API 反向代理到后端服务
# 后端服务在 Docker 容器中,使用容器名访问(都在 aiot-net 网络中)
# 如果后端在宿主机上改为http://172.17.0.1:48080/admin-api
location /admin-api {
# 后端服务地址
# 如果后端监听 0.0.0.0,使用容器名(推荐)
# set $backend "aiot-server:48080";
# 如果后端只监听 127.0.0.1,通过宿主机网关访问(当前方案)
# 172.22.0.1 是 aiot-net 网络的网关,指向宿主机
set $backend "aiot-server:48080";
proxy_pass http://$backend/admin-api;
# 代理超时设置
proxy_connect_timeout 10s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 错误处理:如果后端不可用,返回友好的错误信息
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
proxy_next_upstream_tries 2;
# 代理请求头
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;
# 解决跨域问题
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
# 处理 OPTIONS 预检请求
if ($request_method = 'OPTIONS') {
return 204;
}
}
# 解决 SPA 路由刷新 404 问题
location / {
try_files $uri $uri/ /index.html;
}
# 静态资源缓存配置
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
# 错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}