当前位置:Gxlcms > PHP教程 > nginx和php-fpm之间是怎样通信的?

nginx和php-fpm之间是怎样通信的?

时间:2021-07-01 10:21:17 帮助过:21人阅读

  1. <code>server {
  2. root /srv/www;
  3. location / {
  4. index index.html index.htm;
  5. }
  6. location ~ \.php$ {
  7. fastcgi_pass 127.0.0.1:9000;
  8. fastcgi_param SCRIPT_FILENAME /src/www$fastcgi_script_name;
  9. include fastcgi_params;
  10. }
  11. }
  12. </code>

在目录/srv/www中有index.html index.php 两个文件,访问localhost/index.html ,localhost 能够正常显示/srv/www/index.html页面的内容,但是访问index.php文件却显示File Not Found,不知到是怎么回事?在这种情况下难道nginx不是应该将/srv/www/index.php文件先传送给监听在127.0.0.1:9000的php解析器,然后通过其解析后返回解析器的内容给客户端吗?不知到哪里的问题,希望帮忙解答下

回复内容:

  1. <code>server {
  2. root /srv/www;
  3. location / {
  4. index index.html index.htm;
  5. }
  6. location ~ \.php$ {
  7. fastcgi_pass 127.0.0.1:9000;
  8. fastcgi_param SCRIPT_FILENAME /src/www$fastcgi_script_name;
  9. include fastcgi_params;
  10. }
  11. }
  12. </code>

在目录/srv/www中有index.html index.php 两个文件,访问localhost/index.html ,localhost 能够正常显示/srv/www/index.html页面的内容,但是访问index.php文件却显示File Not Found,不知到是怎么回事?在这种情况下难道nginx不是应该将/srv/www/index.php文件先传送给监听在127.0.0.1:9000的php解析器,然后通过其解析后返回解析器的内容给客户端吗?不知到哪里的问题,希望帮忙解答下

估计是你nginx的fpm的配置问题。
我这边这样搞的,已经用过好多个网站了:

  1. <code>server {
  2. listen 80; ## listen for ipv4; this line is default and implied
  3. root /www/web;
  4. index index.html index.htm index.php;
  5. location / {
  6. # First attempt to serve request as file, then
  7. # as directory, then fall back to displaying a 404.
  8. try_files $uri $uri/ /index.html;
  9. # Uncomment to enable naxsi on this location
  10. # include /etc/nginx/naxsi.rules
  11. }
  12. location ~ \.php$ {
  13. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  14. fastcgi_pass unix:/var/run/php5-fpm.sock;
  15. fastcgi_index index.php;
  16. include fastcgi_params;
  17. }
  18. }
  19. </code>

看看fpm错误日志,分分钟定位问题啊!

  1. <code>location / {
  2. index index.php index.html index.htm;
  3. }
  4. </code>

index 中需要加上index.php 优先识别最前面的文件

问题出在这里:

  1. <code>server {
  2. root /srv/www;#srv目录
  3. location / {
  4. index index.html index.htm;
  5. }
  6. location ~ \.php$ {
  7. fastcgi_pass 127.0.0.1:9000;
  8. fastcgi_param SCRIPT_FILENAME /src/www$fastcgi_script_name; #src目录
  9. include fastcgi_params;
  10. }
  11. }
  12. </code>

另外:
下面那部分这样写最好:

  1. <code>location ~ \.php$ {
  2. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  3. fastcgi_pass 127.0.0.1:9000;
  4. # fastcgi_pass unix:/var/run/php5-fpm.sock;#这行和上面一行二选一。
  5. fastcgi_index index.php;
  6. include fastcgi_params;
  7. }
  8. </code>

人气教程排行