nginx-rewrite、if、浏览器分离、反向代理、负载均衡

目录

一、Rewrite

rewrite配置,自定义网页

flag标记--break 

flag标记-last 

flag标记--redirect 

 flag标记--permanent

​二、if判断

配置基于域名跳转

基于客户端ip访问跳转

基于浏览器实现分离

反向代理和负载均衡 

什么是代理?

正向代理

反向代理

Nginx负载均衡案例:

一、Rewrite

和apache等web服务软件一样,rewrite的主要功能是实现URL地址的重定向。Nginx的rewrite功能需要PCRE软件的支持,即通过perl兼容正则表达式语句进行规则匹配的。默认参数编译nginx就会支持rewrite的模块,但是也必须要PCRE的支持

Rewirte功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。

Rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递参数外的字符串起作用。例如http://www.cy.com/abc/aa/index.php?a=1&b=2  只对/abc/aa/index.php重写

URL:就是具体路径/位置

URI:指的是一个拥有相同类型/特性的对象集合

Nginx:通过ngx_http_rewrite_module模块支持URL重写,支持if条件判断,但不支持else。

跳转:从一个location跳转到另一个location,循环最多可以执行10次,超过后nginx将返回500错误。

PCRE支持:perl兼容正则表达式的语法规则匹配

重写模块set指令:创建新的变量并设其值

语法格式:

rewrite  <regex>  <replacement>  [flag];

regex:表示正则匹配规则

replacement:表示跳转后的内容

flag:表示rewrite支持的flag标记

flag标记说明:

last:本条规则匹配完成后,继续向下匹配新的location URL规则,一般用在server和if中。

break:本条规则匹配完成即终止,不在匹配后面的任何规则,一般使用在location中。

redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址

permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址。

匹配正则的标识符和意义

^   必须以^后的实体开头

$ 必须以$前的实体结尾

. 匹配任意单个字符

[] 匹配指定字符集的任意字符

[^] 匹配任何不包括在指定字符集内的任意字符串

| 匹配|之前或之后的实体

() 分组,组成一组用于匹配的实体,通常会有|来协助

\ 转义

* 匹配前面的字符出现零次或者多次,如“ab*”能匹配a、ab、abb

+ 匹配前面的字符出现一次或者多次,如“ab+”能匹配ab、abb,但是不能匹配a

? 匹配前面的字符出现零次或者一次,如“ab(cd)?”能匹配ab、abcd

(pattern)  匹配括号内pattern并可以在后面获取对应的匹配,常用$0-$9属性获取小括号中匹配的内容。如^(hello | chenyu)$   //字符串为“hello chenyu”,可以捕获的结果为:

$1=hello$2=chenyu   这些被捕获的数据,在后面就可以当作变量一样进行使用了

 nginx的rewrite功能在企业里应用非常广泛

  1. 可以调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求
  2. 为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态的URL地址伪装成静态地址提供服务
  3. 网址更新域名后,让旧的访问跳转到新的域名上,例如访问京东的360buy.com会跳转到jd.com
  4. 根据特殊变量、目录、客户端的信息进行URL调整等。

rewrite配置,自定义网页

[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# mkdir imgs
[root@nginx html]# cd imgs/
[root@nginx imgs]# rz -E
rz waiting to receive.
[root@nginx imgs]# ls
test.jpg
[root@nginx imgs]# vim /usr/local/nginx/conf/nginx.conf
        location /imgs {
        }
[root@nginx imgs]# nginx -s reload

自定义部署完成后浏览器访问 

flag标记--break 

[root@nginx html]# ls
50x.html  imgs  index.html
[root@nginx html]# mv imgs/ images
[root@nginx html]# ls
50x.html  images  index.html
[root@nginx html]# 

现在访问会发现404找不到网页,需要写rewrite才能再次访问

写rewrite 

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf        
        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 break;
        }
[root@nginx html]# nginx -s reload

 再次访问,又能访问到了

也可以实现跳转到百度

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 break;
        }
        location /images {
                rewrite ^/images/(.*\.jpg)$ http://www.baidu.com last;
        }
[root@nginx html]# nginx -s reload

浏览器访问http://192.168.35.142/images/test.jpg 

flag标记-last 

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 last;
        }
        location /images {
                rewrite ^/images/(.*\.jpg)$ http://www.baidu.com last;
[root@nginx html]# nginx -s reload

http://192.168.35.142/imgs/test.jpg浏览器访问http://192.168.35.142/imgs/test.jpg

flag标记--redirect 

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 redirect;
        }
