今天爱分享给大家带来Nginx如何解析php相关配置【附代码】,希望能够帮助到大家。
[root@minglinux-01 ~] vim /usr/local/nginx/conf/vhost/test.com.conf
···
50 location ~ \.php$
51 {
52 include fastcgi_params;
53 fastcgi_pass unix:/tmp/php-fcgi.sock;
54 fastcgi_index index.php;
55 fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
56 }
···
fastcgi_pass用来指定php-fpm的地址,指定错误地址时可能报502错误
。如果php-fpm监听的是一个tcp:port的地址( 比如127.0.0.1:9000),那么也需要在这里改成fastcgi_pass 127.0.0.1:9000
factcgi_param SCRIPT_FILENAME后面跟的路径为该站点的根目录,和server中的root路径保持一致。如果配置不对,访问PHP页面会出现404。
[root@minglinux-01 ~] /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@minglinux-01 ~] /usr/local/nginx/sbin/nginx -s reload
[root@minglinux-01 ~] vim /data/wwwroot/test.com/3.php
1
[root@minglinux-01 ~] vim /usr/local/php-fpm/etc/php-fpm.conf
1 [global]
2 pid = /usr/local/php-fpm/var/run/php-fpm.pid
3 error_log = /usr/local/php-fpm/var/log/php-fpm.log
4 [www]
5 listen = /tmp/php-fcgi.sock //php-fpm监听地址
6 listen.mode = 666 //权限666让所有文件对php的socket文件(/tmp/php-fcgi.sock)有读和写权限,无读和写权限则用户nginx无法读socket文件即无法与php-fpm通信导致php解析不正常。
7 user = php-fpm
8 group = php-fpm
9 pm = dynamic
10 pm.max_children = 50
11 pm.start_servers = 20
12 pm.min_spare_servers = 5
13 pm.max_spare_servers = 35
14 pm.max_requests = 500
15 rlimit_files = 1024