目录
前言
nginx的动静分离 地址重写 反向代理的功能介绍以及集群属性使用方案
一、nginx的动静分离 Fast-CGI
1)Fast-CGI的简单介绍
Fast-CGI支持PHP网页解析 解析php动态页面
2)使用IP端口方式连接
原来是以路径的方式 变成端口的方式进行链接来实现解析php的动态页面 然后显示
Fast-CGI是快速公共(通用)网关接口,可以连接如nginx等网站程序到网站的语言解释器(比如php) ,php-fpm进程使用了Fast-CGI解析动态网站页面
1)修改Nginx配置文件并启动服务
以下的修改配置文件是删除了一行内容 不在此内容内的就是被删除的不要忘记
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
...
65 location ~ \.php$ { #~是使用正则表达式匹配以.php结尾,\ 转义
66 root html;
67 fastcgi_pass 127.0.0.1:9000; #将请求转发给本机php-fpm的9000端口
68 fastcgi_index index.php; #网站默认页
69 include fastcgi.conf; #加载fastcgi配置文件
70 }
如果想要访问此网站时直接输入http://192.168.99.5就能访问到这个登录页面,可以更改以下配置实现
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
location / {
root html;
index index.php index.html index.htm; #新添加index.php
}
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
2)修改 php-fpm配置文件
打开php-fpm配置文件,注意该配置文件中;(分号)是注释
[root@proxy nginx-1.22.1]# vim /etc/php-fpm.d/www.conf
38 listen = 127.0.0.1:9000 #更改php-fpm端口号(使用网络通信)
[root@proxy nginx-1.22.1]# systemctl restart php-fpm #重启服务
[root@proxy nginx-1.22.1]# ss -antlp | grep 9000 #查看监听端口
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:* users:(("php-fpm",pid=15808,fd=8),("php-fpm",pid=15807,fd=8),("php-fpm",pid=15806,fd=8),("php-fpm",pid=15805,fd=8),("php-fpm",pid=15804,fd=8),("php-fpm",pid=15803,fd=6))
[root@proxy nginx-1.22.1]# vim /etc/php-fpm.d/www.conf
115 pm.max_children = 50 #最大进程数量
120 pm.start_servers = 5 #最小进程数量
3)使用socket方式连接
1)更改php-fpm配置
[root@proxy nginx-1.22.1]# vim /etc/php-fpm.d/www.conf
38 listen = /run/php-fpm/www.sock #socket方式(使用进程通信)
55 listen.acl_users = apache,nginx,nobody #添加nobody账户
[root@proxy nginx-1.22.1]# systemctl restart php-fpm #重启服务
2)修改Nginx配置文件并启动服务
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
...
65 location ~ \.php$ { #匹配以.php结尾
66 root html;
67 fastcgi_pass unix:/run/php-fpm/www.sock; #将请求转发给php-fpm进程
68 fastcgi_index index.php;
69 include fastcgi.conf; #加载fastcgi配置文件
70 }
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
使用浏览器访问192.168.88.5/test.php 可以看到页面内容
二、nginx的地址重写
1)主要参数
关于Nginx服务器的地址重写,主要用到的配置参数是rewrite
2)语法格式
rewrite regex replacement flag
rewrite 旧地址 新地址 [选项]
3)访问a.html重定向到b.html
1)修改Nginx服务配置
[root@proxy nginx-1.22.1]# cd /usr/local/nginx/
[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf #还原配置文件
cp: overwrite 'conf/nginx.conf'? y
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
rewrite /a.html /b.html; #新添加地址重写,a.html重定向到b.html
...
location / {
root html;
index index.html index.htm;
}
}
[root@proxy nginx]# echo "nginx-B~~" > /usr/local/nginx/html/b.html
2)重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
3)客户端测试
http://192.168.88.5/a.html #内容显示的是nginx-B~~,但是地址栏没有发生变化,还是a.html页面
此时配置文件中直接写rewrite /a.html /b.html; 配置,在测试是其实会有些问题,
比如在浏览器中访问时把192.168.88.5/a.html写成192.168.88.5/a.htmldc
或者写成 192.168.88.5/dc/a.html,访问都会正常显示b.html的页面,
这是因为此时写的是只要包含a.html的都会跳转,没有进行精准匹配,
可以进行以下修改,只有写a.html时才会正确跳转
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
rewrite ^/a\.html$ /b.html; #新添加地址重写,a.html重定向到b.html
...
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
浏览器重新访问测试即可192.168.88.5/a.html,显示b.html页面内容
4)redirect选项重定向
1)修改Nginx服务配置
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
rewrite ^/a\.html$ /b.html redirect; #新修改,redirect重定向
...
location / {
root html;
index index.html index.htm;
}
}
2)重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
3)浏览器测试,地址栏同时发生变化
http://192.168.88.5/a.html #内容显示的是nginx-B~~,地址栏发生变化,是b.html页面
5)不同网站间跳转
修改Nginx服务配置实现访问192.168.88.5的请求重定向至www.tmooc.cn
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
rewrite / http://www.tmooc.cn/; #新修改,访问旧网站的任意内容都跳转到新网站
location / {
root html;
index index.html index.htm;
}
}
2)重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
3)客户端测试
http://192.168.88.5 #可以成功跳转
6)子页面重定向
1) 修改Nginx服务配置
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
rewrite /(.*) http://www.baidu.com/$1; #新修改 /(.*)代表整体复制 $1代表粘贴
location / {
root html;
index index.html index.htm;
}
}
2)重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
3)客户端测试
http://192.168.88.5/b.html #成功跳转
7)不同浏览器跳转到不同页面
1) 创建网页目录以及对应的页面文件:
[root@proxy nginx]# mkdir html/firefox
[root@proxy nginx]# echo firefox~~ > html/firefox/abc.html #火狐专用页面
[root@proxy nginx]# echo others~~ > html/abc.html #其他浏览器专用页面
火狐访问192.168.88.5/abc.html时可以看到html/firefox/abc.html里面内容
其他浏览器访问192.168.88.5/abc.html时可以看到html/abc.html里面内容
2) 修改Nginx服务配置
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. .
server {
listen 80;
server_name localhost;
if ( $http_user_agent ~* firefox ) { #如果用户使用了火狐浏览器
rewrite (.*) /firefox/$1;
#就进行地址重写,让用户看到火狐专用页面,否则就是其他页面;$http_user_agent是nginx的内置变量
,包含了发起 HTTP 请求的客户端的用户代理(User-Agent)字符串,比如用的什么浏览器
}
location / {
root html;
index index.html index.htm;
}
3)重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
4)客户端测试
用火狐浏览器与其他浏览器访问相同地址192.168.88.5/abc.html,可以得到不同结果
火狐浏览器访问192.168.88.5/abc.html,得到结果firefox~~
其他浏览器访问192.168.88.5/abc.html,得到结果others~~
8)其他选项测试
last 不再读其他语句,但还会继续匹配其他location语句
break 在locatinon 可以达到last效果
1)last不再读其他语句
测试last last不再读其他语句
1)修改Nginx服务配置
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
rewrite /a.html /b.html; #新修改
rewrite /b.html /c.html; #新修改
...
}
...
2)重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@proxy nginx]# echo nginx-c~~ > html/c.html
3)浏览器测试
192.168.88.5/a.html #内容显示的是nginx-c~~
如果想要访问的是b.html的内容,可以做以下更改
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen 80;
server_name localhost;
rewrite /a.html /b.html last; #新修改
rewrite /b.html /c.html;
...
}
...
重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
浏览器测试访问
192.168.88.5/a.html #内容显示的是nginx-b~~
2)同location中last 还会继续读
测试last会继续匹配其他location语句
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
...
location / { #此处为默认的location
rewrite /a.html /b.html last; #新添加
root html;
index index.html index.htm;
}
location /b.html { #这里是新添加的location
rewrite /b.html /c.html;
}
...
重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
客户端测试: http://192.168.88.5/a.html,显示为nginx-c~~
3)同location中break 不会继续读
break 不再读其他语句,结束请求
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
...
location / {
rewrite /a.html /b.html break; #break可以阻止后面的语句
root html;
index index.html index.htm;
}
location /b.html {
rewrite /b.html /c.html;
}
...
重新加载配置文件
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
客户端测试: http://192.168.88.5/a.html,显示为nginx-b~~
三、nginx的反向代理
1)nginx反向代理的简单介绍
反向代理(七层代理)处理web集群 只能处理web集群
2)配置文件修改的应用
1)修改nginx的配置文件
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...
http {
...
#使用upstream定义后端服务器集群,集群名称任意(如webserver)
#使用server定义集群中的具体服务器和端口
upstream webserver {
server 192.168.99.100:80;
server 192.168.99.200:80;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://webserver; #通过proxy_pass将用户的请求转发给webserver集群
}
...
2)启动nginx
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx
3)配置upstream集群池属性
1)设置权重 weight
weight可以设置后台服务器的权重,权重越大任务的分配量就越大
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
.. ..
upstream webserver {
server 192.168.99.100:80 weight=2;
server 192.168.99.200:80;
}
server {
.. ..
2)设置健康检查
设置健康检查 max_fails 检查次数 fails_timeout 过多少秒重新检查一下
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...
upstream webserver {
server 192.168.99.100:80;
server 192.168.99.200:80 max_fails=2 fail_timeout=30;
#链接两次 每过30秒重新链接两次
}
server {
...
3)登录状态锁定
设置相同客户端访问相同Web服务器 已经登录了 不能再次登录
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...
upstream webserver {
ip_hash;
server 192.168.99.100:80;
server 192.168.99.200:80;
}
server {
...
4)标记 集群主机暂时不参与集群活动
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...
upstream webserver {
server 192.168.99.100:80;
server 192.168.99.200:80 down;
}
server {
...
总结
1.Fast-CGI的工作原理?
将CGI解释器进程保持在内存中并因此获得较高的性能
web server 来了解释器进行解析 然后分配到子进程中 返回结果 如果又来了多个web server 扔到不同的子进程中 有较高的性能
2.socket方式和端口转发方式的区别?
仅限于本地主机,不能跨网络通信,效率高,资源消耗少
可以跨网络通信,可以用于不同服务器之间的通信,效率低,资源消耗少
3.redirect和permanent两个重定向的区别?
redirect 临时重定向,状态码302
permanent 永久重定向,状态码301
last 不再读其他语句,但还会继续匹配其他location语句