使用Nginx反向代理fastcgi
使用 ngx_http_fastcgi_module 模块实现转发请求到FastCGI服务器,nginx不支持php模块方式。
fastcgi_pass address;
address为后端的fastcgi server的地址
可用位置:location, if in location
fastcgi_index name;fastcgi默认的主页资源
示例:fastcgi_index index.php;
fastcgi_param parameter value [if_not_empty];设置传递给 FastCGI服务器的参数值,可以是文本,变量或组合
自带的系统参数存放在 /etc/nginx/fastcgi_params 中
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location ~ \.php$ { #当访问的uri以".php"结尾时匹配
fastcgi_pass 192.168.30.75:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
include fastcgi_params; #引用fastcgi_params中的变量(不可缺少)
}
location / {
proxy_pass http://192.168.30.174;
proxy_set_header X_Real_IP "jiangbowen";
proxy_cache testcache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 301 302 1h;
proxy_cache_valid any 1m;
add_header X-Cache $upstream_cache_status;
}
}
访问后端主机上的 php 页面
[root@CentOS69 ~]# curl www.linux.com/index.php
<html>
<head>
<title>PHP 测试</title>
</head>
<body>
<p>Hello World</p> </body>
</html>
实现通过 /status 和 /ping 来获取 fpm server 状态信息
需要在 fpm server 的主配置文件中开启对应功能
[root@CentOS74 ~]# cat /etc/nginx/conf.d/vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location ~ \.php$ {
fastcgi_pass 192.168.30.75:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/(status|ping)$ { #当访问/status或者/ping时匹配
include fastcgi_params;
fastcgi_pass 192.168.30.75:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
location / {
proxy_pass http://192.168.30.174;
proxy_set_header X_Real_IP "jiangbowen";
proxy_cache testcache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 301 302 1h;
proxy_cache_valid any 1m;
add_header X-Cache $upstream_cache_status;
}
}
访问 /status 和 /ping 查看 fpm server 状态信息
[root@CentOS69 ~]# curl www.linux.com/ping
pong
[root@CentOS69 ~]# curl www.linux.com/status
pool: www
process manager: dynamic
start time: 14/Jul/2018:18:35:16 +0800
start since: 868
accepted conn: 12
listen queue: 0
max listen queue: 0
listen queue len: 128
idle processes: 4
active processes: 1
total processes: 5
max active processes: 1
max children reached: 0
slow requests: 0
与反向代理相同,fastcgi 同样支持缓存,语法也类似
fastcgi_cache_path path [选项]...
定义fastcgi的缓存;
path 缓存位置为磁盘上的文件系统
max_size=size:磁盘path路径中用于缓存数据的缓存空间上限
levels=levels:缓存目录的层级数量,以及每一级的目录数量
keys_zone=name:sizek:/v映射的内存空间的名称及大小
inactive=time:非活动时长
[root@CentOS74 ~]# cat /etc/nginx/nginx.conf | grep fastcgi_cache
fastcgi_cache_path /var/cache/fcgi_cache levels=1:2:1 keys_zone=fcgicache:20m inactive=120s max_size=1g;
fastcgi_cache zone | off;
调用指定的缓存空间来缓存数据
可用位置:http, server, location
fastcgi_cache_key string;定义用作缓存项的key的字符串
示例:fastcgi_cache_key $request_rui;
fastcgi_cache_methods GET | HEAD | POST ...;为哪些请求方法使用缓存
fastcgi_cache_min_uses number;缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项
fastcgi_keep_conn on | off;收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接
fastcgi_cache_valid [code ...] time;不同的响应码各自的缓存时长
[root@CentOS74 ~]# cat /etc/nginx/conf.d/vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location ~ \.php$ {
fastcgi_pass 192.168.30.75:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
include fastcgi_params;
fastcgi_cache fcgicache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 301 302 10m;
fastcgi_cache_valid any 1m;
}
}
Nginx多主机调度
ngx_http_upstream_module模块用于将多个服务器定义成服务器组,而由proxy_pass, fastcgi_pass等指令进行引用。
upstream name { ... } 定义后端服务器组,会引入一个新的上下文
默认调度算法是wrr
Context: http
server address [特征];
在upstream上下文中server成员,以及相关的参数;Context:upstream
地址格式:unix:/PATH/TO/SOME_SOCK_FILE
IP[:PORT]
HOSTNAME[:PORT]
特征:weight=number 权重,默认为1
max_conns 连接后端报务器最大并发活动连接数,1.11.5后支持
max_fails=number 失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用,默认为1
fail_timeout=time 后端服务器标记为不可用状态的连接超时时长,默认10s
backup 将服务器标记为“备用”,即所有服务器均不可用时才启用
down 标记为“不可用”,配合ip_hash使用,实现灰度发布
[root@CentOS74 ~]# cat /etc/nginx/nginx.conf
http {
upstream static {
server 192.168.30.75:80; #后端主机与端口号
server 192.168.30.174:8000;
server 127.0.0.1:80 backup; #备份主机与端口号,在后端主机宕机后才会启用
}
}
[root@CentOS74 ~]# cat /etc/nginx/conf.d/vhost/linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location / {
proxy_pass http://static;
}
}
注意:当 server 配置段中声明了是调度器后,就不能提供 http 服务了,需要在另一个 server 配置段中设置本机的虚拟主机设置。
调度算法:Context: upstream
ip_hash 源地址hash调度方法(无法与backup同时使用)
least_conn 最少连接调度算法,当server拥有不同的权重时其为wlc,当所有后端主机连接数相同时,则使用wrr,适用于长连接
hash key [consistent] 基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者组合作用:将请求分类,同一类请求将发往同一个upstream server,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用
hash $request_uri consistent;
hash $remote_addr;
注意:当不使用 consistent 时,无法生成哈希环,当缓存服务器发生变化时,将会发生雪崩效应。
keepalive 连接数N;为每个worker进程保留的空闲的长连接数量,可节约nginx端口,并减少连接管理的消耗。建议开启