Nginx 配置 PHP-FPM 有哪些选项?
在 Nginx 中配置 PHP-FPM 需要关注以下核心选项,这些配置直接影响性能、安全和稳定性:
一、基础代理配置(必需)
location ~ \.php$ {
    # 1. 后端连接方式
    fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;  # Unix Socket(推荐)
    # fastcgi_pass 127.0.0.1:9000;                   # TCP 方式
    # 2. 关键参数传递
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
    # 3. 标准参数集
    include fastcgi_params;   # 或 fastcgi.conf(推荐后者)
    
    # 4. 默认入口文件
    fastcgi_index index.php;
}二、安全加固配置
location ~ \.php$ {
    # 1. 防止任意文件执行
    try_files $uri =404;
    
    # 2. 隐藏PHP版本
    fastcgi_hide_header X-Powered-By;
    
    # 3. 禁用危险函数(在php.ini中配置)
    #    disable_functions = exec,passthru,shell_exec,system
    
    # 4. 限制敏感目录访问
    location ~* /(\.git|vendor|env|config) {
        deny all;
        return 403;
    }
}三、性能优化配置
http {
    # 1. 缓冲区优化
    fastcgi_buffers 16 16k;       # 响应正文缓冲区
    fastcgi_buffer_size 32k;       # 响应头缓冲区
    fastcgi_busy_buffers_size 32k; # 忙碌时缓冲区
    
    # 2. 连接复用
    fastcgi_keep_conn on;          # 保持连接(Nginx ≥1.1.4)
    
    # 3. 超时控制
    fastcgi_connect_timeout 5s;    # 连接后端超时
    fastcgi_send_timeout 15s;      # 发送请求超时
    fastcgi_read_timeout 60s;      # 接收响应超时
    
    # 4. 临时文件优化
    fastcgi_temp_path /dev/shm/nginx_temp; # 使用内存盘
    fastcgi_max_temp_file_size 1024m;
}四、文件上传处理
server {
    # 1. 最大上传大小
    client_max_body_size 100m;
    
    # 2. 请求体缓存
    client_body_buffer_size 128k;
    
    location ~ \.php$ {
        # 3. 文件上传超时延长
        fastcgi_read_timeout 300s;
    }
}五、路径路由支持
# 支持PATH_INFO(如 index.php/user/profile)
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    
    # 防止路径遍历攻击
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
}六、缓存与压缩
http {
    # 1. 静态资源缓存
    location ~* \.(jpg|css|js)$ {
        expires 30d;
        add_header Cache-Control "public";
    }
    
    # 2. PHP响应压缩
    gzip on;
    gzip_types text/plain application/json application/x-javascript text/css;
}七、日志与监控
server {
    # 1. 访问日志格式
    log_format php_log '$remote_addr - $request_time - $upstream_response_time';
    access_log /var/log/nginx/php-access.log php_log;
    
    location ~ \.php$ {
        # 2. 记录慢请求
        fastcgi_slow_log /var/log/nginx/php-slow.log;
        fastcgi_slow_log_timeout 2s; # >2秒视为慢请求
    }
}八、完整配置示例
server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.php;
    # 静态文件处理
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    # PHP处理核心
    location ~ \.php$ {
        try_files $uri =404;
        
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        
        # 性能优化
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_read_timeout 60s;
        fastcgi_keep_conn on;
        
        # 安全
        fastcgi_hide_header X-Powered-By;
    }
    # 安全防护
    location ~ /\.(env|git) {
        deny all;
        return 404;
    }
    
    # 文件上传限制
    client_max_body_size 50m;
}九、关键检查清单
- 
权限验证: ls -l /var/run/php/php*.sock # 确认Nginx用户有访问权限
- 
配置测试: nginx -t && systemctl reload nginx
- 
连接测试: # Unix Socket测试 sudo -u www-data curl --unix-socket /var/run/php/php8.3-fpm.sock http://localhost/status # TCP端口测试 telnet 127.0.0.1 9000
- 
性能监控: # 实时查看PHP-FPM状态 watch -n1 "curl -s http://localhost/status | grep 'idle\|active'"
最佳实践提示:
Unix Socket 比 TCP 性能高 30% 以上
fastcgi.conf比fastcgi_params更安全(默认包含SCRIPT_FILENAME)
生产环境必须设置
try_files $uri =404防止任意代码执行
缓冲区大小需根据平均响应体积调整(使用
$upstream_response_length日志分析)
本文来自博客园,作者:Carver大脸猫,转载请注明原文链接:https://www.cnblogs.com/carver/articles/18978370

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号