Nginx之fastcgi配置
1  概述
nginx代理通过ngx_http_fastcgi_module这个模块,将收到php程序的请求后就转发到后台FastCGI服务器处理,这⾥nginx可以把php-fpm 服务运⾏在同⼀机器上,也可以将nginx和php-fpm分离在两台机器上。但是,nginx不⽀持php模块⽅式,只能是php-fpm模式。
本⽂将介绍ngx_http_fastcgi_module模块的相关命令和配置。
2  fastcgi配置
.1、fastcgi_pass
fastcgi_pass  address;
address为后端的fastcgi  server的地址
可⽤位置:location,if in location
.2、fastcgi_index
fastcgi_index  name;
fastcgi默认的主页资源
⽰例:fastcgi_index  index.php;
.3、fastcgi_param
fastcgi_param  parameter value [if_not_empty];
设置传递给FastCGI服务器的参数值,可以是⽂本,变量或组合
.⽰例1:将php后缀的⽂件调度到运⾏php-fpm的服务器
.1)在后端服务器先配置fpm server和mariadb-server
yum install php-fpm mysql-server;
fastcgi服务器上配置
mv  /f /f.bak
cp /usr/share/doc/php-fpm-5.3.f.default  /f
更改php的配置⽂件的监听端⼝和允许连接的ip
vim  /f
listen 9000
listen.allowed_clients = 127.0.0.1,172.18.50.73
service php-fpmre  start
.2)在前端nginx服务上做以下配置:
注意,以下的/app/php是指在安装php-fpm主机上存放php⽂件的路径,这⾥就是在172.18.50.65这台主机下的路径/app/php下存放的php 后缀的脚本,后端fpm服务器IP:9000;
⽅法⼀
vim  /etc/nginx/conf.f
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_params  fastcgi参数在⼦配置⽂件f⾥定义了,所以这⾥有另⼀种配置⽅法,直接更改f这个⽂件nginx 配置文件
vim /etc/f
fastcgi_param SCRIPT_FILENAME    /app/php$fastcgi_script_name;
vim  /etc/nginx/conf.f
location ~ \.php$ {
fastcgi_pass 172.18.50.65:9000;
fastcgi_index index.php;
}
3)测试
重启nginx服务,当访问172.18.50.73下的php⽂件都会被调度到172.18.50.65去处理,且对应的php⽂件的⽬录是172.18.50.65下
的/app/php.
这⾥附上172.18.50.65下的/app/php/index.php的测试脚本。由于以下的脚本中,有测试服务器的连接,因此php-fpm服务器端要安装php-mysql,才能连接mysql数据库。
yum  install php-mysql
脚本如下
<?php
echo "I am index.php in  /app/phpwihtin 65";
$mysqli=newmysqli("172.18.50.65","wpadmin","Pass123456");
if(mysqli_connect_errno()){
echo "it is falilure!";
$mysqli=null;
exit;
}
echo "nice job,";
$mysqli->close();
phpinfo();
>
.⽰例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 [levels=levels][use_temp_path=on|off]
keys_zone=name:size [inactive=time][max_size=size] [manager_files=number][manager_sleep=time] [manager_threshold=time] [loader_files=number][loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number][purger_sleep=time] [purger_threshold=time];
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_cache  zone | off;
调⽤指定的缓存空间来缓存数据
可⽤位置:http,server, location
.6、fastcgi_cache_key
fastcgi_cache_key  string;
定义⽤作缓存项的key的字符串
⽰例:fastcgi_cache_key  $request_rui;
.7、fastcgi_cache_methods
fastcgi_cache_methods GET| HEAD | POST ...;
为哪些请求⽅法使⽤缓存
.8、fastcgi_cache_min_uses
fastcgi_cache_min_uses  number;
缓存空间中的缓存项在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;...
}
}