Skip to content

Nginx、PHP-FPM 与反向代理配置教程 ​

文章定位:Nginx、PHP-FPM、静态资源、TLS、缓存和安全头

Nginx、PHP-FPM 与反向代理配置教程技术图

核心结论 ​

请求通常先到 Nginx,再根据路径返回静态资源、转发上游或交给 PHP-FPM。排查时区分客户端、Nginx、FPM、应用和数据库层。

nginx
server {
    listen 80;
    server_name example.com;
    root /srv/app/public;
    location / { try_files $uri $uri/ /index.php?$query_string; }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }
}

实际 socket 路径以系统安装版本为准。配置发布前执行 nginx -t。禁止暴露 .env、版本控制目录、备份文件和可执行上传文件。

FPM worker 数量根据内存和请求耗时压测决定。Opcache 需要配合发布策略和缓存失效。反向代理缓存不能缓存个性化响应或带身份信息的内容。

参考资料 ​

1 2

VitePress Vue.js 3.5+ Node.js 24.04 LTS pnpm Nginx Released under the MIT License.