Nginx《五》——NginxNginx反向代理功能

Nginx《一》——I/O模型以及web服务介绍
Nginx《二》——Nginx基础服务介绍以及编译安装、配置
Nginx《三》——Nginx基础配置介绍
Nginx《四》——NginxNginx 高级配置

Nginx 反向代理功能

反向代理:反向代理也叫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协议转发至指定服务器处理。

逻辑调用关系:
在这里插入图片描述

生产环境部署结构:
在这里插入图片描述

实现http反向代理

要求:将用户对域 pc.likai.tech 的请求转发值后端服务器处理,官方文档: https://nginx.org/en/docs/http/
ngx_http_proxy_module.html

反向代理配置参数

环境准备:
172.20.32.110 #Nginx 代理服务器
172.20.32.120 #后端web 1,Apache2部署
172.20.32.100 #后端web 2,Apache2部署
在这里插入图片描述

部署后端Apache服务器

root@web1:~# apt  install apache2 -y
root@web1:~# echo "web1 test1" > /var/www/html/index.html

root@web1:~# vim /var/www/html/app/idex.html 
web1 app1 172.20.32.120
root@web1:~#systemctl start apache2

root@web2:~# apt  install apache2 -y
root@web2:~# echo "web2 test2" > /var/www/html/index.html

root@web2:~# vim /var/www/html/app/idex.html 
web2 app2 172.20.32.100
root@web1:~#systemctl start apache2

#访问测试
root@make-install:~# curl 172.20.32.120
web1 test1
root@make-install:~# curl 172.20.32.100
web2 test2

Nginx http 反向代理入门

官方文档:https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

proxy_pass;
#用来设置将客户端请求转发给的后端服务器的主机,可以是主机名、IP地址:端口的方式,也可以代理到预先设置的
主机群组,需要模块gx_http_upstream_module支持。
location /web {
index index.html;
proxy_pass http://192.168.7.103:80;
#不带斜线将访问的/web,等于访问后端服务器 http://192.168.7.103:80/web/index.html,即后端服务
器配置的站点根目录要有web目录才可以被访问,这是一个追加/web到后端服务器
http://servername:port/WEB/INDEX.HTML的操作
proxy_pass http://192.168.7.103:80/;
#带斜线,等于访问后端服务器的http://192.168.7.103:80/index.html 内容返回给客户端
}
#重启Nginx测试访问效果:
# curl -L http://www.magedu.net/web/index.html
proxy_hide_header field;
#用于nginx作为反向代理的时候,在返回给客户端http响应的时候,隐藏后端服务版本相应头部的信息,可以设置在
http/server或location块,
location /web {
index index.html;
proxy_pass http://192.168.7.103:80/;
proxy_hide_header ETag;
}
proxy_pass_header field;
#默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel等参数,如果要传递的
话则要使用 proxy_pass_header field声明将后端服务器返回的值传递给客户端。
proxy_pass_request_body on | off;
#是否向后端服务器发送HTTP包体部分,可以设置在http/server或location块,默认即为开启
proxy_pass_request_headers on | off;
#是否将客户端的请求头部转发给后端服务器,可以设置在http/server或location块,默认即为开启
proxy_set_header;
#可以更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实IP的时
候,就要更改每一个报文的头部,如下:
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
#添加HOST到报文头部,如果客户端为NAT上网那么其值为客户端的共用的公网IP地址,常用于在日之中记录客户端的
真实IP地址。
proxy_connect_timeout time;
#配置nginx服务器与后端服务器尝试建立连接的超时时间,默认为60秒,用法如下:
proxy_connect_timeout 60s;
#60s为自定义nginx与后端服务器建立连接的超时时间
proxy_read_time time;
#配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时时间,默认60s
proxy_send_time time;
#配置nginx项后端服务器或服务器组发起write请求后,等待的超时时间,默认60s
proxy_http_version 1.0;
#用于设置nginx提供代理服务的HTTP协议的版本,默认http 1.0
proxy_ignore_client_abort off;
#当客户端网络中断请求时,nginx服务器中断其对后端服务器的请求。即如果此项设置为on开启,则服务器会忽略客
户端中断并一直等着代理服务执行返回,如果设置为off,则客户端中断后Nginx也会中断客户端请求并立即记录499日
志,默认为off。
proxy_headers_hash_bucket_size 128;
#当配置了 proxy_hide_header和proxy_set_header的时候,用于设置nginx保存HTTP报文头的hash表的上限。
proxy_headers_hash_max_size 512;
#设置proxy_headers_hash_bucket_size的最大可用空间
server_namse_hash_bucket_size 512;
#server_name hash表申请空间大小
server_names_hash_max_szie 512;
#设置服务器名称hash表的上限大小
location /app {
   index index.html;
   proxy_pass http://172.20.32.120:80;
}

访问:
在这里插入图片描述

反向代理示例–单台web服务器

[root@s2 conf.d]# mv pc.conf pc.conf.bak0304
[root@s2 conf.d]# cp mobile.conf pc.conf
[root@s2 conf.d]# cat pc.conf
server {
listen 80;
server_name www.magedu.net;
location / {
proxy_pass http://192.168.7.103:80/;
}
}
#重启Nginx 并访问测试

反向代理示例–指定location

server {
listen 80;
server_name www.magedu.net;
location / {
index index.html index.php;
root /data/nginx/html/pc;
}
location /web {
#proxy_pass http://192.168.7.103:80/; #注意有后面的/,
proxy_pass http://192.168.7.104:80/;
}
}
#后端web服务器必须要有相对于的访问URL
[root@s3 ~]# mkdir /var/www/html/web
[root@s3 ~]# echo "web1 page for apache" > /var/www/html/web/index.html
[root@s4 ~]# mkdir /var/www/html/web
[root@s4 ~]# echo "web2 page for apache" > /var/www/html/web/index.html
#重启Nginx并访问测试:
[root@s2 ~]# curl -L http://www.magedu.net/
pc web
[root@s2 ~]# curl -L http://www.magedu.net/web
web2 page for apache
#Apache的访问日志:
[root@s4 ~]# tail -f /var/log/httpd/access_log
192.168.7.102 - - [04/Mar/2019:18:52:00 +0800] "GET /web/ HTTP/1.1" 200 21 "-"
"curl/7.29.0"
192.168.7.102 - - [04/Mar/2019:18:52:00 +0800] "GET /web HTTP/1.0" 301 233 "-"
"curl/7.29.0"
192.168.7.102 - - [04/Mar/2019:18:52:00 +0800] "GET /web/ HTTP/1.1" 200 21 "-"
"curl/7.29.0"

反向代理示例–缓存功能

缓存功能默认关闭状态

