十一、ngx_http_fastcgi_module
ngx_http_fastcgi_module模块
转发请求到FastCGI服务器,不支持php模块方式
1、fastcgi_pass address;
address为后端的fastcgi server的地址可用位置:location, if in location
2、fastcgi_index name;
fastcgi默认的主页资源示例:fastcgi_index index.php;
3、fastcgi_param parameter value [if_not_empty];
设置传递给 FastCGI服务器的参数值,可以是文本,变量或组合
4、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];
定义fastcgi的缓存;path 缓存位置为磁盘上的文件系统max_size=size磁盘path路径中用于缓存数据的缓存空间上限levels=levels:缓存目录的层级数量,以及每一级的目录数量levels=ONE:TWO:THREE示例:leves=1:2:2keys_zone=name:sizek/v映射的内存空间的名称及大小inactive=time非活动时长
5、fastcgi_cache zone | off;
调用指定的缓存空间来缓存数据可用位置:http, server, location
6、fastcgi_cache_key string;
定义用作缓存项的key的字符串示例:fastcgi_cache_key $request_rui;
7、fastcgi_cache_methods GET | HEAD | POST ...;
为哪些请求方法使用缓存
8、fastcgi_cache_min_uses number;
缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项
9、fastcgi_keep_conn on | off;
收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接
10、fastcgi_cache_valid [code ...] time;
不同的响应码各自的缓存时长
举例:当访问的是pjp程序,交给fastcgi主机来处理
1)首先把fastcgi监听端口改为对外服务的ip地址
vim /etc/php-fpm.d/www.conf
listen = 9000
listen.allowed_clients =any或网段
2)修改nginx子配置文件
vim /etc/nginx/conf.d/vhosts.conf
location ~* \.php$ {
fastcgi_pass 后端fpm服务器IP:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /app/php$fastcgi_script_name;
include fastcgi_params;
}
nginx和fastcgi在一台服务器设置方法
1)前端服务器设置(调度器)
vim /etc/nginx/conf.d/vhosts.conf
location ~* \.php$ {
proxy_pass http://192.168.43.67;
}
2)后端服务器设置(nginx和fastcgi都安装好了)
虚拟主机
vim /etc/nginx/conf.d/vshost.conf
server {
。。。
location / {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /app/nginx$fastcgi_script_name;
include fastcgi_params;
}
}
3)重启服务
备注:nginx和fastcgi在同一台主机就不需要封装解封装了,直接fascgi监听在socke文件上
1)vim /etc/php-fpm.d/www.conf
listen = /run/fastcgi.sock
2)修改nginx配置文件(调用)
vim /etc/nginx/nginx.conf
location 下
fastcgi_pass unix:/run/fastcgi.sock;
重启测试
实例:fastcgi缓存
调度器主机配置
http {
fastcgi_cache_path /var/cache/nginx/fcgi_cache levels=1:2:1
keys_zone=fcgicache:20m inactive=120s;
...
server {
location ~* \.php$ {
...
fastcgi_cache fcgicache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
...
}
}