总结与备忘:nginx与php-fpm与php

来源:互联网 发布:淘宝网首页包 编辑:程序博客网 时间:2024/04/20 05:08

nginx

      不仅仅可作HTTP服务器使用,更可用于做HTTP(第七层)上的负载均衡或代理,也可以做UDP/TCP(第四层)上的负载均衡或代理(V1.9.0开始),也可以做邮箱服务器

php-fpm

      实现了CGI标准接口并实现了FastCGI进程的管理

php

      相当于以前的CGI子程序,具体的服务逻辑脚本



访问php页面大致工作流程:

1.nginx接收并解析外部http请求报文

2.nginx根据配置(nginx.conf),判断若为请求php页面则向php-fpm服务(fastcgi_pass)传入(通过IPC_SOCKET或TCP)相关的CGI参数(fastcgi_param)及php脚本所在路径参数(fastcgi_param  SCRIPT_FILENAME)

3.php-fpm接收到CGI参数和php脚本路径后,调度一个fastCGI进程,将CGI参数设置到进程环境变量

4.执行相应的php脚本,运行后php-fpm得到输出内容

5.php-fpm将输出内容传回nginx

6.nginx响应http响应报文



配置示例

1.php-fpm.conf里[www]中listen的配置:

[www]

;listen = 127.0.0.1:9000

;本地通信可使用IPC Socket作优化

listen=/dev/shm/php_fpm.sock


2.nginx.conf里http节点里的server节点配置:

2.a 无负载均衡配置:

server {

    listen       80;

    server_name  www.xxx.com;


    #charset koi8-r;

    #access_log  /var/log/nginx/log/host.access.log  main;


    location / {

        root   /usr/share/nginx/html;

        index  index.html index.htm;

    }


    #'~'表示进行正则表达式匹配URL-PATH

    #图片访问配置在本地

    location ~ \.(gif|jpg|png)$ {

        root /usr/share/nginx/img;

    }


    location ~ \.php$ {

        root   /usr/share/nginx/php;

        #本地通信可使用IPC Socket作优化

        fastcgi_pass   unix:/dev/shm/php_fpm.sock;

        #fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        #指定php脚本的路径.如访问"http://www.xxx.com/test.php",则会执行脚本"/usr/share/nginx/php/test.php"

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        #这里可以自定义一些CGI进程环境变量,php中可通过$_SERVER['USER_DEFINE']获得该值

        fastcgi_param  USER_DEFINE RdaOniCelK;

        #定义CGI所需的变量

        include        fastcgi_params;

    }

}


#配置二级域名的示例

server {

    listen       80;

    server_name  admin.xxx.com;


    location / {

        root   /usr/share/nginx/admin/html;

        index  index.html index.htm;

    }

}

2.b 负载均衡配置

http{

    #负载节点集

    upstream myapp1 {

        #默认为round-robin负载策略,即顺序循环

        #最少连接负载策略:

        #least_conn;

        #ip哈希负载策略(session保持):

        #ip_hash;

        #固定权重负载策略:

        #server srv1.example.com weight=3;

        server srv2.example.com;

        server srv3.example.com;

    }


    server {

        listen 80;

        location / {

            #负载到节点集myapp1

            proxy_pass http://myapp1;

        }

    }

}



0 0
原创粉丝点击