Nginx 配置 PHP 完整指南
一、基础环境准备
确保已安装:
Nginx
PHP-FPM (PHP FastCGI Process Manager)
PHP
二、核心配置原理
Nginx 本身不能直接处理 PHP,需要通过 PHP-FPM 来处理 PHP 请求。
复制客户端 → Nginx (静态资源) → PHP-FPM (PHP处理) → 返回结果
三、配置文件详解
1. 主配置文件 nginx.conf
nginxnginx复制user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
sendfile on;
keepalive_timeout 65;
# 引入站点配置
include /etc/nginx/sites-enabled/*;
}
2. PHP-FPM 池配置 (http://www.conf)
iniini复制[www]user = www-datagroup = www-datalisten = /run/php/php7.4-fpm.sock ; Unix Socket 方式; listen = 127.0.0.1:9000 ; TCP 方式pm = dynamicpm.max_children = 5pm.start_servers = 2pm.min_spare_servers = 1pm.max_spare_servers = 3
3. 站点配置文件 (example.com.conf)
nginxnginx复制server { listen 80;
server_name example.com www.example.com;
root /var/www/html; index index.php index.html index.htm; # 字符集
charset utf-8; # 访问日志
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log; # 安全头
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff"; # ============ PHP 核心配置 ============
location ~ \.php$ {
try_files $uri =404;
# 使用 Unix Socket 连接 PHP-FPM
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# 或者使用 TCP 方式
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# 超时设置
fastcgi_read_timeout 300;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
} # 禁止访问 .ht 开头的文件
location ~ /\.ht {
deny all;
} # 静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|txt)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
四、配置说明
关键指令解释
指令作用
fastcgi_pass 指定 PHP-FPM 地址(Socket 或 TCP)
fastcgi_index 默认索引文件
SCRIPT_FILENAME PHP 脚本的绝对路径
try_files $uri =404 防止 PHP 文件不存在时的漏洞
两种连接方式对比
Unix Socket (推荐)
nginxnginx复制fastcgi_pass unix:/run/php/php7.4-fpm.sock;
✅ 性能更好
✅ 不需要网络开销
❌ 仅限本机
TCP 方式
nginxnginx复制fastcgi_pass 127.0.0.1:9000;
✅ 可跨服务器
❌ 性能稍差
❌ 需要开放端口
五、测试与验证
1. 检查配置语法
bashbash复制sudo nginx -t
2. 重启服务
bashbash复制# Ubuntu/Debiansudo systemctl restart php7.4-fpmsudo systemctl restart nginx# CentOS/RHELsudo systemctl restart php-fpmsudo systemctl restart nginx
3. 创建测试文件
bashbash复制echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
4. 验证访问
bashbash复制curl http://localhost/info.php
六、常见问题排查
1. 502 Bad Gateway
bashbash复制# 检查 PHP-FPM 是否运行sudo systemctl status php7.4-fpm# 检查 Socket 文件是否存在ls -la /run/php/
2. 权限问题
bashbash复制# 确保 Nginx 用户有权限访问网站目录sudo chown -R www-data:www-data /var/www/htmlsudo chmod -R 755 /var/www/html
3. 查看错误日志
bashbash复制tail -f /var/log/nginx/error.logtail -f /var/log/php7.4-fpm.log
七、生产环境优化建议
nginxnginx复制# 在 http 块中添加fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 300;# 启用 gzipgzip on;
gzip_types text/plain text/css application/json application/javascript;
gzip_min_length 1000;
陕公网安备 61012502000223号
评论一下吧