nginx做为一款支持高并发,并且相当轻量级的处理静态资源的开源软件。

同时也支持多种模块进行功能扩展,虽然nginx本身并不能处理动态资源,但通过fastcgi模块可以实现处理动态资源的功能




**nginx_http_fastcgi_module模块配置**


  • fastcgi_pass address;address为fastcgi server监听的地址;

  • fasgcgi_index name;定义fastcgi应用的默认主页;

  • fastcgi_param parameter value;设定传递给后端fastcgi server的参数及其值;


  • fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size];定义缓存机制,应用上下文:http


缓存机制:

    元数据:    

    数据:磁盘,即为path;

    元数据:内存,即为keys_zone;

 levels=#[:#[:#]];  例:levels=2:1

 keys_zone=name:size

    name:cache的标识符;

    size:元数据cache大小;

 max_size:缓存空间上限;


  • fastcgi_cache zone|off;调用定义过得缓存;zone即为通过fastcgi_cache_path定义缓存时keys_zone参数中的name;

  • fastcgi_cache_key string;定义如何使用缓存键;

  • fastcgi_cache_methods GET|HEAD|POST..;为何种请求方法对应的请求进行缓存,默认为GET和HEAD;

  • fastcgi_cache_valid [code..]time;对不同响应码的响应设定缓存时长;


注意:调用缓存时,应该调用指定的三个参数:

    fastcgi_cache

    fastcgi_cache_key

    fastcgi_cache_valid


fastcgi_cache_path /var/cache/fastcgi levels=1:1 keys_zone=fastcgicache:10m inactive=5m max_size=1g;               #定义fastcgi的缓存
server {
        listen       *:80;
        server_name  www.node8.com;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {                 #定义主页文件
          index index.html index.php index.htm;
        }
        location ~* ^/(p_w_picpaths|javascript|js|css|flash|media|static)/ {  #定义静态文件自己处理,过期时长为30d
          expires 30d;
        }
        location ~* \.php$ {               #将动态资源发往本机的fastcgi处理
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
         include fastcgi_params;
         fastcgi_cache fastcgicache;       #调用fastcgi的缓存;
         fastcgi_cache_key $request_uri;
         fastcgi_cache_valid 200 302 10m;
         fastcgi_cache_valid 403 1m;
        }
        error_page 404 /404.html;        
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }


**启用php-fpm的状态页面**



[root@node8 nginx]# grep "^pm.status_path" /etc/php-fpm.d/   #在php-fpm文件中启用该项 
pm.status_path = /phpfpmstatus   #状态页面url可以自己定义;
 
location /phpfpmstatus {                   #在nginx配置文件中增加此状态页面location
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
         include fastcgi_params;
        }

重启nginx和php-fpm:

wKioL1bdGOuz0sG_AAA92pQ7lmU698.png

pool-fpm:池子名称,大多数为www;

process manager:进程管理方式;

start time:启动时间;

start since:运行时长;

accepted conn:当前池子接受的请求数;

listen queue:请求等待队列,如果不为0,需要增加fpm的进程数;

max listen queue:请求等待队列最高的数量;

listen queue len:socket等待队列长度;

idle processes:空闲进程数量;

active processes:活跃进程数量;

total processes:总进程数量;

max active processes:最大的活跃进程数量,从启动开始算;

max childrwn reached:

slow requests:启用了fpm的慢日志功能,缓慢请求的数量




**基于unix套接字文件连接php-fpm**



通过监听端口的方式进行处理php请求时,转发方式为TCP协议,每次都要进行三次握手和四次断开,当nginx与php-fpm在同一台主机上时,我们可以基于本地unix套接字文件进行通信,提高传输性能;

listen = /var/run/php-fpm.sock;   #修改php-fpm的配置文件使它监听到unix socket文件;


fastcgi_pass  unix:/var/run/php-fpm.sock;  #修改nginx配置文件中的fastcgi_pass选项;