Nginx Rewrite

跳转场景

        URL看起来更规范、合理

        企业会将动态URL地址伪装成静态地址提供服务

        网址换新域名后,让旧的访问跳转到新的域名上

        服务端某些业务调整

        实际场景

                Nginx跳转需求的实现方式

                        使用rewrite进行匹配跳转

                        使用if匹配全局变量后跳转

                        使用location匹配在跳转

                rewrite放在server{},if{},location{}段中

                        location只对域名后边的除去传递参数外的字符串起作用

                对域名或参数字符串
                        使用if全局变量匹配
                        使用proxy_pass反向代理

        正则表达式

Rewrite

        语法

                rewrite <regex> <replacement> [flag];

                flag标记类型 

                 last与break的区别

                        last:一般写在server和if中,不终止重写后的url匹配

                        break:一般使用在location中,终止重写后的url匹配

location

        分类

                location = patt{} 精准匹配

        优先级

                相同类型的表达式,字符串长的会优先匹配

                按优先级排列

                        = 类型

                        ^~类型表达式

                        正则表达式(~和~*)类型

                        通用匹配(/),如果没有其他匹配,任何请求都会匹配到 

        比较rewrite和location

                相同点

                        都能实现跳转

                不同点

                        rewrite式是在同一域名内更改获取资源的路径

                        location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器

                rewrite会写在location里,执行顺序

                        执行server块里面的rewrite指令

                        执行location匹配

                        执行选定的location中的rewrite指令

                优先级规则

                        匹配某个具体文件

                                (location = 完整路径) > (location ^~ 完整路径) > (location ~* 完整路径)> (location ~ 完整路径) > (location 完整路径) > (location /)

                        用目录做匹配访问某个文件

                                (location = 目录) > (location ^~ 目录/) > (location ~ 目录)> (location ~* 目录) > (location 目录) > (location /)

        新旧域名替换

                1.用yum安装nginx

#因为不能直接下,所以要手动配置

#指定路径
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#编辑配置
vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

                2. 清除所有源,编译

yum clean all
yum makecache
yum -y install nginx

server {
    listen       80;
    server_name  www.kc65.com;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

 `        在打开一台桌面版Linux,配置hosts解析。浏览器访问

        配置一个新的conf文件,配置上server段 ,在桌面版Linux中配置上hosts解析,并访问新域名 

server {
    listen       80;
    server_name  www.newkgc65.com;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

 

        做旧域名跳转到新域名(重定向) 

                        打开旧域名配置文件(配置上重定向)

                        检查配置,重载生效 

[root@bogon conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@bogon conf.d]# systemctl reload nginx
[root@bogon conf.d]# 

         发布
                

vi /usr/share/nginx/html/weihu.html

                 为防止乱码,采取的是html

<html>
  <head>
    <title>Test</title>
    <meta charset="utf-8"></meta>
  </head>
  <body>
    <p>本网站正在维护,维护时间大概两小时</p>
  </body>
</html>

                编辑配置文件

vi kgc65.conf
    set $rewrite true;
    if ($remote_addr = '192.168.27.131') {
        set $rewrite false;
    }
    if ($rewrite = true) {
        rewrite (.+) /weihu.html;
    }

                检测配置,重载环境 

nginx -t
systemctl reload nginx

                测试(在桌面版中的浏览器)

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值