18
2025
05
22:48:48

nginx代理php请求

  1. 安装和启动php-fpm

    # 以下测试环境系统为CentOS Linux release 7.7.1908

    安装php及相关模块
    yum install php php-gd php-soap php-mbstring php-xmlrpc php-dom php-fpm -y

    检查是否安装成功
    php -v

    修改监听  listen = 127.0.0.1:9000
    vi /etc/php-fpm.d/php.conf

    # 启动php-fpm
    systemctl start php-fpm


  2. 配置nginx

    server {
     listen 86;
     server_name  localhost;

     location ~ \.php$ {
      root html;
      include snippets/fastcgi-php.conf;
      fastcgi_pass 127.0.0.1:9000;
      include fastcgi_params;
     }
    }


  3. 测试

    # 准备两个测试文件
    echo '<?php phpinfo();?>' >> /usr/local/nginx/html/index.php
    echo '<?php echo "php-fpm is working...";?>' >> /usr/local/nginx/html/test.php


    http://xxx.xxx.xxx.xxx:86/index.php

    图片


    http://xxx.xxx.xxx.xxx:86/test.php

    图片


  4. 相关知识点

  • CGI

    即Common Gateway Interface。早期的web服务器只能处理静态请求,对于动态请求需要生新进程来处理,进程把处理完的数据返回给web服务器,进程退出,web服务器再返回给客户端。CGI是早期的http服务器与其它应用程序的通信接口,CGI程序运行在http服务器上。CGI可以使用多种语言编写。

  • FastCGI

    当大量动态请求进入web服务器时,CGI每次都要生成新的进程效率低,资源占用高,因此出现了FastCGI。FastCGI常驻在系统中,不会每次都生成新的进程。nginx中http模块默认支持FastCGI模式,默认配置为:

    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;
    }
  • php-fpm

    php-fpm是php的FastCGI管理进程,接收和处理web服务器的php请求。

  • 查看已安装的php模块:php -m
  • 检查php-fpm配置文件是否配置正确:php-fpm -t,这里可以看到配置文件路径




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://zblog.hqyman.cn/post/11261.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: