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

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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 23:24:05       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 23:24:05       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 23:24:05       45 阅读
  4. Python语言-面向对象

    2024-07-20 23:24:05       55 阅读

热门阅读

  1. vue-print-nb 前端打印的一个实现方案

    2024-07-20 23:24:05       21 阅读
  2. 【Linux的线程篇章 - 线程基础知识储备】

    2024-07-20 23:24:05       15 阅读
  3. 解决网络游戏频繁掉线的策略与实践

    2024-07-20 23:24:05       16 阅读
  4. Qt项目:基于Qt实现的网络聊天室---好友申请

    2024-07-20 23:24:05       15 阅读
  5. 微软全球大蓝屏:必须手工修复

    2024-07-20 23:24:05       20 阅读
  6. 25、气象填色图绘制

    2024-07-20 23:24:05       13 阅读
  7. 【Flutter】 webview_flutter避坑

    2024-07-20 23:24:05       17 阅读
  8. C++的模板(十二):forward模板

    2024-07-20 23:24:05       16 阅读
  9. Kotlin协程最佳实践

    2024-07-20 23:24:05       11 阅读
  10. SQL Server的魔法工坊:打造数据库的自定义函数

    2024-07-20 23:24:05       20 阅读
  11. Qt判定鼠标是否在该多边形的线条上

    2024-07-20 23:24:05       15 阅读
  12. 什么是虹膜识别技术

    2024-07-20 23:24:05       14 阅读