origin/config/nginx/conf.d/web.conf

116 lines
3.7 KiB
Plaintext
Raw Normal View History

2025-01-03 09:24:46 +08:00
server {
2025-01-08 00:52:11 +08:00
# 监听80端口
2025-01-03 09:24:46 +08:00
listen 80;
2025-01-08 00:52:11 +08:00
# 服务器域名/IP地址使用环境变量
2025-03-19 15:57:48 +08:00
server_name 192.168.252.77;
2025-01-03 09:24:46 +08:00
2025-01-08 00:52:11 +08:00
# 基础性能优化配置
# 启用tcp_nopush以优化数据发送
2025-01-03 09:24:46 +08:00
tcp_nopush on;
2025-01-08 00:52:11 +08:00
# 启用tcp_nodelay减少网络延迟
2025-01-03 09:24:46 +08:00
tcp_nodelay on;
2025-01-08 00:52:11 +08:00
# 设置哈希表最大大小
2025-01-03 09:24:46 +08:00
types_hash_max_size 2048;
2025-01-08 00:52:11 +08:00
# Gzip压缩配置提高传输效率
2025-01-03 09:24:46 +08:00
gzip on;
2025-01-08 00:52:11 +08:00
# 对IE6禁用Gzip
2025-01-03 09:24:46 +08:00
gzip_disable "msie6";
2025-01-08 00:52:11 +08:00
# 启用Vary头支持缓存变体
2025-01-03 09:24:46 +08:00
gzip_vary on;
2025-01-08 00:52:11 +08:00
# 对所有代理请求启用压缩
2025-01-03 09:24:46 +08:00
gzip_proxied any;
2025-01-08 00:52:11 +08:00
# 压缩级别1-96为推荐值
2025-01-03 09:24:46 +08:00
gzip_comp_level 6;
2025-01-08 00:52:11 +08:00
# 设置压缩缓冲区
2025-01-03 09:24:46 +08:00
gzip_buffers 16 8k;
2025-01-08 00:52:11 +08:00
# 压缩HTTP版本
2025-01-03 09:24:46 +08:00
gzip_http_version 1.1;
2025-01-08 00:52:11 +08:00
# 压缩的文件类型
2025-01-03 09:24:46 +08:00
gzip_types
text/plain
text/css
application/json
application/javascript
text/xml
application/xml
application/xml+rss
text/javascript;
2025-01-08 00:52:11 +08:00
# 默认站点位置配置
2025-01-03 09:24:46 +08:00
location / {
2025-01-08 00:52:11 +08:00
# 网站根目录
2025-01-03 09:24:46 +08:00
root /usr/share/nginx/html;
2025-01-08 00:52:11 +08:00
# 默认首页文件
2025-01-03 09:24:46 +08:00
index index.html index.htm;
2025-01-08 00:52:11 +08:00
# 文件缓存优化
# 最大缓存1000个文件非活跃文件20秒后失效
2025-01-03 09:24:46 +08:00
open_file_cache max=1000 inactive=20s;
2025-01-08 00:52:11 +08:00
# 缓存验证时间
2025-01-03 09:24:46 +08:00
open_file_cache_valid 30s;
2025-01-08 00:52:11 +08:00
# 至少被访问2次的文件才缓存
2025-01-03 09:24:46 +08:00
open_file_cache_min_uses 2;
2025-01-08 00:52:11 +08:00
# 缓存文件错误信息
2025-01-03 09:24:46 +08:00
open_file_cache_errors on;
2025-01-08 00:52:11 +08:00
# 尝试查找文件不存在则重定向到index.html适用于单页应用
2025-01-03 09:24:46 +08:00
try_files $uri $uri/ /index.html;
}
2025-01-08 00:52:11 +08:00
# 文件上传处理位置
2025-01-03 09:24:46 +08:00
location /uploads/ {
2025-01-08 00:52:11 +08:00
# 文件实际存储路径
2025-01-03 09:24:46 +08:00
alias /data/uploads/;
2025-01-08 00:52:11 +08:00
# 文件传输性能优化
2025-01-03 09:24:46 +08:00
sendfile on;
tcp_nopush on;
2025-01-08 00:52:11 +08:00
# 异步IO
2025-01-03 09:24:46 +08:00
aio on;
2025-01-08 00:52:11 +08:00
# 直接IO提高大文件传输效率
2025-01-03 09:24:46 +08:00
directio 512;
2025-01-08 00:52:11 +08:00
# 文件访问认证
# 通过内部认证服务验证
2025-01-03 09:24:46 +08:00
auth_request /auth-file;
2025-01-08 00:52:11 +08:00
# 存储认证状态和用户信息
2025-01-03 09:24:46 +08:00
auth_request_set $auth_status $upstream_status;
auth_request_set $auth_user_id $upstream_http_x_user_id;
auth_request_set $auth_resource_type $upstream_http_x_resource_type;
2025-01-08 00:52:11 +08:00
# 不缓存
2025-01-03 09:24:46 +08:00
expires 0;
2025-01-08 00:52:11 +08:00
# 私有缓存,禁止转换
2025-01-03 09:24:46 +08:00
add_header Cache-Control "private, no-transform";
2025-01-08 00:52:11 +08:00
# 添加用户和资源类型头
2025-01-03 09:24:46 +08:00
add_header X-User-Id $auth_user_id;
add_header X-Resource-Type $auth_resource_type;
# 带宽控制
2025-01-08 00:52:11 +08:00
# 超过100MB后限制速率为102400KB/s
2025-01-03 09:24:46 +08:00
limit_rate 102400k;
limit_rate_after 100m;
2025-01-08 00:52:11 +08:00
# 跨域资源共享(CORS)配置
2025-01-03 09:24:46 +08:00
add_header 'Access-Control-Allow-Origin' '$http_origin' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
add_header 'Access-Control-Allow-Headers'
'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization'
always;
}
2025-01-08 00:52:11 +08:00
# 内部认证服务位置
2025-01-03 09:24:46 +08:00
location = /auth-file {
2025-01-08 00:52:11 +08:00
# 仅供内部使用
2025-01-03 09:24:46 +08:00
internal;
2025-01-08 00:52:11 +08:00
# 代理到认证服务
2025-04-10 20:24:16 +08:00
proxy_pass http://192.168.252.77:3001/auth/file;
2025-02-24 19:10:38 +08:00
2025-01-08 00:52:11 +08:00
# 请求优化:不传递请求体
2025-01-03 09:24:46 +08:00
proxy_pass_request_body off;
proxy_set_header Content-Length "";
2025-01-08 00:52:11 +08:00
# 传递原始请求信息
2025-01-03 09:24:46 +08:00
proxy_set_header X-Original-URI $request_uri;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Original-Method $request_method;
proxy_set_header Host $host;
proxy_set_header X-Query-Params $query_string;
}
2025-01-08 00:52:11 +08:00
}