; 内存限制
memory_limit = 256M
; 最大执行时间
max_execution_time = 30
; 每个脚本解析输入数据(POST, GET, upload)的最大时间
max_input_time = 60
; 上传文件大小限制
upload_max_filesize = 32M
post_max_size = 40M
; 禁用危险函数
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
; 启用OPcache加速
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
; 生产环境关闭错误显示
display_errors = Off
display_startup_errors = Off
; 记录错误日志
log_errors = On
error_log = /var/log/php_errors.log
; 错误报告级别
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
; 进程管理方式
pm = dynamic
; 最大子进程数
pm.max_children = 50
; 启动时的子进程数
pm.start_servers = 10
; 空闲时最小子进程数
pm.min_spare_servers = 5
; 空闲时最大子进程数
pm.max_spare_servers = 15
; 每个子进程处理多少请求后重启
pm.max_requests = 500
; 慢日志记录
slowlog = /var/log/php-fpm-slow.log
request_slowlog_timeout = 10s
; 每个请求的内存限制
php_admin_value[memory_limit] = 128M
; 上传限制
php_admin_value[upload_max_filesize] = 32M
php_admin_value[post_max_size] = 40M
; 禁用危险函数
php_admin_value[disable_functions] = "exec,passthru,shell_exec,system"
worker_processes auto; # 自动根据CPU核心数设置工作进程
worker_rlimit_nofile 100000; # 每个worker能打开的文件描述符数量
events {
worker_connections 4096; # 每个worker的最大连接数
multi_accept on; # 一次接受多个连接
use epoll; # Linux下高性能事件模型
}
http {
sendfile on; # 启用sendfile系统调用
tcp_nopush on; # 启用TCP_NOPUSH选项
tcp_nodelay on; # 禁用Nagle算法
keepalive_timeout 30; # 保持连接超时时间
keepalive_requests 1000; # 单个连接的最大请求数
client_max_body_size 40M; # 客户端请求体最大值
# 启用gzip压缩
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 静态文件缓存头
map $sent_http_content_type $expires {
default off;
text/html 1h;
text/css max;
application/javascript max;
~image/ max;
}
expires $expires;
# 包含虚拟主机配置
include /etc/nginx/conf.d/*.conf;
}
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
# 静态文件处理
location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|svg)$ {
expires max;
access_log off;
add_header Cache-Control "public";
try_files $uri =404;
}
# PHP处理
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# 优化FastCGI参数
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
# 连接超时设置
fastcgi_connect_timeout 60s;
fastcgi_send_timeout 60s;
fastcgi_read_timeout 60s;
}
# 禁止访问隐藏文件
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# 日志设置
access_log /var/log/nginx/access.log combined buffer=32k flush=5m;
error_log /var/log/nginx/error.log warn;
}
# 增加文件描述符限制
fs.file-max = 65535
# TCP优化
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.ip_local_port_range = 1024 65535
# 内存优化
vm.swappiness = 10
vm.vfs_cache_pressure = 50
应用修改:sysctl -p
* soft nofile 65535
* hard nofile 65535
nginx soft nofile 65535
nginx hard nofile 65535
监控工具:
htop
、glances
监控系统资源nginx-status
模块监控Nginx状态php-fpm-status
监控PHP-FPM状态调优方法:
pm.max_children
值(公式:总内存 / 单个PHP进程内存)php-fpm-slow.log
优化慢脚本ab
或wrk
进行压力测试安全建议:
以上配置需要根据实际服务器硬件配置和应用特点进行调整,建议先在测试环境验证后再应用到生产环境。