在nginx上用FastCGI解析PHP

编程

nginx配置文件:

Nginx 默认使用 include enable-php.conf; 通过enable-php.conf 来解析PHP,该文件内容如下:

location ~ [^/].php(/|$)

{

try_files $uri =404;

fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_index index.php;

include fastcgi.conf;

}

而我们使用nginx自然要使用fastCGI来跑PHP,Nginx之所以并发高跟fastCGI脱不开关系,有自动管理php-cgi进程的能力,总之就是它很屌,使用Nginx不用fastCGI的话就好像抽烟不点火。

因此我们看到 Nginx的配置文件中有 :include enable-php.conf; 这行代码的话,有两种方法

1、请自觉在前面加个#注释掉~

然后添加一个类似的location,下面是例子

location ~ [^/].php(/|$) 

{

try_files $uri =404;

fastcgi_pass 127.0.0.1:9000;

# fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_index index.php;

include /usr/local/nginx/conf/fastcgi.conf;

fastcgi_split_path_info ^((?U).+.php)(/?.+)$;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; #该参数正常情况下应打开,如果报错access deny 且常规方法无法解决时 请注释掉

include /usr/local/nginx/conf/fastcgi_params;

}

2、第二种解决方式,仍然引用enable-php.conf文件,但是需要修改此文件

[root@ACA83229 conf]# cat /usr/local/nginx/conf/enable-php.conf 

location ~ [^/].php(/|$)

{

try_files $uri =404;

#fastcgi_pass unix:/tmp/php-cgi.sock; //注释掉此行代码,下面新写一行,使用9000端口

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

然后重启php-fpm 和 nginx, service不行的用systemctl命令。

service php-fpm restart

service nginx restart

OK了 结束

以上内容希望帮助到大家, 很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家 ,需要戳这里   PHP进阶架构师>>>视频、面试文档免费获取

以上是 在nginx上用FastCGI解析PHP 的全部内容, 来源链接: utcz.com/z/517012.html

回到顶部