proxy_cache zone | off; 默认off
#指明调用的缓存,或关闭缓存机制;Context:http, server, location
proxy_cache_key string;
#缓存中用于“键”的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri;
proxy_cache_valid [code ...] time;
#定义对特定响应码的响应内容的缓存时长,定义在http{...}中
示例:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_path;
定义可用于proxy功能的缓存;Context:http
proxy_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];
示例:在http配置定义缓存信息
proxy_cache_path /var/cache/nginx/proxy_cache #定义缓存保存路径,proxy_cache会自动创建
levels=1:2:2 #定义缓存目录结构层次,1:2:2可以生成2^4x2^8x2^8=1048576个目录
keys_zone=proxycache:20m #指内存中缓存的大小,主要用于存放key和metadata(如:使用次数)
inactive=120s; #缓存有效时间
max_size=1g; #最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值
#调用缓存功能,需要定义在相应的配置段,如server{...};或者location等
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 10m; #指定的状态码返回的数据缓存多长时间
proxy_cache_valid any 1m;
proxy_cache_use_stale error http_502 http_503;
#在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端,
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502
| http_503 | http_504 | http_403 | http_404 | off ; #默认是off
proxy_cache_methods GET | HEAD | POST ...;
#对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存
非缓存场景压测
[root@s3 app1]# pwd
/var/www/html/app1
[root@s3 app1]# cp /var/log/messages ./log.html #准备测试页面
[root@s2 pc]# ab -n 2000 -c200 http://www.magedu.net/web/log.html
Total transferred: 3059318000 bytes
HTML transferred: 3058760000 bytes
Requests per second: 155.14 [#/sec] (mean)
Time per request: 1289.166 [ms] (mean)
Time per request: 6.446 [ms] (mean, across all concurrent requests)
Transfer rate: 231747.94 [Kbytes/sec] received
准备缓存配置
[root@s2 pc]# vim /apps/nginx/conf/nginx.conf
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m
inactive=120s max_size=1g; #配置在nginx.conf http配置段
[root@s2 pc]# vim /apps/nginx/conf/conf.d/pc.conf
location /web { #要缓存的URL 或者放在server配置项对所有URL都进行缓存
proxy_pass http://192.168.7.104:80/;
proxy_set_header clientip $remote_addr;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
}
[root@s2 pc]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@s2 pc]# systemctl start nginx
访问并验证缓存文件
#访问web并验证缓存目录
[root@s2 pc]# curl http://www.magedu.net/web/log.html
[root@s2 pc]# ab -n 2000 -c200 http://www.magedu.net/web/log.html
Total transferred: 3059318000 bytes
HTML transferred: 3058760000 bytes
Requests per second: 1922.78 [#/sec] (mean)
Time per request: 104.016 [ms] (mean)
Time per request: 0.520 [ms] (mean, across all concurrent requests)
Transfer rate: 2872259.55 [Kbytes/sec] received
#验证缓存目录结构及文件大小
[root@s2 pc]# tree /data/nginx/proxycache/
/data/nginx/proxycache/
└── f
└── 0
└── 6
└── 50b643197ae7d66aaaa5e7e1961b060f
3 directories, 1 file
[root@s2 pc]# ll -h /data/nginx/proxycache/f/0/6/50b643197ae7d66aaaa5e7e1961b060f
-rw------- 1 nginx nginx 1.5M Mar 7 14:30
/data/nginx/proxycache/f/0/6/50b643197ae7d66aaaa5e7e1961b060f
#验证文件内容:
[root@s2 pc]# head -n100 /data/nginx/proxycache/f/0/6/50b643197ae7d66aaaa5e7e1961b060f
#会在文件首部添加相应码
HTTP/1.1 200 OK
Date: Thu, 07 Mar 2019 18:35:37 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 07 Mar 2019 14:14:47 GMT
ETag: "175624-58381ba95ac05"
Accept-Ranges: bytes
Content-Length: 1529380
Connection: close
Content-Type: text/html; charset=UTF-8
192.168.0.1 - - [18/Feb/2019:10:26:33 +0800] "GET / HTTP/1.1" 200 612

添加头部报文信息

nginx基于模块ngx_http_headers_module可以实现对头部报文添加指定的key与值, https://nginx.org/en/docs/
http/ngx_http_headers_module.html

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;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;
#添加自定义响应信息的尾部, 1.13.2版后支持
add_trailer name value [always];

Nginx配置

location /web {
proxy_pass http://192.168.7.103:80/;
proxy_set_header clientip $remote_addr;
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;
}

Nginx http 反向代理高级应用

在上一个章节中Nginx可以将客户端的请求转发至单台后端服务器但是无法转发至特定的一组的服务器,而且不能对后端服务器提供相应的服务器状态监测,但是Nginx可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能,官方文档: https://nginx.org/en/docs/http/ngx_http_upstream_module.html

http upstream配置参数

upstream name {
}
#自定义一组服务器,配置在http内
server address [parameters];
#配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
#server支持的parameters如下:
weight=number #设置权重,默认为1。
max_conns=number #给当前server设置最大活动链接数,默认为0表示没有限制。
max_fails=number #对后端服务器连续监测失败多少次就标记为不可用。
fail_timeout=time #对后端服务器的单次监测超时时间,默认为10秒。
backup #设置为备份服务器,当所有服务器不可用时将重新启用次服务器。
down #标记为down状态。
resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx。
hash KEY consistent;
#基于指定key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器
(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算。
hash $request_uri consistent; #基于用户请求的uri做hash
ip_hash;
#源地址hash调度方法,基于的客户端的remote_addr(源地址)做hash计算,以实现会话保持,
least_conn;
#最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器

反向代理示例–多台web服务器

upstream webserver {
#hash $request_uri consistent;
#ip_hash;
#least_conn;
server 192.168.7.103:80 weight=1 fail_timeout=5s max_fails=3; #后端服务器状态监测
server 192.168.7.104:80 weight=1 fail_timeout=5s max_fails=3;
server 192.168.7.101:80 weight=1 fail_timeout=5s max_fails=3 backup;
}
server {
listen 80;
server_name www.magedu.net;
location / {
index index.html index.php;
root /data/nginx/html/pc;
}
location /web {
index index.html;
proxy_pass http://webserver/;
}
}
#重启Nginx 并访问测试
[root@s2 ~]# curl http://www.magedu.net/web
web1 192.168.7.103
[root@s2 ~]# curl http://www.magedu.net/web
web2 192.168.7.104

关闭192.168.7.103和192.168.7.104,测试nginx backup服务器可用性:

[root@s4 ~]# while true;do curl http://www.magedu.net/web;sleep 1;done
反向代理示例–客户端IP透传
[root@s2 conf.d]# cat pc.conf
upstream webserver {
#hash $request_uri consistent; #consistent 一致性hash算法
#server 192.168.7.103:80 weight=1 fail_timeout=5s max_fails=3;
server 192.168.7.104:80 weight=1 fail_timeout=5s max_fails=3;
server 192.168.7.101:80 weight=1 fail_timeout=5s max_fails=3 backup;
}
server {
listen 80;
server_name www.magedu.net;
location / {
index index.html index.php;
root /data/nginx/html/pc;
}
location /web {
index index.html;
proxy_pass http://webserver/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #添加客户端IP到报文头部
}
}
#重启nginx
#后端web服务器配置
1、Apache:
[root@s4 ~]# vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-
Agent}i\"" combined
#重启apache访问web界面并验证apache日志:
192.168.7.104 192.168.7.102 - - [05/Mar/2019:00:36:03 +0800] "GET / HTTP/1.0" 200 19 "-
" "curl/7.29.0"
192.168.0.1 192.168.7.102 - - [05/Mar/2019:00:40:46 +0800] "GET / HTTP/1.0" 200 19 "-"
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/72.0.3626.119 Safari/537.36"
2、Nginx:
[root@s1 conf.d]# cat /apps/nginx/conf/nginx.conf
"$http_x_forwarded_for"' #默认日志格式就有此配置
重启nginx访问web界面并验证日志格式:
192.168.7.102 - - [04/Mar/2019:16:33:24 +0800] "GET // HTTP/1.0" 200 24 "-"
"curl/7.29.0" "192.168.7.104"
192.168.7.102 - - [04/Mar/2019:16:40:51 +0800] "GET / HTTP/1.0" 200 24 "-" "Mozilla/5.0
(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/72.0.3626.119 Safari/537.36" "192.168.0.1"

实现Nginx tcp负载均衡

Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http 代理类似,其基于ngx_stream_proxy_module模块实现tcp负载,另外基于模块ngx_stream_upstream_module实现后端服务器分组转发、权重分配、状态监测、调度算法等高级功能。

官方文档:https://nginx.org/en/docs/stream/ngx_stream_core_module.html

tcp负载均衡配置参数

stream { #定义stream
upstream backend { #定义后端服务器
hash $remote_addr consistent; #定义调度算法
server backend1.example.com:12345 weight=5; #定义具体server
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
}
upstream dns { #定义后端服务器
server 192.168.0.1:53535; #定义具体server
server dns.example.com:53;
}
server { #定义server
listen 12345; #监听IP:PORT
proxy_connect_timeout 1s; #连接超时时间
proxy_timeout 3s; #转发超时时间
proxy_pass backend; #转发到具体服务器组
}
server {
listen 127.0.0.1:53 udp reuseport;
proxy_timeout 20s;
proxy_pass dns;
}
server {
listen [::1]:12345;
proxy_pass unix:/tmp/stream.socket;
}
}

实现FastCGI

CGI的由来:
最早的Web服务器只能简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态html文件(http://www.zbbz.com/index.html),但是后期随着网站功能增多网站开发也越来越复杂,以至于出现动态技术,比如像php(1995年)、java(1995)、python(1991)语言开发的网站,但是nginx/apache服务器并不能直接运行 php、java这样的文件,apache实现的方式是打补丁,但是nginx缺通过与第三方基于协议实现,即通过某种特定协议将客户端请求转发给第三方服务处理,第三方服务器会新建新的进程处理用户的请求,处理完成后返回数据给Nginx并回收进程,最后nginx在返回给客户端,那这个约定就是通用网关接口(common gatewayinterface,简称CGI),CGI(协议) 是web服务器和外部应用程序之间的接口标准,是cgi程序和web服务器之间传递信息的标准化接口。
在这里插入图片描述
在这里插入图片描述
为什么会有FastCGI?
CGI协议虽然解决了语言解析器和seb server之间通讯的问题,但是它的效率很低,因为web server每收到一个请求都会创建一个CGI进程,PHP解析器都会解析php.ini文件,初始化环境,请求结束的时候再关闭进程,对于每一个创建的CGI进程都会执行这些操作,所以效率很低,而FastCGI是用来提高CGI性能的,FastCGI每次处理完请求之后不会关闭掉进程,而是保留这个进程,使这个进程可以处理多个请求。这样的话每个请求都不用再重新创建一个进程了,大大提升了处理效率。
在这里插入图片描述
什么是PHP-FPM? PHP-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个实现了Fastcgi的管理程序,并且提供进程管理的功能,进程包括master进程和worker进程,master进程只有一个,负责监听端口,接受来自web server的请求。worker进程一般会有多个,每个进程中会嵌入一个PHP解析器,进行PHP代码的处理。

FastCGI配置指令

Nginx基于模块ngx_http_fastcgi_module实现通过fastcgi协议将指定的客户端请求转发至php-fpm处理,其配置
指令如下:

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服务器的参数值,可以是文本,变量或组合,可用于将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


Nginx默认配置示例:
location ~ \.php$ {
root          html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param  SCRIPT_FILENAME /scripts$fastcgi_script_name;   #默认脚本路径 php路径
include fastcgi_params;
}

缓存定义指令:

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:2
keys_zone=name:size #设置缓存名称及k/v映射的内存空间的名称及大小
inactive=time #缓存有效时间,默认10分钟,需要在指定时间满足fastcgi_cache_min_uses 次数被视为
活动缓存。

缓存调用指令:

fastcgi_cache zone | off;
#调用指定的缓存空间来缓存数据,可用位置:http, server, location
fastcgi_cache_key string;
#定义用作缓存项的key的字符串,示例:fastcgi_cache_key $request_uri;
fastcgi_cache_methods GET | HEAD | POST ...;
#为哪些请求方法使用缓存
fastcgi_cache_min_uses number;
#缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项
fastcgi_keep_conn on | off;
#收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接
fastcgi_cache_valid [code ...] time;
#不同的响应码各自的缓存时长
fastcgi_hide_header field; #隐藏响应头指定信息
fastcgi_pass_header field; #返回响应头指定信息,默认不会将Status、X-Accel-...返回

FastCGI示例–Nginx与php-fpm在同一服务器

php安装可以通过yum或者编译安装,使用yum安装相对比较简单,编译安装更方便自定义参数或选项。

php环境准备
#yum安装默认版本php
[root@s2 ~]# yum install php-fpm php-mysql -y #默认版本
[root@s2 ~]# systemctl start php-fpm && systemctl enable php-fpm
[root@s2 ~]# ps -ef | grep php-fpm
php相关配置优化
[root@s2 ~]# grep "^[a-Z]" /etc/php-fpm.conf
include=/etc/php-fpm.d/*.conf
pid = /run/php-fpm/php-fpm.pid
error_log = /var/log/php-fpm/error.log
daemonize = yes #是否后台启动
[root@s2 ~]# cat /etc/php-fpm.d/www.conf
[www]
listen = 127.0.0.1:9000 #监听地址及IP
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保存方式及路径
php_value[session.save_path] = /var/lib/php/session #当时使用file保存session的文件路径
准备php测试页面
[root@s2 ~]# mkdir /data/nginx/php
[root@s2 ~]# cat /data/nginx/php/index.php #php测试页面
<?php
phpinfo();
?>
Nginx配置转发

Nginx安装完成之后默认生成了与fastcgi的相关配置文件,一般保存在nginx的安装路径的conf目录当中,比
如/apps/nginx/conf/fastcgi.conf、/apps/nginx/conf/fastcgi_params。

[root@s2 ~]# vim /apps/nginx/conf/conf.d/pc.conf #在指定文件配置fastcgi
location ~ \.php$ {
root /data/nginx/php; #$document_root调用root目录
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;
#如果SCRIPT_FILENAME是绝对路径则可以省略root /data/nginx/php;
include fastcgi_params;
}
#重启Nginx并访问web测试
systemctl restart nginx
fastcgi_pass常见的错误:
File not found. #路径不对
502: php-fpm处理超时、服务停止运行等原因导致的无法连接或请求超时
php-fpm 的运行状态页面

访问配置文件里面指定的路径,会返回php-fpm的当前运行状态。
Nginx配置:

location ~ ^/(pm_status|ping)$ {
#access_log off;
#allow 127.0.0.1;
#deny all;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
}

重启Nginx并测试:

curl http://www.magedu.net/pm_status
[root@s2 ~]# curl http://www.magedu.net/ping
ping-pong
[root@s2 ~]# curl http://www.magedu.net/pm_status?full
[root@s2 ~]# curl http://www.magedu.net/pm_status?html
[root@s2 ~]# curl http://www.magedu.net/pm_status?json

FastCGI示例–Nginx与php不在同一个服务器

nginx会处理静态请求,但是会转发动态请求到后端指定的php-fpm服务器,因此代码也需要放在后端的php-fpm服务器,即静态页面放在Nginx上而动态页面放在后端php-fpm服务器,正常情况下,一般都是采用6.3.2的部署方式。

yum安装较新版本php-fpm
[root@s4 ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remirelease-
7.rpm #包含较新的PHP等软件包
#[root@s4 ~]# yum install php56-php-fpm php56-php-mysql -y #安装指定版本的php
[root@s4 ~]# yum install php72-php-fpm php72-php-mysql -y
验证安装路径:
[root@s4 ~]# rpm -ql php72-php-fpm

生成php72配置文件:
[root@s4 ~]# cp /opt/remi/php72/root/usr/share/doc/php72-php-common-7.2.24/php.iniproduction
/opt/remi/php72/root/usr/etc/php.ini
[root@s4 ~]# cp /opt/remi/php72/root/usr/share/doc/php72-php-fpm-7.2.24/phpfpm.
conf.default /opt/remi/php72/root/usr/etc//php-fpm.conf
修改php-fpm监听配置

php-fpm默认监听在127.0.0.1的9000端口,也就是无法远程连接,因此要做相应的修改。

#php56修改监听配置
[root@s4 ~]# vim /opt/remi/php56/root/etc/php-fpm.d/www.conf
39 listen = 192.168.7.104:9000 #指定监听IP
65 #listen.allowed_clients = 127.0.0.1 #注释运行的客户端
#php72修改监听配置
[root@s4 ~]# vim /etc/opt/remi/php72/php-fpm.d/www.conf
user = apache
group = apache
listen = 172.18.0.202:9000
; listen.allowed_clients = 127.0.0.1
准备php测试页面
#准备php数据目录
[root@s4 ~]# mkdir /data/nginx/php -p
[root@s4 ~]# vim /data/nginx/php/index.php
<?php
phpinfo();
?>
启动并验证php-fpm
#启动php-fpm56
[root@s4 ~]# systemctl start php56-php-fpm
[root@s4 ~]# systemctl enable php56-php-fpm
#启动php-fpm72
[root@s4 ~]# systemctl start php72-php-fpm
[root@s4 ~]# systemctl enable php56-php-fpm
#验证php-fpm进程及端口:
[root@s4 ~]# ps -ef | grep php-fpm
[root@s4 ~]# ss -tnl | grep 9000
LISTEN 0 128 127.0.0.1:9000 *:*
[root@s4 ~]# netstat -tuanlp | grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 5339/php-fpm: maste
Nginx配置转发
location ~ \.php$ {
root /data/nginx/php;
fastcgi_pass 192.168.7.104:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /data/php$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#重启nginx
[root@s2 ~]# systemctl restart nginx
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值