一、ngx_http_fastcgi_module模块:
- ngx_http_fastcgi_module模块允许向FASCGI服务器传递请求。
1、fastcgi_pass address;
- address地址为fastcgi服务器的地址; location, if in location;
例如:http://www.ilinux.io/admin/index.php --> /admin/index.php (uri)
/data/application/admin/index.php
2、fastcgi_index name;
- fastcgi默认的主页资源;
3、fastcgi_param parameter value [if_not_empty];
- 设置应该传递给FASCGI服务器的参数。该值可以包含文本、变量以及它们的组合。
配置示例1:
前提:配置好fpm server和mariadb-server服务;
location ~* .php{ root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/htmlfastcgi_script_name;
include fastcgi_params;
}
配置示例2:通过/pm_status和/ping来获取fpm server状态信息;
location ~* ^/(pm_status|ping){ include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAMEfastcgi_script_name;
}
4、fastcgi_cache_path
- 定义fastcgi的缓存;缓存位置为磁盘上的文件系统,由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];
levels=levels:缓存目录的层级数量,以及每一级的目录数量;levels=ONE:TWO:THREE
leves=1:2:2
keys_zone=name:size
k/v映射的内存空间的名称及大小
inactive=time
非活动时长
max_size=size
磁盘上用于缓存数据的缓存空间上限
5、fastcgi_cache zone | off;
- 调用指定的缓存空间来缓存数据;http, server, location
6、fastcgi_cache_key string;
- 定义用作缓存项的key的字符串;
7、fastcgi_cache_methods GET | HEAD | POST ...;
- 为哪些请求方法使用缓存;
8、fastcgi_cache_min_uses number;
- 缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项;
9、fastcgi_cache_valid [code ...] time;
不同的响应码各自的缓存时长;
示例:
http {
...
fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20m inactive=120s;
...
server {
...
location ~* \.php$ {
...
fastcgi_cache fcgi;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
...
}
...
}
...
}
10、fastcgi_keep_conn on | off;
默认情况下,FASTCGI服务器将在发送响应后关闭连接。但是,当这个指令设置为ON值时,NGIX将指示FASCGI服务器保持连接打开。
二、搭建LANMP示例
- rs1服务器设置
[root@rs1 ~]# yum -y install php-mysql php-fpm php-mbstring php-mcrypt mariadb-server
[root@rs1 ~]# cd /etc/php-fpm.d/
[root@rs1 php-fpm.d]# vim www.conf
listen = 0.0.0.0:9000#监听所有ip的9000端口
;listen.allowed_clients = 127.0.0.1#把他注释掉,标识监听所有客户端
user = apache#因为没有安装nginx,所以不用改
group = apache
pm = dynamic#动态资源
pm.max_children =150
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.status_path = /status#信息页
ping.path = /ping#健康状态监测
php_value[session.save_path] = /var/lib/php/session#创建此目录,并赋予Apache属主组织权限
[root@rs1 ~]# mkdir /var/lib/php/session -pv#创建session目录
mkdir: 已创建目录 "/var/lib/php/session"
[root@rs1 ~]# chown apache:apache /var/lib/php/session #设置属主属组
[root@rs1 ~]# mkdir /data/apps -pv
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/apps"
[root@rs1 ~]# vim /data/apps/index.php#编辑PHP信息页
[root@rs1 ~]# systemctl start php-fpm.service#启动PHP
[root@rs1 ~]# ss -tnl#查询监听端口
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:9000 *:*
[root@rs1 ~]#wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz#下载phpMyAdmin
[root@rs1 ~]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz -C /data/apps#解压缩到指定目录
[root@rs1 ~]# cd /data/apps
[root@apps ~]# ln -sv phpMyAdmin-4.0.10.20-all-languages pma#设置别名为pma
[root@apps ~]# cp config.sample.inc.php config.inc.php#备份phpMyAdmin原配置文件
[root@apps ~]# vim config.inc.php#编辑phpMyAdmin配置文件
...
*/
$cfg['blowfish_secret'] = '点击几下键盘添加随机数'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
...
[root@rs1~]# scp phpMyAdmin-4.0.10.20-all-languages.tar.gz 192.168.1.6:/root#拷贝到vs服务器
[root@rs1~]# scp phpMyAdmin-4.0.10.20-all-languages.tar.gz 192.168.1.12:/root#拷贝到rs2服务器
[root@rs1~]# vim /etc/my.cnf.d/server.cnf#编辑mariadb配置文件
[mysqld]
skip_name_resolve=ON#关闭地址解析
innodb_file_per_table=ON
[root@rs1 ~]# systemctl start mariadb#启动mariadb服务
[root@rs1 ~]# ss -tnl #查询端口
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
- rs2服务器设置
[root@rs2 ~]# yum -y install nginx #安装nginx
[root@rs2 ~]# mkdir /data/nginx/html -pv #创建目录
[root@rs2 ~]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz -C /data/nginx/html/ #解压缩phpMyAdmin
[root@rs2 ~]# cd /data/nginx/html
[root@rs2 html]# ln -sv phpMyAdmin-4.0.10.20-all-languages/ pma
"pma" -> "phpMyAdmin-4.0.10.20-all-languages/" #设置别名为pma
[root@rs2 ~]# vim /etc/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /data/nginx/html;
[root@rs2 ~]# nginx -t#语法检查
[root@rs2 ~]# systemctl start nginx启动nginx
nginx代理服务器
[root@vs ~]# vim /etc/nginx/conf.d/ilinux.conf
server {
listen 80;#监听端口
server_name www.ilinux.io;#服务名称
index index.php index.html; #
location / {
root /data/nginx/html;
proxy_pass http://192.168.1.12:80;#传递到后端rs2主机
}
location ~* \.php$ { #动态匹配.php
fastcgi_pass 192.168.1.11:9000;#传递到后端rs1主机
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/apps/$fastcgi_script_name;#设置后端网页根目录所在位置
include fastcgi_params;#其他设置都包含在从此文件中
fastcgi_keep_conn on;#启用缓存保持连接
fastcgi_cache fcache;
fastcgi_cache_key $request_uri;#以uri当做键
fastcgi_cache_valid 200 302 10m;#指定的响应码对应缓存时间
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
}
location ~* ^/(status|ping)$ { #设置可以ping后端主机
include fastcgi_params;
fastcgi_pass 192.168.1.11:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
}
[root@vs ~]# vim /etc/nginx/nginx.conf
....
http {
.....
include /etc/nginx/mime.types;
default_type application/octet-stream;
proxy_cache_path /data/nginx/cache levels=1:1:1 keys_zone=pcache:10m max_size=2g;#定义缓存
fastcgi_cache_path /data/nginx/fcgicache levels=2:2:2 keys_zone=fcache:10m max_size=2g;#定义fcgi类型缓存
[root@vs ~]# nginx -t
[root@vs ~]# systemctl start nginx
[root@vs ~]# mkdir /data/nginx/html -pv #创建目录
[root@vs ~]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz -C /data/nginx/html/ #解压缩phpMyAdmin
[root@vs ~]# cd /data/nginx/html
[root@vs html]# ln -sv phpMyAdmin-4.0.10.20-all-languages/ pma
"pma" -> "phpMyAdmin-4.0.10.20-all-languages/" #设置别名为pma
测试
静态访问nginx服务器响应:
动态访问后端服务器响应:
正常的压力测试:
[root@mysql ~]# ab -c 100 -n 2000 http://www.ilinux.io/index.php
....
Requests per second: 477.21 [#/sec] (mean)
......
启用缓存后压力测试:
[root@mysql ~]# ab -c 100 -n 2000 http://www.ilinux.io/index.php
.......
Requests per second: 2162.90 [#/sec] (mean)#启用缓存后
......
启用保持连接后压力测试:
[root@mysql ~]# ab -c 100 -n 2000 http://www.ilinux.io/index.php
.......
Requests per second: 2481.65 [#/sec] (mean)#启用缓存后
......
ping检查:

status信息检查:
