本文编写于 779 天前,最后修改于 779 天前,其中某些信息可能已经过时。
0x01 worker_processes:
这个参数定义了Nginx启动的worker进程的数量。通常设置为服务器的CPU核心数。例如,如果你有4个CPU核心,可以将
worker_processes设置为4。
worker_processes 4;
0x02 worker_connections:
worker_connections定义了每个worker进程可以处理的最大连接数。根据服务器的负载和并发连接数进行调整。
events {
worker_connections 1024;
}
0x03 multi_accept:
启用multi_accept可以让一个worker进程同时接受多个新连接,提高性能。
events {
multi_accept on;
}
0x04 keepalive_timeout:
keepalive_timeout设置了Keep-Alive连接的超时时间,过长的超时时间可能导致连接池被占满。
keepalive_timeout 15;
0x05 sendfile:
sendfile指令允许Nginx直接将文件从磁盘发送到客户端,提高文件传输效率。
sendfile on;
0x06 tcp_nodelay:
tcp_nodelay开启TCP_NODELAY,禁用Nagle算法,提高传输性能。
tcp_nodelay on;
0x07 tcp_nopush:
tcp_nopush禁用TCP_CORK,适用于发送大文件,提高性能。
tcp_nopush on;
0x08 gzip:
使用gzip压缩来减小传输数据量,提高响应速度。
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
0x09 server_tokens:
禁用server_tokens以隐藏Nginx版本信息。
server_tokens off;
0x10 open file cache:
open_file_cache指令可以提高文件的打开速度,减轻文件I/O压力。
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
0x11 使用Nginx缓存:
使用Nginx的proxy_cache模块或者fastcgi_cache模块进行缓存,减轻后端服务器负载。