[root@nginx html]# nginx -s reload

 浏览器访问 http://192.168.35.142/imgs/test.jpg

 flag标记--permanent

[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
        location /imgs {
                rewrite ^/imgs/(.*\.jpg)$ /images/$1 permanent;
        }
[root@nginx html]# nginx -s reload

 二、if判断

可以使用在server段和location

语法:

if  (condition)  {...}

常见的condition:

  1. 变量名
  2. 以变量名为操作数构成的比较表达式(可使用=,!=类似的比较符进行测试)
  3. 正则表达式的模式匹配操作

~  区分大小写的模式匹配检查

~*  不区分大小写的模式检查

  1. 测试指定路径为文件的可能性(-f  !-f)
  2. 测试指定路径为目录的可能性(-d  !-d)
  3. 测试文件的存在性(-e  !-e)
  4. 检查文件是否有执行权限(-x  !-x)

配置基于域名跳转

假如现在公司旧的域名www.cy.com有业务需求,需要使用新的域名www.chenyu.com代替,但是旧域名不能废除,需要跳转到新的域名上,而且后面的参数保持不变

//修改nginx服务器主机名为www.cy.com
[root@nginx html]# hostnamectl set-hostname www.cy.com
[root@nginx html]# bash

//将两个域名写入到/etc/hosts中,并传给客户端
[root@www html]# vim /etc/hosts
192.168.35.142 www.cy.com
192.168.35.142 www.chenyu.com

//修改配置文件,写入rewrite和if结合使用
[root@www html]# vim /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  www.cy.com;   //设置域名
        location / {
                if ($host = 'www.cy.com') {    //变量host为rewrite的全局变量
                        rewrite ^/(.*)$ http://www.chenyu.com/$1 permanent; 
                }                 //$1是匹配http://www.cy.com/后面的字段
                root html;
                index index.html index.htm;
        }
[root@www html]# echo "chenyu test" > /usr/local/nginx/html/index.html 
[root@www html]# nginx -s reload

 客户端使用浏览器访问--http://www.cy.com---我们会发现自动跳转到新的域名www.chenyu.com中

基于客户端ip访问跳转

假如今天公司业务新版本上线,要求所有ip访问任何内容都显示一个固定维护页面,只有公司ip:192.168.100.20访问正常

[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
server {
        listen       80;
        server_name  www.cy.com;
        set $rewrite true;
        if ($remote_addr = "192.168.35.1") {
                set $rewrite false;
                }
        if ($rewrite = true) {
                rewrite (.+) /weihu.html;
                }
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location = /weihu.html {
                root /var/www/html;
                }
        location / {
                root html;
                index index.html index.htm;
                }

//新建/var/www/html目录,并往该目录下写入文件weihu.html,内容为weihu
[root@www ~]# mkdir /var/www/html -p
[root@www ~]# echo "weihu" > /var/www/html/weihu.html

[root@www ~]# nginx -s reload

 在其他主机上面测试

[root@ca ~]# curl http://192.168.35.142
weihu
[root@ca ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:3c:fe:5b brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    inet 192.168.35.143/24 brd 192.168.35.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe3c:fe5b/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@ca ~]# curl http://192.168.35.142
weihu

基于浏览器实现分离

//在/usr/local/nginx/html目录中创建如下目录和文件
[root@www ~]# echo "firefox test" > firefox/index.html
[root@www ~]# echo "chrome test" > chrome/index.html

//修改配置文件
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
        location / {
                if ($http_user_agent ~ Firefox) {
                        rewrite ^(.*)$ /firefox/$1 break;
                }

                if ($http_user_agent ~ Chrome) {
                        rewrite ^(.*)$ /chrome/$1 break;
                }
                root html;
                index index.html index.htm;
                }
        location /firefox {
                root html;
                index index.html;
                }
        location /chrome {
                root html;
                index index.html;
                }
//重载nginx
[root@www ~]# nginx -s reload

反向代理和负载均衡 

nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。

nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。

但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或者使用NFS、MFS分布式共享存储。

Http Proxy模块,功能很多,最常用的是proxy_pass和proxy_cache

如果要使用proxy_cache,需要集成第三方的ngx_cache_purge模块,用来清除指定的URL缓存。这个集成需要在安装nginx的时候去做,如:

./configure --add-module=…/ngx_cache_purge-1.0 …

nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内

weight轮询(默认):接收到的请求按照顺序逐一分配到不同的后端服务器,即使在使用过程中,某一台后端服务器宕机,nginx会自动将该服务器剔除出队列,请求受理情况不会受到任何影响。 这种方式下,可以给不同的后端服务器设置一个权重值(weight),用于调整不同的服务器上请求的分配率;权重数据越大,被分配到请求的几率越大;该权重值,主要是针对实际工作环境中不同的后端服务器硬件配置进行调整的。

ip_hash:每个请求按照发起客户端的ip的hash结果进行匹配,这样的算法下一个固定ip地址的客户端总会访问到同一个后端服务器,这也在一定程度上解决了集群部署环境下session共享的问题。

什么是代理?

说到代理,首先我们要明确一个概念,所谓代理就是一个代表、一个渠道;

此时就设计到两个角色,一个是被代理角色,一个是目标角色,被代理角色通过这个代理访问目标角色完成一些任务的过程称为代理操作过程;如同生活中的专卖店~客人到某达斯专卖店买了一双鞋,这个专卖店就是代理,被代理角色就是某达斯厂家,目标角色就是用户

正向代理

我们如果由于技术需要要去访问国外的某些网站,此时你会发现位于国外的某网站我们通过浏览器是没有办法访问的,此时大家可能都会用一个操作(科学上网)进行访问,科学上网的方式主要是找到一个可以访问国外网站的代理服务器,我们将请求发送给代理服务器,代理服务器去访问国外的网站,然后将访问到的数据传递给我们。

这样的代理模式称为正向代理,正向代理最大的特点是客户端非常明确要访问的服务器地址;服务器只清楚请求来自哪个代理服务器,而不清楚来自哪个具体的客户端;正向代理模式屏蔽或者隐藏了真实客户端信息。

反向代理

多个客户端给服务器发送的请求,nginx服务器接收到之后,按照一定的规则分发给了后端的业务处理服务器进行处理了。此时~请求的来源也就是客户端是明确的,但是请求具体由哪台服务器处理的并不明确了,nginx扮演的就是一个反向代理角色

反向代理,主要用于服务器集群分布式部署的情况下,反向代理隐藏了服务器的信息!

在实际生产项目中,大部分的就是正向代理和反向代理结合起来使用

我们在实际项目操作时,正向代理和反向代理很有可能会存在在一个应用场景中,正向代理代理客户端的请求去访问目标服务器,目标服务器是一个反向代理服务器,反向代理了多台真实的业务处理服务器

Nginx负载均衡案例:

主机

Ip

安装

系统

Nginx

192.168.35.142

Nginx

Rockylinux9

Rs1

192.168.35.143

Httpd

Rockylinux9

Rs2

192.168.35.144

Httpd

Rockylinux9

//rs1主机上,安装httpd,然后添加一个测试网页
[root@rs1 ~]# yum -y install httpd
[root@rs1 ~]# echo "this is rs1" > /var/www/html/index.html
[root@rs1 ~]# systemctl restart httpd
[root@rs1 ~]# systemctl enable httpd

//rs2主机上,安装httpd,然后添加一个测试网页
[root@rs2 ~]# yum -y install httpd
[root@rs2 ~]# echo "this is rs2" > /var/www/html/index.html
[root@rs2 ~]# systemctl restart httpd
[root@rs2 ~]# systemctl enable httpd
//在nginx主机上,修改配置文件,设置负载均衡
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
//在http{}中配置upstream
.....
    upstream webserver {
        server 192.168.35.143;
        server 192.168.35.144;
    }

server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://webserver;
        }

//测试配置文件是否正确,然后重载nginx
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s reload

测试

[root@node4 ~]# curl http://192.168.35.142
this is rs1
[root@node4 ~]# curl http://192.168.35.142
this is rs2

权重 

// 设置权重,如果想其中一台后端真实服务器,多承担一些访问量,可以去设置weight权重
    upstream webserver {
        server 192.168.35.143 weight=2;
        server 192.168.35.144;
    }

测试

[root@node4 ~]# curl http://192.168.35.142
this is rs1
[root@node4 ~]# curl http://192.168.35.142
this is rs1
[root@node4 ~]# curl http://192.168.35.142
this is rs2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值