常青树 发表于 2018-11-14 08:31:29

Nginx 之 fastcgi配置

  1概述
  nginx代理通过ngx_http_fastcgi_module这个模块,将收到php程序的请求后就转发到后台FastCGI服务器处理,这里nginx可以把php-fpm服务运行在同一机器上,也可以将nginx和php-fpm分离在两台机器上。但是,nginx不支持php模块方式,只能是php-fpm模式。
  本文将介绍ngx_http_fastcgi_module模块的相关命令和配置。
  
  2fastcgi配置
  
  .1、fastcgi_pass
  fastcgi_passaddress;
  address为后端的fastcgiserver的地址
  可用位置:location,if in location
  .2、fastcgi_index
  fastcgi_indexname;
  fastcgi默认的主页资源
  示例:fastcgi_indexindex.php;
  .3、fastcgi_param
  fastcgi_paramparameter value ;
  设置传递给FastCGI服务器的参数值,可以是文本,变量或组合
  .示例1:将php后缀的文件调度到运行php-fpm的服务器
  .1)在后端服务器先配置fpm server和mariadb-server
yum install php-fpm mysql-server;  fastcgi服务器上配置
mv/etc/php-fpm.conf /etc/php-fpm.conf.bak  
cp /usr/share/doc/php-fpm-5.3.3/php-fpm.conf.default/etc/php-fpm.conf
  更改php的配置文件的监听端口和允许连接的ip
vim/etc/php-fpm.conf  
listen 9000
  
listen.allowed_clients = 127.0.0.1,172.18.50.73
  
service php-fpmrestart
  .2)在前端nginx服务上做以下配置:
  注意,以下的/app/php是指在安装php-fpm主机上存放php文件的路径,这里就是在172.18.50.65这台主机下的路径/app/php下存放的php后缀的脚本,后端fpm服务器IP:9000;
  方法一
vim/etc/nginx/conf.d/server.conf  
    location ~ \.php$ {
  
      fastcgi_pass 172.18.50.65:9000;
  
      fastcgi_index index.php;
  
      fastcgi_param SCRIPT_FILENAME/app/php$fastcgi_script_name;
  
      include fastcgi_params;
  
    }
  方法二
  fastcgi_paramsfastcgi参数在子配置文件fastcgi.conf里定义了,所以这里有另一种配置方法,直接更改fastcgi.conf这个文件
vim /etc/nginx/fastcgi.conf  
fastcgi_param SCRIPT_FILENAME    /app/php$fastcgi_script_name;
  
vim/etc/nginx/conf.d/server.conf
  
    location ~ \.php$ {
  
      fastcgi_pass 172.18.50.65:9000;
  
      fastcgi_index index.php;
  
      includefastcgi.conf;
  
    }
  3)测试
  重启nginx服务,当访问172.18.50.73下的php文件都会被调度到172.18.50.65去处理,且对应的php文件的目录是172.18.50.65下的/app/php.
  在浏览器下输入http://172.18.50.73/index.php,通过命令tcpdump -ieth1 -nn port 9000在65主机上查看访问情况
  这里附上172.18.50.65下的/app/php/index.php的测试脚本。由于以下的脚本中,有测试服务器的连接,因此php-fpm服务器端要安装php-mysql,才能连接mysql数据库。
yuminstall php-mysql  脚本如下

  .示例2:通过/pm_status和/ping来获取fpm server状态信息
  可以通过?格式来看对应不同格式的显示。也可以是?full
  php-fpm服务器要开启如下两个配置,注意,这里斜杠后的status和ping是可以自定义,在nginx上location上配置匹配的选项即可。
  pm.status_path = /status
  ping.path = /ping
  nginx调度器端配置如下:
    location ~* ^/(status|ping)$ {  
      include fastcgi_params;
  
      fastcgi_pass 172.18.50.65:9000;
  
      fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  
   }
  .4、定义fastcgi的缓存
  这个需要在http配置段里定义
fastcgi_cache_path path   
keys_zone=name:size ;
  path 缓存位置为磁盘上的文件系统
  max_size=size
  磁盘path路径中用于缓存数据的缓存空间上限
  levels=levels:缓存目录的层级数量,以及每一级的目录数量
  levels=ONE:TWO:THREE
  示例:leves=1:2:2
  keys_zone=name:size
  k/v映射的内存空间的名称及大小
  inactive=time
  非活动时长
  .5、fastcgi_cachezone | off;
  调用指定的缓存空间来缓存数据
  可用位置:http,server, location
  .6、fastcgi_cache_key
  fastcgi_cache_keystring;
  定义用作缓存项的key的字符串
  示例:fastcgi_cache_key$request_rui;
  .7、fastcgi_cache_methods
  fastcgi_cache_methods GET| HEAD | POST ...;
  为哪些请求方法使用缓存
  .8、fastcgi_cache_min_uses
  fastcgi_cache_min_usesnumber;
  缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项
  .9、fastcgi_keep_conn
  fastcgi_keep_conn on | off;
  收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接
  .10、fastcgi_cache_valid
  fastcgi_cache_valid time;
  不同的响应码各自的缓存时长
  
  3    配置fastcgi缓存
  #http配置段里定义缓存
http {  
fastcgi_cache_path/var/cache/nginx/fcgi_cache levels=1:2:2 keys_zone=sunny_fcgicache:20m inactive=120s;
  
...
  
}
  #server 端里调用缓存
server {  
location ~* \.php$ {
  
...
  
fastcgi_cache sunny_fcgicache;
  
      fastcgi_cache_key $request_uri;
  
      fastcgi_cache_valid 200 302 10m;
  
      fastcgi_cache_valid 301 1h;
  
      fastcgi_cache_valid any 1m;...
  
}
  
}
  在浏览器里打开http://172.18.50.73/index.php第二次打开的时候,明显比第一次快,因为第一次打开的时候,还没有产生缓存


页: [1]
查看完整版本: Nginx 之 fastcgi配置