一、基本配置
nginx默认配置文档中的php配置,这段配置是可用的:
1 2 3 4 5 6 7 |
location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } |
一些情况下使用这段配置可能会导致以下错误:
1 |
[error] 20591#0: *14 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/usr/local/php/var/run/php-fpm.sock:", host: "127.0.0.1" |
此时需要把配置中的:
1 |
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; |
修改成:
1 |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
二、修改fastcgi的监听方式为socket
当并发量小的时候,可以考虑把fastcgi的通信方式改成socket
,socket
可以减少一部分tcp
开销,性能更优一点。
打开修改php-fpm的配置文件,查找listen
做如下修改:
1 2 3 4 |
# 注释掉本行 ; listen = 127.0.0.1:9000 # socket文件的目录,保证启动用户有权限访问到上级目录 listen = /usr/local/php/var/run/php-fpm.sock |
nginx中的配置:
1 2 3 4 5 6 7 8 |
location ~ \.php$ { root html; # 改成socket文件的地址 fastcgi_pass unix:/usr/local/php/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } |
然后重启nginx
和php-fpm
即可。
遇到的错误
1 |
[crit] 20721#0: *16 connect() to unix:/usr/local/php/var/run/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/usr/local/php/var/run/php-fpm.sock:", host: "127.0.0.1 |
这是因为nginx
启动用户没有权限访问socket
文件,需要在php-fpm
的配置文件中修改启动用户,然后重启php-fpm
:
1 2 |
listen.owner = www listen.group = www |
评论