perf: 优化 Nginx 配置以提升性能

This commit is contained in:
jmal 2025-03-31 16:46:23 +08:00
parent 12a6790331
commit d288dc598a
2 changed files with 89 additions and 41 deletions

View File

@ -1,10 +1,11 @@
# nginx : /usr/local/Cellar/nginx/nginx-1.17.8/objs/nginx # nginx : /usr/local/Cellar/nginx/nginx-1.17.8/objs/nginx
# nginx.conf : /usr/local/nginx/conf/nginx.conf # nginx.conf : /usr/local/nginx/conf/nginx.conf
#user nobody; #user nobody;
worker_processes 2; worker_processes auto; # 自动根据 CPU 核心数设置
events { events {
worker_connections 1024; worker_connections 1024;
multi_accept on; # 提高并发连接效率
} }
#设定http服务器,利用它的反向代理功能提供负载均衡支持 #设定http服务器,利用它的反向代理功能提供负载均衡支持
@ -12,17 +13,22 @@ http {
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" '
'"$http_user_agent" "$http_x_forwarded_for"'; '"$http_user_agent" "$http_x_forwarded_for"';
#设定mime类型,类型由mime.type文件定义
include mime.types; include mime.types;
#设置文件使用的默认的MIME-type
default_type application/octet-stream; default_type application/octet-stream;
#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime
# 全局性能优化
sendfile on; sendfile on;
tcp_nopush on; tcp_nopush on;
#keepalive超时时间。 tcp_nodelay on; # 动态内容传输优化
keepalive_timeout 65; keepalive_timeout 65s;
#gzip on; keepalive_requests 100; # 单连接最大请求数
# 默认启用 gzip
gzip on;
gzip_types text/plain text/css application/javascript application/json text/xml;
gzip_min_length 1000;
gzip_vary on; # 添加 Vary: Accept-Encoding 头
map $http_upgrade $connection_upgrade { map $http_upgrade $connection_upgrade {
default upgrade; default upgrade;
'' close; '' close;
@ -137,6 +143,12 @@ http {
proxy_set_header Server-Port $server_port; proxy_set_header Server-Port $server_port;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade; proxy_set_header Connection $connection_upgrade;
sendfile on;
tcp_nopush on;
send_timeout 600s; # 延长超时
keepalive_timeout 600s; # 延长保持连接时间
output_buffers 1 512k; # 增大输出缓冲区
gzip off; # 禁用压缩,避免大文件开销
} }
location /webDAV/ { location /webDAV/ {
@ -157,6 +169,12 @@ http {
proxy_set_header Server-Port $server_port; proxy_set_header Server-Port $server_port;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection; proxy_set_header Connection $http_connection;
sendfile on;
tcp_nopush on;
send_timeout 600s; # 延长超时
keepalive_timeout 600s; # 延长保持连接时间
output_buffers 1 512k; # 增大输出缓冲区
gzip off; # 禁用压缩,避免大文件开销
} }
location /file/ { location /file/ {
@ -176,6 +194,12 @@ http {
proxy_set_header Server-Port $server_port; proxy_set_header Server-Port $server_port;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade; proxy_set_header Connection $connection_upgrade;
sendfile on;
tcp_nopush on;
send_timeout 600s; # 延长超时
keepalive_timeout 600s; # 延长保持连接时间
output_buffers 1 512k; # 增大输出缓冲区
gzip off; # 禁用压缩,避免大文件开销
} }
location /mq { location /mq {

View File

@ -1,10 +1,11 @@
# nginx : /usr/local/Cellar/nginx/nginx-1.17.8/objs/nginx # nginx : /usr/local/Cellar/nginx/nginx-1.17.8/objs/nginx
# nginx.conf : /usr/local/nginx/conf/nginx.conf # nginx.conf : /usr/local/nginx/conf/nginx.conf
#user nobody; #user nobody;
worker_processes 2; worker_processes auto; # 自动根据 CPU 核心数设置
events { events {
worker_connections 1024; worker_connections 1024;
multi_accept on; # 提高并发连接效率
} }
#设定http服务器,利用它的反向代理功能提供负载均衡支持 #设定http服务器,利用它的反向代理功能提供负载均衡支持
@ -12,17 +13,22 @@ http {
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" '
'"$http_user_agent" "$http_x_forwarded_for"'; '"$http_user_agent" "$http_x_forwarded_for"';
#设定mime类型,类型由mime.type文件定义
include mime.types; include mime.types;
#设置文件使用的默认的MIME-type
default_type application/octet-stream; default_type application/octet-stream;
#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime
# 全局性能优化
sendfile on; sendfile on;
tcp_nopush on; tcp_nopush on;
#keepalive超时时间。 tcp_nodelay on; # 动态内容传输优化
keepalive_timeout 65; keepalive_timeout 65s;
#gzip on; keepalive_requests 100; # 单连接最大请求数
# 默认启用 gzip
gzip on;
gzip_types text/plain text/css application/javascript application/json text/xml;
gzip_min_length 1000;
gzip_vary on; # 添加 Vary: Accept-Encoding 头
map $http_upgrade $connection_upgrade { map $http_upgrade $connection_upgrade {
default upgrade; default upgrade;
'' close; '' close;
@ -81,7 +87,7 @@ http {
listen 80; listen 80;
server_name localhost; server_name localhost;
root /var/www/public; root /var/www/public;
client_max_body_size 50m; client_max_body_size 1024m;
client_body_buffer_size 512k; client_body_buffer_size 512k;
location / { location / {
@ -137,6 +143,12 @@ http {
proxy_set_header Server-Port $server_port; proxy_set_header Server-Port $server_port;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade; proxy_set_header Connection $connection_upgrade;
sendfile on;
tcp_nopush on;
send_timeout 600s; # 延长超时
keepalive_timeout 600s; # 延长保持连接时间
output_buffers 1 512k; # 增大输出缓冲区
gzip off; # 禁用压缩,避免大文件开销
} }
location /webDAV/ { location /webDAV/ {
@ -157,6 +169,12 @@ http {
proxy_set_header Server-Port $server_port; proxy_set_header Server-Port $server_port;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection; proxy_set_header Connection $http_connection;
sendfile on;
tcp_nopush on;
send_timeout 600s; # 延长超时
keepalive_timeout 600s; # 延长保持连接时间
output_buffers 1 512k; # 增大输出缓冲区
gzip off; # 禁用压缩,避免大文件开销
} }
location /file/ { location /file/ {
@ -176,6 +194,12 @@ http {
proxy_set_header Server-Port $server_port; proxy_set_header Server-Port $server_port;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade; proxy_set_header Connection $connection_upgrade;
sendfile on;
tcp_nopush on;
send_timeout 600s; # 延长超时
keepalive_timeout 600s; # 延长保持连接时间
output_buffers 1 512k; # 增大输出缓冲区
gzip off; # 禁用压缩,避免大文件开销
} }
location /mq { location /mq {