🗣 Wd提问: 程序PHPNginx配置
🤖 Ai回答: PHP与Nginx配置指南
1、基础配置
Nginx服务器块配置
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
2、PHP-FPM配置
PHP-FPM池配置 (/etc/php/8.1/fpm/pool.d/www.conf)
[www]
user = www-data
group = www-data
listen = /var/run/php/php8.1-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
3、性能优化配置
Nginx优化参数
在http块中添加
client_max_body_size 100M;
client_body_timeout 60;
keepalive_timeout 65;
启用Gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
PHP优化参数 (php.ini)
memory_limit = 256M
max_execution_time = 300
upload_max_filesize = 100M
post_max_size = 100M
opcache.enable = 1
opcache.memory_consumption = 256
opcache.max_accelerated_files = 20000
4、安全配置
Nginx安全设置
隐藏Nginx版本号
server_tokens off;
防止点击劫持
add_header X-Frame-Options SAMEORIGIN;
防止MIME类型嗅探
add_header X-Content-Type-Options nosniff;
PHP安全设置
expose_php = Off
display_errors = Off
log_errors = On
5、常见问题解决
502 Bad Gateway错误
检查PHP-FPM服务状态:
bash
systemctl status php8.1-fpm
文件权限问题
设置正确的文件权限:
bash
chown -R www-data:www-data /var/www/html
find /var/www/html -type f -exec chmod 644 {} \;
find /var/www/html -type d -exec chmod 755 {} \;
6、重启服务
bash
重启Nginx
systemctl restart nginx
重启PHP-FPM
systemctl restart php8.1-fpm
这个配置涵盖了PHP与Nginx集成的主要方面,包括基础配置、性能优化、安全设置和常见问题解决方案。
📣 商家广告:





0
IP地址: 154.184.208.230
搜索次数: 13
提问时间: 2025-12-08 02:07:33
本站所有
❓
问答
均由Ai自动分析整理,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
本站由
🟢
豌豆Ai
提供技术支持,使用的最新版:
豌豆Ai站群搜索引擎系统 V.25.10.25
搭建本站。