|
|
|
@ -4,18 +4,30 @@ error_log /var/log/nginx/error.log warn;
|
|
|
|
pid /var/run/nginx.pid;
|
|
|
|
pid /var/run/nginx.pid;
|
|
|
|
|
|
|
|
|
|
|
|
events {
|
|
|
|
events {
|
|
|
|
|
|
|
|
# 可以根据业务并发量适当调高
|
|
|
|
worker_connections 1024;
|
|
|
|
worker_connections 1024;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
http {
|
|
|
|
http {
|
|
|
|
include mime.types;
|
|
|
|
include mime.types;
|
|
|
|
default_type application/octet-stream;
|
|
|
|
default_type application/octet-stream;
|
|
|
|
|
|
|
|
# 高效传输文件
|
|
|
|
sendfile on;
|
|
|
|
sendfile on;
|
|
|
|
|
|
|
|
# 长连接超时时间
|
|
|
|
keepalive_timeout 65;
|
|
|
|
keepalive_timeout 65;
|
|
|
|
|
|
|
|
# 单连接最大请求数,提高长连接复用率
|
|
|
|
|
|
|
|
keepalive_requests 100000;
|
|
|
|
# 限制body大小
|
|
|
|
# 限制body大小
|
|
|
|
client_max_body_size 100m;
|
|
|
|
client_max_body_size 100m;
|
|
|
|
|
|
|
|
client_header_buffer_size 32k;
|
|
|
|
|
|
|
|
client_body_buffer_size 512k;
|
|
|
|
# 开启静态资源压缩
|
|
|
|
# 开启静态资源压缩
|
|
|
|
gzip_static on;
|
|
|
|
gzip_static on;
|
|
|
|
|
|
|
|
# 连接数限制 (防御类配置) 10m 一般够用了,能存储上万 IP 的计数
|
|
|
|
|
|
|
|
limit_conn_zone $binary_remote_addr zone=perip:10m;
|
|
|
|
|
|
|
|
limit_conn_zone $server_name zone=perserver:10m;
|
|
|
|
|
|
|
|
# 隐藏 nginx 版本号,防止暴露版本信息
|
|
|
|
|
|
|
|
server_tokens off;
|
|
|
|
|
|
|
|
|
|
|
|
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
|
|
|
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
|
|
|
'$status $body_bytes_sent "$http_referer" '
|
|
|
|
'$status $body_bytes_sent "$http_referer" '
|
|
|
|
@ -69,17 +81,29 @@ http {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
location /prod-api/ {
|
|
|
|
location /prod-api/ {
|
|
|
|
|
|
|
|
# 设置客户端请求头中的 Host 信息(保持原始 Host)
|
|
|
|
proxy_set_header Host $http_host;
|
|
|
|
proxy_set_header Host $http_host;
|
|
|
|
|
|
|
|
# 获取客户端真实 IP
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
|
|
|
# 自定义头 REMOTE-HOST,记录客户端 IP
|
|
|
|
proxy_set_header REMOTE-HOST $remote_addr;
|
|
|
|
proxy_set_header REMOTE-HOST $remote_addr;
|
|
|
|
|
|
|
|
# 获取完整的客户端 IP 链(经过多级代理时)
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
|
|
|
|
# 设置后端响应超时时间(这里是 24 小时,适合长连接/SSE)
|
|
|
|
proxy_read_timeout 86400s;
|
|
|
|
proxy_read_timeout 86400s;
|
|
|
|
# sse 与 websocket参数
|
|
|
|
# SSE (Server-Sent Events) 与 WebSocket 支持参数
|
|
|
|
proxy_http_version 1.1;
|
|
|
|
proxy_http_version 1.1;
|
|
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
|
|
proxy_set_header Connection "upgrade";
|
|
|
|
proxy_set_header Connection "upgrade";
|
|
|
|
|
|
|
|
# 禁用代理缓冲,数据直接传给客户端
|
|
|
|
proxy_buffering off;
|
|
|
|
proxy_buffering off;
|
|
|
|
|
|
|
|
# 禁用代理缓存
|
|
|
|
proxy_cache off;
|
|
|
|
proxy_cache off;
|
|
|
|
|
|
|
|
# 按 IP 限制连接数(防 CC 攻击) 小型站:10~20 就够 中型站:50~100
|
|
|
|
|
|
|
|
limit_conn perip 20;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 按 Server 限制总并发连接数 根据服务器的最大并发处理能力来定 太小会限制合法用户访问,太大会占满服务器资源
|
|
|
|
|
|
|
|
limit_conn perserver 500;
|
|
|
|
proxy_pass http://server/;
|
|
|
|
proxy_pass http://server/;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|