目录
反向代理(reverse proxy),指的是代理外⽹⽤户的请求到内部的指定web服务器,并将数据返回给⽤户的⼀种⽅式,这是⽤的⽐较多的⼀种⽅式。
Nginx除了可以在企业提供⾼性能的web服务之外,另外还可以将本身不具备的请求通过某种预定义的协议转发⾄其它服务器处理,不同的协议就是Nginx服务器与其他服务器进⾏通信的⼀种规范,主要在不同的场景使⽤以下模块实现不同的功能。
ngx_http_proxy_module:将客户端的请求以http协议转发⾄指定服务器进⾏处理。
ngx_stream_proxy_module:将客户端的请求以tcp协议转发⾄指定服务器处理。
ngx_http_fastcgi_module:将客户端对php的请求以fastcgi协议转发⾄指定服务器助理。
ngx_http_uwsgi_module:将客户端对Python的请求以uwsgi协议转发⾄指定服务器处理。
一、反向代理配置参数
Syntax:proxy_pass URL;
Default:—
Context:location, if in location, limit_except
proxy_pass; #⽤来设置将客户端请求转发给的后端服务器的主机,可以是主机名、IP地址:端⼝的⽅式,也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module⽀持。
客户端访问IP/web/1.jpg
#不带斜线,实际访问http://10.0.0.21:80/web/1.jpg(一般采用不加斜线)
location /web {
proxy_pass http://10.0.0.21:80;
}
#带斜线,实际访问http://10.0.0.21:80/1.jpg
location /web {
proxy_pass http://10.0.0.21:80/;
}
Syntax:proxy_hide_header field;
Default:—
Context:http, server, location
proxy_hide_header field; #⽤于nginx作为反向代理的时候,在返回给客户端http响应的时候,隐藏后端服务版本相应头部的信息
#Nginx代理服务器向浏览器客户端发送http响应报文的header的时候,不包含Etag字段
location /web {
index index.html;
proxy_pass http://10.0.0.11:80/;
proxy_hide_header ETag;
}
Etag是web服务器的静态文件的一个值,该字段会随着资源的变化而变化。
浏览器会根据Etag进行判断,静态资源是否发生改变,如果未发生改变,则使用缓存。如果发生改变,则重新向服务器发送新资源的请求。
客户端访问反向代理服务器获得的Etag值与客户端访问后端服务器获得的Etag值一样。
Syntax:proxy_pass_header field;
Default:—
Context:http, server, location
proxy_pass_header field; #默认nginx在响应报⽂中不传递后端服务器的⾸部字段Date,Server,X-Pad,X-Accel等参数,如果要传递的话则要使⽤proxy_pass_header field声明将后端服务器返回的值传递给客户端。
#客户端收到的http响应报头中含有server字段
location /web {
index index.html;
proxy_pass http://10.0.0.11:80/;
proxy_pass_header Server;
}
Syntax:proxy_pass_request_body on | off;
Default:proxy_pass_request_body on;
Context:http, server, location
proxy_pass_request_body on | off; #是否向后端服务器发送HTTP包体部分,可以设置在http/server或location块,默认即为开启
#默认开启,不要关闭。
Syntax:proxy_pass_request_headers on | off;
Default:proxy_pass_request_headers on;
Context:http, server, location
proxy_pass_request_headers on | off; #是否将客户端的请求头部转发给后端服务器,可以设置在http/server或location块,默认即为开启
#默认开启,不要关闭。
Syntax:proxy_set_header field value;
Default:proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context:http, server, location
proxy_set_header; #可以更改或添加客户端的请求头部信息内容并转发⾄后端服务器,⽐如在后端服务器想要获取客户端的真实IP的时候,就要更改每⼀个报⽂的头部。
#透传http客户端真实IP
location /web {
index index.html;
proxy_pass http://10.0.0.11:80/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
proxy_set_header X-Forwarded-For $remote_addr; #同等与上面配置$proxy_add_x_forwarded_for = $remote_addr
虽然进行真实IP地址透传,后端Nginx服务器需要修改日志文件对$X-Forwarded-For进行记录
Syntax:proxy_connect_timeout time; #配置nginx服务器与后端服务器尝试建⽴连接的超时时间,默认为60秒
Default:proxy_connect_timeout 60s;
Context:http, server, location
Syntax:proxy_read_timeout time;
Default:proxy_read_timeout 60s;
Context:http, server, location
proxy_read_time time; #新版本为 proxy_read_timeout time;
配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时时间,默认60s
后端服务器响应超时时间
Syntax:proxy_send_timeout time;
Default:proxy_send_timeout 60s;
Context:http, server, location
proxy_send_time time; #新版本 proxy_send_timeout time;
配置nginx项后端服务器或服务器组发起write请求后,等待的超时时间,默认60s
如果后端服务器是java php处理时间可能会长一点,尽量长一点
Syntax:proxy_http_version 1.0 | 1.1;
Default:proxy_http_version 1.0;
Context:http, server, location
This directive appeared in version 1.1.4.
proxy_http_version 1.0; #⽤于设置nginx提供代理服务的HTTP协议的版本,默认http 1.0
根据后端服务器支持的http版本进行指定
Syntax:proxy_ignore_client_abort on | off;
Default:proxy_ignore_client_abort off;
Context:http, server, location
proxy_ignore_client_abort off; #当客户端⽹络中断请求时,nginx服务器中断其对后端服务器的请求。
如果此项设置为on开启,则服务器会忽略客户端中断,一直等待后端服务器返回结果。
如果设置为off,则客户端中断后Nginx也会中断客户端请求并⽴即记录499⽇志。
Syntax:proxy_headers_hash_bucket_size size;
Default:proxy_headers_hash_bucket_size 64;
Context:http, server, location
proxy_headers_hash_bucket_size 128; #当配置了proxy_hide_header和proxy_set_header的时候,⽤于设置nginx保存HTTP报⽂头的hash表的上限。
其实就是使用hash表来保存你设置的信息,这个值可以设置相对高一些
Syntax:proxy_headers_hash_max_size size; #设置proxy_headers_hash_bucket_size的最⼤可⽤空间
Default:proxy_headers_hash_max_size 512;
Context:http, server, location
Syntax:server_names_hash_bucket_size size; #server_name hash表申请空间⼤⼩
Default:server_names_hash_bucket_size 32|64|128;
Context:http
Syntax:server_names_hash_max_size size; #设置服务器名称hash表的上限⼤⼩
Default:server_names_hash_max_size 512;
Context:http
二、实现 Nginx HTTP 反向代理
2.1 案例:后端单台服务器
server {
listen 80;
server_name www.lck.net;
location / {
index index.html;
proxy_pass http://10.0.0.11:80;
}
}
2.2 案例:指定location转发后端服务器
server {
listen 80;
server_name www.lck.net;
location /web {
index index.html;
proxy_pass http://10.0.0.13:80;
}
}
一般企业上Nginx服务器都是静态资源默认访问本身服务器,只有指定资源才转发到指定后端服务器。
2.3 案例:Nginx实现代理缓存功能
2.3.1 缓存功能配置参数
Syntax:proxy_cache zone | off;
Default:proxy_cache off;
Context:http, server, location
#指定调用缓存的区域,zone需要单独去指定,在选择调用。
Syntax:proxy_cache_key string;
Default:proxy_cache_key $scheme$proxy_host$request_uri;
Context:http, server, location
#用于缓存“键”的内容,$scheme$proxy_host$request_uri,协议 主机名 URI
Syntax:proxy_cache_valid [code ...] time;
Default:—
Context:http, server, location
#对特定响应码的响应内容的缓存时长
proxy_cache_valid 200 302 10m; #对200 302可以缓存时间长一些
proxy_cache_valid 404 1m; #对404 500可以缓存时间短一些
Syntax:proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [min_free=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];
Default:—
Context:http
示例:在http作用域定义http代理缓存
proxy_cache_path /var/cache/nginx/proxy_cache #定义缓存保存路径,proxy_cache会⾃动创建
levels=1:2:2 #定义缓存⽬录结构层次,1:2:2可以⽣成2^4x2^8x2^8=1048576个⽬录
hash计算之后 123134 89 fa f 分完级之后f/fa/89
keys_zone=proxycache:20m #指内存中缓存的⼤⼩,主要⽤于存放key和metadata(如:使⽤次数) 指定zone名称,内存中缓存20,可以大一些
inactive=120s #缓存有效时间,不要太长,不超过5min
max_size=1g #最⼤磁盘占⽤空间,磁盘存⼊⽂件内容的缓存空间最⼤值,10g
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
Syntax:proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...;
Default:proxy_cache_use_stale off;
Context:http, server, location
#在被代理的后端服务器出现哪种情况下,可直接使⽤过期的缓存响应客户端
proxy_cache_use_stale error http_502 http_503;
Syntax:proxy_cache_methods GET | HEAD | POST ...;
Default:proxy_cache_methods GET HEAD;
Context:http, server, location
#对哪些客户端请求⽅法对应的响应进⾏缓存,GET和HEAD⽅法总是默认被缓存,不需要配此选项
最终缓存是以 key为proxy_cache_key指定的键,value为缓存的内容。
一般只缓存读请求,写请求不进行缓存。
2.3.2 实现缓存功能
如果后端进行功能升级,用户访问未生效,可能是配置了nginx缓存功能,这时,需要重启nginx将缓存进行清理。
#在http作用域定义缓存模板信息,配置在nginx.conf http配置段
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
#实现缓存功能
location /web {
index index.html;
proxy_pass http://10.0.0.11:80/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
}
通过压力测试来测试缓存功能是否生效,建议配置缓存前测试一次,配置缓存后测试一次。
ab -n 2000 -c200 http://www.lck.net/web/log.html
2.4 案例:在响应报文的头部报文添加header信息
如上图,我们得知可以在nginx上配置的只有第二和第四步骤,所以,我们来介绍以下如何添加header字段返回给客户端。
2.4.1 添加header信息配置参数
Syntax:add_header name value [always];
Default:—
Context:http, server, location, if in location
add_header name value [always];
add_header X-Via $server_addr; #返回客户端的响应报文中 返回后端服务器的IP地址
add_header X-Cache $upstream_cache_status; #是否命中缓存 MISS表示没命中,HIT表示命中
add_header X-Accel $server_name;
add_trailer name value [always]; #添加⾃定义响应信息的尾部, 1.13.2版后⽀持
2.4.2 实现添加head信息
location /web {
index index.html;
proxy_pass http://10.0.0.11:80/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;
}
命令行测试 curl -I URL 或者火狐浏览器测试,查看X-Cache字段。
2.5 案例:后端多台服务器
2.5.1 http upstream 配置参数
模块:ngx_http_upstream_module
Syntax:upstream name { ... }
Default:—
Context:http
#⾃定义⼀组服务器,配置在http内,默认轮询转发
Syntax:server address [parameters];
Default:—
Context:upstream
address:地址可以指定为带有可选端口的域名或IP地址,也可以指定为UNIX域套接字路径。
#可以设置以下参数:
weight=number #默认情况下,设置服务器的权重1。
max_conns=number #给当前server设置最⼤活动链接数,默认为0表示没有限制。
fail_timeout=time #指定时间内与后端服务器连接超时,视为服务器不可用。
max_fails=number #在fail_timeout时间对后端服务器连续监测失败多少次就标记为不可⽤。
backup #设置为备份服务器,当所有服务器不可⽤时将重新启⽤次服务器。一般作为报错服务器。
down #标记为down状态。
resolve #当server定义的是主机名的时候,当A记录发⽣变化会⾃动应⽤新IP⽽不⽤重启Nginx。
Syntax:proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default:proxy_next_upstream error timeout;
Context:http, server, location
#指定在哪种检测状态下将请求转发器其他服务器。
proxy_next_upstream=error timeout;
Syntax:hash key [consistent];
Default:—
Context:upstream
#基于指定key做hash计算,使⽤consistent参数,将使⽤ketama⼀致性hash算法,适⽤于后端是Cache服务器(如varnish)时使⽤,consistent定义使⽤⼀致性hash运算,⼀致性hash基于取模运算。
hash $request_uri consistent; #基于⽤户请求的uri做hash,对同一个请求转发给同一个后端服务器
Syntax:ip_hash;
Default:—
Context:upstream
#源地址hash调度⽅法,基于的客户端的remote_addr(源地址)做hash计算,以实现会话保持。
Syntax:least_conn;
Default:—
Context:upstream
#最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器
2.5.2 实现后端多台服务器
#10.0.0.11 apache服务器上
echo "10.0.0.11 web" > /var/www/html/web/index.html
#10.0.0.12 apache服务器上
echo "10.0.0.12 web" > /var/www/html/web/index.html
http {
upstream webserver {
server 10.0.0.11:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.12:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.13:80 weight=1 fail_timeout=15s max_fails=3 backup;
}
server {
listen 80;
server_name www.lck.net;
location /web {
index index.html;
proxy_pass http://webserver;
}
}
}
浏览器访问http://www.lck.net/web/index.html测试
命令行测试curl http://www.lck.net/web/index.html
2.5.3 基于http负载均衡的调度算法
#基于$request_uri做hash计算的调度算法,$request_uri也可以是其它变量
upstream webserver {
hash $request_uri consistent;
server 10.0.0.11:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.12:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.13:80 weight=1 fail_timeout=15s max_fails=3 backup;
}
#基于http客户端源IP的hash计算
upstream webserver {
ip_hash;
server 10.0.0.11:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.12:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.13:80 weight=1 fail_timeout=15s max_fails=3 backup;
}
#调度至少连接数的服务器
upstream webserver {
least_conn;
server 10.0.0.11:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.12:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.13:80 weight=1 fail_timeout=15s max_fails=3 backup;
}
#默认采用轮询算法,通过修改weight,可以变成权重轮询调度
upstream webserver {
server 10.0.0.11:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.12:80 weight=2 fail_timeout=15s max_fails=3;
server 10.0.0.13:80 weight=1 fail_timeout=15s max_fails=3 backup;
}
在github上有插件支持更多的nginx负载均衡的调度算法。
2.5.4 透传客户端真实IP
http {
upstream webserver {
server 10.0.0.11:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.12:80 weight=1 fail_timeout=15s max_fails=3;
server 10.0.0.13:80 weight=1 fail_timeout=15s max_fails=3 backup;
}
server {
listen 80;
server_name www.lck.net;
location /web {
index index.html;
proxy_pass http://webserver;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
修改后端服务器日志类型
Apache:
vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"% {User-Agent}i\"" combined
Nginx:
cat /apps/nginx/conf/nginx.conf
"$http_x_forwarded_for"' #默认⽇志格式就有此配置
三、实现 Nginx TCP 负载均衡
Nginx在1.9.0版本开始⽀持tcp模式的负载均衡,在1.9.13版本开始⽀持udp协议的负载,udp主要⽤于DNS的域名解析。
一般不用Nginx做TCP负载均衡,要的话用LVS或HAproxy。
3.1 Nginx TCP 负载均衡配置参数
模块:ngx_stream_proxy_module #实现tcp负载
模块:ngx_stream_upstream_module #实现后端服务器分组转发、权重分配、状态监测、调度算法等⾼级功
Syntax:stream { ... }
Default:—
Context:main
3.2 实现 redis 负载均衡
实验环境准备:10.0.0.11、10.0.0.12作为redis服务器、10.0.0.100作为Nginx负载均衡设备
yum install redis -y
vim /etc/redis.conf #修改服务监听IP
bind 0.0.0.0
systemctl start redis
vim /apps/nginx/conf/tcp/tcp_redis.conf
stream {
upstream redis_server {
server 10.0.0.11:6379 max_fails=3 fail_timeout=30s;
server 10.0.0.12:6379 max_fails=3 fail_timeout=30s;
}
server {
listen 10.0.0.100:6379;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass redis_server;
}
}
vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf/tcp/tcp_redis.conf; #注意此处的include与http模块平级
3.3 实现 MySQL 负载均衡
实验环境准备:10.0.0.11、10.0.0.12作为MySQL服务器、10.0.0.100作为Nginx负载均衡设备
yum install mariadb mariadb-server -y
systemctl start mariadb
mysql_secure_installation #安全初始化
systemctl enable mariadb
mysql -uroot -p123456 #尝试登陆
vim /apps/nginx/conf/tcp/tcp_mysql.conf
stream {
upstream mysql_server {
server 10.0.0.11:3306 max_fails=3 fail_timeout=30s;
server 10.0.0.12:3306 max_fails=3 fail_timeout=30s;
}
server {
listen 10.0.0.100:3306;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass mysql_server;
}
}
vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf/tcp/tcp_mysql.conf; #注意此处的include与http模块平级
四、实现 Nginx fastCGI 反向代理
4.1 CGI、fastCGI、PHP-FMP
CGI:Common Gateway Interface 公共网关接口
CGI 在2000年或更早的时候用得比较多,以前web服务器一般只处理静态的请求,如果碰到一个动态请求怎么办呢?web服务器会根据这次请求的内容,然后会 fork 一个新进程来运行外部的 C 程序或者bash,perl脚本等,这个进程会把处理完的数据返回给web服务器,最后web服务器把内容发送给用户,刚才fork的进程也随之退出。 如果下次用户还请求改动态脚本,那么web服务器又再次fork一个新进程,周而复始的进行。(缺点:用完进程之后随之丢弃)
fastcgi的方式是,web服务器收到一个请求时,不会重新fork一个进程(因为这个进程在web服务器启动时就开启了,而且不会退出),web服务器直接把内容传递给这个进程(进程间通信,但fastcgi使用了别的方式,tcp方式通信),这个进程收到请求后进行处理,把结果返回给web服务器,最后自己接着等待下一个请求的到来,而不是退出。(优点:用完进程之后不会丢弃)
PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是⼀个实现了Fastcgi的管理程序,并且提供进程管理的功能,进程包括master进程和worker进程,master进程只有⼀个,负责监听端⼝,接受来⾃web server的请求。worker进程⼀般会有多个,每个进程中会嵌⼊⼀个PHP解析器,进⾏PHP代码的处理。
4.2 Nginx fastCGI 反向代理配置参数
模块:ngx_http_fastcgi_module
Syntax:fastcgi_pass address;
Default:—
Context:location, if in location
fastcgi_pass localhost:9000 #前后不加协议,如果是http反向代理,就有http://
fastcgi_pass 127.0.0.1:9000 #转发本机
fastcgi_pass 10.0.0.11:9000 #转发后端fastCGI服务器
Syntax:fastcgi_index name;
Default:—
Context:http, server, location
#fastcgi默认的主⻚资源,一定要定义,不定义会报错
fastcgi_index index.php;
Syntax:fastcgi_param parameter value [if_not_empty];
Default:—
Context:http, server, locatio
#设置传递给FastCGI服务器的参数值,可以是⽂本,变量或组合,可⽤于将Nginx的内置变量赋值给⾃定义key
fastcgi_param REMOTE_ADDR $remote_addr; #客户端源IP
fastcgi_param REMOTE_PORT $remote_port; #客户端源端⼝
fastcgi_param SERVER_ADDR $server_addr; #请求的服务器IP地址
fastcgi_param SERVER_PORT $server_port; #请求的服务器端⼝
fastcgi_param SERVER_NAME $server_name; #请求的server name
缓存的定义
Syntax:fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [min_free=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];
Default:—
Context:http
#示例:在http作用域定义fastCGI缓存
path #缓存位置为磁盘上的⽂件系统路径
max_size=size #磁盘path路径中⽤于缓存数据的缓存空间上限
levels=levels #⼗六进制的缓存⽬录的层级数量,以及每⼀级的⽬录数量,levels=ONE:TWO:THREE,示例:leves=1:2:2
keys_zone=name:size #设置缓存名称及k/v映射的内存空间的名称及⼤⼩
inactive=time #缓存有效时间,默认10分钟,需要在指定时间满⾜fastcgi_cache_min_uses 次数被视为活动缓存。
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
缓存的调用
Syntax:fastcgi_cache zone | off;
Default:fastcgi_cache off;
Context:http, server, location
#调⽤指定的缓存空间来缓存数据
Syntax:fastcgi_cache_key string;
Default:—
Context:http, server, location
#定义⽤作缓存项的key的字符串
fastcgi_cache_key $request_uri; #对用户请求的URI做缓存
Syntax:fastcgi_cache_methods GET | HEAD | POST ...;
Default:fastcgi_cache_methods GET HEAD;
Context:http, server, location
#为哪些请求⽅法使⽤缓存,通常做GET和HEAD缓存
Syntax:fastcgi_cache_min_uses number;
Default:fastcgi_cache_min_uses 1;
Context:http, server, location
#缓存空间中的缓存项在inactive定义的⾮活动时间内⾄少要被访问到此处所指定的次数⽅可被认作活动项
Syntax:fastcgi_keep_conn on | off;
Default:fastcgi_keep_conn off;
Context:http, server, location
#收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启⽤⻓连接
fastcgi_keep_conn on;
Syntax:fastcgi_cache_valid [code ...] time;
Default:—
Context:http, server, location
#不同的响应码各⾃的缓存时⻓
Syntax:fastcgi_hide_header field;
Default:—
Context:http, server, location
#隐藏响应头指定信息
Syntax:fastcgi_pass_header field;
Default:—
Context:http, server, location
#返回响应头指定信息,默认不会将Status、X-Accel-...返回
最终缓存是以 key为fastcgi_cache_key指定的键,value为缓存的内容。
一般只缓存读请求,写请求不进行缓存。
4.3 案例:fastCGI与Nginx在同一台服务器
4.3.1 PHP环境准备
#yum安装php
yum install php-fpm php-mysql -y
systemctl start php-fpm && systemctl enable php-fpm
ps -ef | grep php-fpm
#一般同一个业务的所有服务都要是同一个账号(同一个UID)启动:使用ansible推送或者虚拟机模板或者手动创建
#/etc/php-fpm.conf该配置文件一般不需要修改,只需要改/etc/php-fpm.d/www.conf
cat /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000 #监听地址及IP,如果Nginx和php不在同一个服务器需要更改
listen.allowed_clients = 127.0.0.1 #允许客户端从哪个源IP地址访问,要允许所有⾏⾸加;注释即可
user = nginx #php-fpm启动的⽤户和组,会涉及到后期⽂件的权限问题,所有业务的账号要一致
group = nginx
pm = dynamic #动态模式进程管理
pm.max_children = 500 #静态⽅式下开启的php-fpm进程数量,在动态⽅式下他限定php-fpm的最⼤进程数
pm.start_servers = 100 #动态模式下初始进程数,必须⼤于等于pm.min_spare_servers和⼩于等于pm.max_children的值。
pm.min_spare_servers = 100 #最⼩空闲进程数
pm.max_spare_servers = 200 #最⼤空闲进程数
pm.max_requests = 500000 #进程累计请求回收值,会回收并重新⽣成新的⼦进程
pm.status_path = /pm_status #状态访问URL
ping.path = /ping #ping访问动地址
ping.response = ping-pong #ping返回值
slowlog = /var/log/php-fpm/www-slow.log #慢⽇志路径
php_admin_value[error_log] = /var/log/php-fpm/www-error.log #错误⽇志
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files #phpsession保存⽅式及路径,如果是redis需要更改
php_value[session.save_path] = /var/lib/php/session #当时使⽤file保存session的⽂件路径,如果是redis需要更改
#重启PHP服务
systemctl restart php-fpm
#查看启用php进程的用户是不是nginx
ps -ef | grep php
#准备php测试⻚⾯
mkdir /data/nginx/php
cat /data/nginx/php/index.php
<?php
phpinfo();
?>
4.3.2 Nginx配置fastCGI反向代理
location ~ \.php$ {
root /data/nginx/php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /data/nginx/php$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#重启Nginx并访问web测试
systemctl restart nginx
其中变量$document_root就是root,$fastcgi_script_name就是用户请求的URI。
访问测试www.lck.net/index.php
fastcgi_pass常⻅的错误:File not found 路径不对。可能是fastcgi_param写错 502:php-fpm处理超时、服务停⽌运⾏等原因导致的⽆法连接或请求超时,可能是fastcgi_pass写错。
4.4 案例:fastCGI与Nginx不在同一台服务器
nginx服务器IP:10.0.0.11,php服务器账号:10.0.0.12
4.4.1 yum安装新版本PHP-FMP
#在/etc/yum.repos.d/目录下生成php的新版yum源
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
#查看具体有哪些版本可以安装
yum list *-php-fpm*
#安装指定版本的php
yum install php72-php-fpm php72-php-mysql -y
#验证安装路径
rpm -ql php72-php-fpm
/etc/logrotate.d/php72-php-fpm
/etc/opt/remi/php72/php-fpm.conf
/etc/opt/remi/php72/php-fpm.d
/etc/opt/remi/php72/php-fpm.d/www.conf
/etc/opt/remi/php72/sysconfig/php-fpm
/etc/systemd/system/php72-php-fpm.service.d
/opt/remi/php72/root/usr/sbin/php-fpm
。。。。。
/usr/lib/systemd/system/php72-php-fpm.service
#⽣成php72配置⽂件
cp /opt/remi/php72/root/usr/share/doc/php72-php-common-7.2.24/php.ini-production /opt/remi/php72/root/usr/etc/php.ini
cp /opt/remi/php72/root/usr/share/doc/php72-php-fpm-7.2.24/php-fpm.conf.default /opt/remi/php72/root/usr/etc//php-fpm.conf #include=/etc/opt/remi/php72/php-fpm.d/*.conf
4.4.2 准备PHP-FMP环境
#创建账号
useradd nginx
#修改/etc/opt/remi/php72/php-fpm.d/*.conf配置文件
vim /etc/opt/remi/php72/php-fpm.d/www.conf
user = nginx
group = nginx
listen = 10.0.0.12:9000
listen.allowed_clients = 10.0.0.11
#准备php测试⻚⾯
mkdir /data/nginx/php
cat /data/nginx/php/index.php
<?php
phpinfo();
?>
#重启PHP服务
systemctl restart php-fpm
#查看启用php进程的用户是不是nginx
ps -ef | grep php
4.4.3 Nginx配置fastCGI反向代理
location ~ \.php$ {
root /data/nginx/php;
fastcgi_pass 10.0.0.12:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /data/nginx/php$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#重启Nginx并访问web测试
systemctl restart nginx