78 lines
2.7 KiB
Nginx Configuration File
78 lines
2.7 KiB
Nginx Configuration File
# 配置 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 容器中,使用容器名访问(都在 1panel-network 网络中)
|
||
# 如果后端在宿主机上,改为:http://172.17.0.1:48080/admin-api
|
||
location /admin-api {
|
||
# 后端服务地址
|
||
# 方案1:使用容器名(推荐,自动 DNS 解析)- 如果后端监听 0.0.0.0
|
||
proxy_pass http://aiot-gateway:48080/admin-api;
|
||
|
||
# 方案2:直接使用后端容器 IP(当前后端 IP: 172.22.0.3)
|
||
# 如果方案1不行,取消注释下面这行,注释掉上面那行
|
||
# proxy_pass http://172.22.0.3:48080/admin-api;
|
||
|
||
# 方案3:通过宿主机网关访问(如果后端只监听 127.0.0.1)
|
||
# proxy_pass http://172.22.0.1:48080/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;
|
||
}
|
||
}
|
||
|