四、实现“ngx_http_rewrite_module”重写url
1、修改配置文件
location / {
rewrite (.*)\.html$ $1.txt last;
rewrite (.*)\.txt$ $1.jpg break;
}
##当用户第一次请求uri中以html结尾时,会把uri重写为以txt结尾并重新开始循环;
随后匹配到第二条rewrite,把uri中的txt更改为以jpg结尾的文件并响应给客户端
rewrite regex replacement [flag]
regex:基于正则表达式,用于匹配用户请求的url
replacement:为重写的结果
Flag:
Last:重写完成后停止对当前uri在location中的后续其他操作,而后更改为对新的uri做出新一轮处理
Break:重写完成后停止对当前uri在当前location中的后续其他操作,直接返回给客户端
Redirect:重写完成后,临时返回给客户端一个新的URL,随后浏览器在根据新的URL请求新的资源;响应码为302
Premanent:重写完成后,永久返回给客户端一个新的URL,随后浏览器在根据新的URL请求信的资源;响应码为301
2、访问web服务

五、实现“ngx_http_gizp_module”压缩功能:只能用在http上下文
1、修改nginx配置文件
http {
gzip on;
gzip_comp_level 6;
gzip_disable msie6;
gzip_http_version 1.0;
gzip_types text/plain;
}
Gzip on | off:启用或禁用gzip压缩响应报文
Gzip_comp_level level:指定压缩比,1-9;默认为1
Gzip_disable regex:regex是匹配客户端浏览器类型的模式,表示对所有匹配到的浏览器下不执行压缩响应
Gzip_min_length length:触发启用先压缩功能的响应报文的最小长度
Gzip_http_version:设定启用压缩响应功能时,协议的最小版本
Gzip_types:指定仅执行压缩的资源内容类型;默认为text/html
gzip_types text/plain、text/css、text/xml、application/x-javascript 、application/xml、application/json、application/java-script;
2、访问web

六、实现“ngx_http_fastcgi_module”
1、虚拟机2安装php-fpm服务
[root@node1 ~]# yum install php-fpm -y ##安装php-fpm服务
[root@node1 ~]# vim /etc/php-fpm.d/www.conf ##修改php-fpm的配置文件
listen = 172.18.42.201:9000 ##监听本地能与外部通信的IP地址
listen.allowed_clients = 172.18.42.200 ##监听具有httpd服务的IP
[root@node1 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 172.18.42.201:9000 *:*
Nginx和php结合的方式只有fpm;php-fpm的工作方式类似于httpd的prefork模块
Listen = :指明本地能与外部通信的地址
Listen.allow_clients = :指明具有httpd服务的IP
Pm = dynamic | static
Pm.start_servers:启动fpm进程时启动的工作进程数量
Pm.min_spare_servers:最少空闲进程数
Pm.max_spare_servers:最大空闲进程数
Pm.max_children:最大工作进程数
2、修改nginx的配置文件
location ~ \.php$ {
fastcgi_pass 172.18.42.201:9000; ##指明fpm-php服务的IP
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /web/lweim/$fastcgi_script_name;
include fastcgi_params;
}
Fastcgi_pass:用来指明代理那个服务器(装了php-fpm的服务器能与外部监听的地址)
Fastcgi_index:fastcgi应用的主页面名称
Fastcgi_param:传递给fpm服务器参数
3、访问web
七、实现“fastcgi_cache_path”缓存:只能用在http上下文
1、修改nginx的配置文件
[root@node0 ~]# vim /etc/nginx/nginx.conf
http {
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=ngxcache:10m; ##定义缓存
}
location ~ \.php$ {
fastcgi_pass 172.18.42.201:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /web/lweim/$fastcgi_script_name;
include fastcgi_params;
fastcgi_cache ngxcache; ##指明缓存名称
fastcgi_cache_key $request_uri; ##定义缓存键
fastcgi_cache_valid 200 302 10m; ##状态码200、302缓存10分钟
fastcgi_cache_valid 301 1h; ##状态码301缓存1个小时
fastcgi_cache_valid 404 2h; ##状态码404缓存2个小时
}
Fastcgi_cache_path path:指明缓存文件的路径
Fastcgi_cache zone | off:是否启用缓存,如果启用,需要指明缓存与那个cache文件中
Fastcgi_cache_key string:定义要使用的缓存键
Fastcgi_cache_methods GET | HEAD | POST:缓存那些类型的请求的相关数据
Fastcgi_cache_vaild [code..] time:对不同响应码设定其可缓存时间
2、查看缓存目录
[root@node0 ~]# tree /var/cache/nginx/fastcgi/
/var/cache/nginx/fastcgi/
├── 1
│ └── af
│ └── e251273eb74a8ee3f661a7af00915af1
├── b
│ └── fe
│ └── c86156f7dcfecf44876ca30d1bac7feb
└── e
└── 39
└── f46b8508aa08a6f8670fb088b8a9739e
八、实现“ngx_http_proxy_module”
1、修改nginx配置文件
[root@node0 ]# vim nginx.conf
location ~ .*\.txt$ { ##txt结尾的文件由虚拟机2来响应
proxy_pass
}
location ~ .*\.jpg$ {
proxy_pass ##jpg结尾的文件由虚拟机3来响应
}
(1)当proxy_pass后面的路径不带uri时,会将其location中的uri传递给后端主机
location /bbs {
proxy_pass http://172.18.42.200;
}
##最终请求的路径为http://172.18.42.200/bbs
(2)当proxy_pass后面路径是一个uri时,它会将location中的uri替换为proxy_pass的后端主机
location /bbs {
proxy_pass http://172.18.42.200/;
}
##最终请求的路径为http://172.18.42.200;把“/bbs/”当做成“/”
(3)如果location定义其uri时使用了正则表达式模式匹配机制,则proxy_pass后的路径一定不能带uri
location ~* \.php$ {
proxy_pass
}
2、配置虚拟机2
[root@node1 ~]# yum install httpd -y
[root@node1 ~]# echo "172.18.42.201 txt" > /var/www/html/wtc.txt
[root@node1 ~]# systemctl start httpd.service
3、配置虚拟级3
[root@node1 ~]# yum install httpd -y
[root@node2 ~]# mv /root/wawa.jpg /var/www/html/wtc.jpg
[root@node1 ~]# systemctl start httpd.service
4、访问web


