centos 7.6——Nginx中rewrite模块应用(location)——基于域名的跳转等

文章目录

  • centos 7.6——Nginx中rewrite模块应用(location)——基于域名的跳转等
  • 一、Nginx Rewrite概述
    • 1.1 Rewrite跳转场景
    • 1.2 Rewrite跳转实现
    • 1.3 Nginx跳转需求的实现方式
    • 1.4 常用的正则表达式元字符
    • 1.5 Rewrite命令语法
      • 1.5.1 rewrite 命令语法
      • 1.5.2 last和break比较
      • 1.3.3flag标记说明
    • 1.6 location分类
      • 1.6.1分类
      • 1.6.2 正则匹配的常用表达式
    • 1.6.3 location 优先级
      • 1.6.4 比较 rewrite 和 location
      • 1.6.5 Location优先级的示例 2-1
      • 1.6.6 Location优先级的示例 2-2
      • 1.6.7 location优先级规则
  • 二、Nginx Rewrite基本操作
    • 2.1 基于域名的跳转
    • 2.2基于客户端ip访问跳转
    • 2.3 基于旧 旧域名跳转到新域名后面加目录
    • 2.4 基于参数匹配的跳转,例如现在访问
    • 2.5 基于目录下所有结尾的文件跳转,访问http://www.kgc.com/upload/1.php跳转到首页。
    • 2.6 基于最普通一条url请求的跳转,访问一个具体的页面跳转的首页
  • 拓展小结

一、Nginx Rewrite概述

什么是rewrite?

rewrite重写是由ngx_http_rewrite_module模块提供,主要是实现URI改写功能,并且此模块是Nginx默认安装的模块。通过
rewrite,用户可以实现url重定向,根据regex规则来匹配内容跳转到replacement,结尾以flag标记。

1.1 Rewrite跳转场景

  • URL看起来更规范、合理
  • 企业会将动态URL地址伪装成静态地址提供服务
  • 网址换新域名后,让旧的访问跳转到新的域名上
  • 服务端某些业务调整

1.2 Rewrite跳转实现

ngx_http_rewrite_module

  • Nginx : 支持URL重写、支持if条件判断,但不支持else
  • 跳转 :循环最多可以执行10次,超过后Nginx将返回500错误
  • PCRE支持
  • 重写模块set指令

rewrite使用Nginx全局变量或自己设置的变量,结合正则表达式和标志位实现URL重写以及重定向

1.3 Nginx跳转需求的实现方式

  • 使用rewrite进行匹配跳转
  • 使用if匹配全局变量后跳转
  • 使用location匹配再跳转
  • rewrite放在 server{},if{},location{} 段中
  • location只对域名后边的除去传递参数外的字符串起作用
  • 对域名或参数字符串
  • 使用if全局变量匹配
  • 使用proxy_pass反向代理

1.4 常用的正则表达式元字符

1.5 Rewrite命令语法

1.5.1 rewrite 命令语法


rewrite      <regex>       <repalcement>         [flag];(正则)         (跳转的内容)         rewrite 支持的flag标记

1.5.2 last和break比较

last break
使用场景 一般写在server和if中 一般使用在location中
URL匹配 不终止重写后的url匹配 终止重写后的url匹配

1.3.3flag标记说明

标记 说明
last 相当于Apache的[L]标记,表示完成rewrite
break 本条规则匹配完成即终止,不再匹配后面的任何规则
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url
permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url

1.6 location分类

1.6.1分类

  • location = patt {} [精准匹配]
  • location patt {} [一般匹配]
  • location ~ patt {} [正则匹配]

1.6.2 正则匹配的常用表达式

1.6.3 location 优先级

  • 相同类型的表达式,字符串长的会优先匹配
  • 按优先级排列
  • = 类型
  • ^~ 类型表达式
  • 正则表达式(~和 ~*)类型
  • 常规字符串匹配类型,按前缀匹配
  • 通用匹配(/),如果没有其它匹配,任何请求都会匹配到

1.6.4 比较 rewrite 和 location

相同点

  • 都能实现跳转

不同点

  • rewrite是在同一域名内更改获取资源的路径
  • location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器
  • rewrite会写在location里,执行顺序
  • 执行server块里面的rewrite指令
  • 执行location匹配
  • 执行选定的location中的rewrite指令

1.6.5 Location优先级的示例 2-1

1.6.6 Location优先级的示例 2-2

1.6.7 location优先级规则

  • 匹配某个具体文件

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

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

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

二、Nginx Rewrite基本操作

安装Nginx服务

安装nginx源

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm


yum install -y nginx修改默认站点配置文件:etc/nginx/conf.d/default.conf
server {listen       80;server_name  www.domain.com;#charset koi8-r;access_log  /var/log/nginx/www.domain.com-access.log  main;location / {root   /usr/share/nginx/html;index  index.html index.htm;}......//省略部分
}systemctl start nginx

==== 注 意=====

  • 确定域名可以正常解析
  • 做下一个场景前,要删除上一个场景的配置
  • 及时清除浏览器缓存

2.1 基于域名的跳转

公司旧域名www.kgc.com,因业务需求有变更,需要使用新域名
www.ttyy778899.com代替

  • 不能废除旧域名
  • 从旧域名跳转到新域名,且保持其参数不变
先做一个DNS域名, www.kgc.com[root@promote html]# vim /etc/named.conflisten-on port 53 { any; };allow-query     { any; };[root@promote html]# vim /etc/named.rfc1912.zones zone "kgc.com" IN {type master;file "kgc.com.zone";allow-update { none; };
};
zone "ttyy778899.com" IN {        type master;file "ttyy778899.com.zone";allow-update { none; };
};
[root@promote html]# cd /var/named/
[root@promote named]# ll
总用量 20
drwxrwx---. 2 named named   23 8月  12 14:26 data
drwxrwx---. 2 named named   60 8月  12 19:59 dynamic
-rw-r-----. 1 root  named  167 8月  12 14:26 kgc.com.zone
-rw-r-----. 1 root  named 2253 4月   5 2018 named.ca
-rw-r-----. 1 root  named  152 12月 15 2009 named.empty
-rw-r-----. 1 root  named  152 6月  21 2007 named.localhost
-rw-r-----. 1 root  named  168 12月 15 2009 named.loopback
drwxrwx---. 2 named named    6 6月   1 23:26 slaves
[root@promote named]# cp -p named.localhost kgc.com.zone[root@promote named]# vim kgc.com.zone $TTL 1D
@       IN SOA  @ rname.invalid. (0       ; serial1D      ; refresh1H      ; retry1W      ; expire3H )    ; minimumNS      @A       127.0.0.1
www IN  A       192.168.75.131[root@promote named]# cp -p kgc.com.zone ttyy778899.com.zone
[root@promote named]# vim ttyy778899.com.zone $TTL 1D
@       IN SOA  @ rname.invalid. (0       ; serial1D      ; refresh1H      ; retry1W      ; expire3H )    ; minimumNS      @A       127.0.0.1
www IN  A       192.168.75.131
~
~
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm[root@localhost ~]#yum install -y nginx
[root@promote named]# rpm -q nginx
nginx-1.18.0-1.el7.ngx.x86_64
[root@promote named]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
[root@promote named]# vim /etc/nginx/conf.d/default.conf
server {listen       192.168.75.131:80;server_name  www.kgc.com;location / {if ($host = 'www.kgc.com') {rewrite ^/(.*)$ http://www.ttyy778899.com/$1 permanent;          }root   /usr/share/nginx/html;}
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote named]# systemctl restart nginx
[root@promote named]# 

虚拟机win10 浏览器输入旧的域名www.kgc.com 之后跳转到www.ttyy778899.com

2.2基于客户端ip访问跳转

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


[root@promote named]# vim /etc/nginx/conf.d/default.conf set $rewrite true;      #判断标志$rewriteif ($remote_addr = "192.168.75.30") {set $rewrite false;     # 允许公司内部访问,更改标识位false}if ($rewrite = true) {     #如果不是公司ip,加上后缀地址作为标识rewrite (.+) /error.html;  }location = /error.hmtl {root /usr/share/nginx/nginx/html;}location / {root /usr/share/nginx/html;index  index.html index.htm;}
[root@promote named]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote named]# systemctl restart nginx

使用虚拟机win10 IP地址 192.168.75.88不属于公司内部网络访问www.kgc.com失败

使用虚拟机win10 IP地址 192.168.75.30属于公司内部IP地址访问www.kgc.com成功

2.3 基于旧 旧域名跳转到新域名后面加目录

例如现在访问的是 http://bbs.kgc.com,
现在需要将这个域名下面的发帖都跳转到http://www.kgc.com/bbs,注意保持域名跳转后的参数不变。


[root@promote named]# vim /etc/nginx/conf.d/default.conf location /post {rewrite (.+) http://www.kgc.com/bbs$1 permanent;
}
[root@promote named]# cd /var/named/
[root@promote named]# vim kgc.com.zone $TTL 1D
@       IN SOA  @ rname.invalid. (0       ; serial1D      ; refresh1H      ; retry1W      ; expire3H )    ; minimumNS      @A       127.0.0.1
bbs IN  A       192.168.75.131
~
[root@promote named]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote named]# systemctl restart nginx
[root@promote named]# systemctl restart named

虚拟机win10 输入 http://bbs.kgc.com/post/abc.html 之后还是继续跳转到www.kgc.com/bbs/post/abc.html验证成功
现在访问的是 http://bbs.kgc.com,现在需要将这个域名下面的发帖都跳转到http://www.kgc.com/bbs,注意保持域名跳转后的参数不变。**
浏览器需要清空缓存

2.4 基于参数匹配的跳转,例如现在访问

http://www.kgc.com/100-100-100.html

http://www.kgc.com/100-200-任意数字.html

跳转到http://www.kgc.com 页面


先做一个DNS域名, www.kgc.com[root@promote html]# vim /etc/named.conflisten-on port 53 { any; };allow-query     { any; };
[root@promote html]# vim /etc/named.rfc1912.zones zone "kgc.com" IN {type master;file "kgc.com.zone";allow-update { none; };
};
[root@promote named]# vim kgc.com.zone $TTL 1D
@       IN SOA  @ rname.invalid. (0       ; serial1D      ; refresh1H      ; retry1W      ; expire3H )    ; minimumNS      @A       127.0.0.1
www IN  A       192.168.75.131
~
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm[root@localhost ~]#yum install -y nginx
[root@promote named]# rpm -q nginx
nginx-1.18.0-1.el7.ngx.x86_64
[root@promote named]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
[root@promote named]# vim /etc/nginx/conf.d/default.conf server {listen       192.168.75.131:80;server_name  www.kgc.com;if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {   //直接在server中写,不要在location中写rewrite (.*) http://www.kgc.com permanent;
}[root@promote nginx]# systemctl restart nginx
[root@promote nginx]# iptables -F
[root@promote nginx]# setenforce 0
[root@promote nginx]# systemctl restart named

虚拟机win0 验证

http://www.kgc.com/100-100-100.html

2.5 基于目录下所有结尾的文件跳转,访问http://www.kgc.com/upload/1.php跳转到首页。


[root@promote nginx]# vim /etc/nginx/conf.d/default.conf location ~* /upload/.*\.php$ {rewrite (.+) htpp://www.kgc.com permanent;
}[root@promote nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote nginx]# systemctl restart nginx
[root@promote nginx]# 

虚拟机win10 验证访问http://www.kgc.com/upload/1.php

2.6 基于最普通一条url请求的跳转,访问一个具体的页面跳转的首页


[root@promote nginx]# vim /etc/nginx/conf.d/default.conflisten       192.168.75.131:80;server_name  www.kgc.com;location ~* ^/1/test.html {rewrite (.+) http://www.kgc.com permanent;   //+号,代表一次或者多次, “ . ”点号是匹配除了“\n” 的任意字符
}[root@promote nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@promote nginx]# systemctl restart nginx
[root@promote nginx]# 

win10 虚拟机验证访问 http://www.kgc.com/1/test.html

注:该实验思路,当你想通过链接直接访问淘宝购物车里的商品,会弹出登录用户。
这里就是对某个网页的页面进行权限设置,无法直接通过网页的链接进行直接的访问。只能经过首页验证等方式才能访问。

拓展小结

匹配内容是URI
站点目录:/d/www
URL:http://127.0.0.1/admin/index.php
对应本地文件:/d/www/admin/index.php,如果是windows文件系统,由于不区分大小写,如果站点下的目录是Admin,那么这里的文件地址会变成:/d/www/Admin/index.php,下面的URI也会跟着变。
URI:admin/index.php,这里的URI是从文件系统去掉站点目录的部分与“%{REQUEST_URI}”有区别的。如果站点下的目录是Admin,URI=Admin/index.php

%{REQUEST_URI}:/admin/index.php,是URL去掉域名的部分

%{REQUEST_FILENAME}:URL对应本地文件,这里有几点要注意的地方:
1.windows文件系统下,会自动根据本地的文件夹大小写进行转换
2.FILENAME会自动去掉多余的部分,如/d/www/admin/setting/index.php,如果admin是个空文件夹,里面没有任何东西,这里的%{REQUEST_FILENAME}=,估计apache的文件系统是先从顶级目录往下遍历,当某个目录不存时,则从这个不存在的目录地址中断

下面是可以用作if判断的全局变量

$args : #这个变量等于请求行中的参数,同$query_string$content_length : 请求头中的Content-length字段。$content_type : 请求头中的Content-Type字段。$document_root : 当前请求在root指令中指定的值。$host : 请求主机头字段,否则为服务器名称。$http_user_agent : 客户端agent信息$http_cookie : 客户端cookie信息$limit_rate : 这个变量可以限制连接速率。$request_method : 客户端请求的动作,通常为GET或POST。$remote_addr : 客户端的IP地址。$remote_port : 客户端的端口。$remote_user : 已经经过Auth Basic Module验证的用户名。$request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。$scheme : HTTP方法(如http,https)。$server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。$server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。$server_name : 服务器名称。$server_port : 请求到达服务器的端口号。$request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。$uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。$document_uri : 与$uri相同。

rewrite模块

    URI跟URL介绍什么是uri?统一标识符,拿www.abc.com/aw/wd/举例,那么rui就是/aw/wd/这部分数据(也有可能是图片,html网页,如果是伪静态的话,那就得看配置是啥玩意了什么是url?  统一定位符,还是拿www.abc.com/aw/wd/举例,那么整个www.abc.com/aw/wd/就是url        什么是rewrite?rewrite重写是由ngx_http_rewrite_module模块提供,主要是实现URI改写功能,并且此模块是Nginx默认安装的模块。通过rewrite,用户可以实现url重定向,根据regex规则来匹配内容跳转到replacement,结尾以flag标记。工作方式rewrite匹配默认是按照从上至下的顺序进行匹配,如果没有添加flag参数,直到把最后一个rewrite匹配完才终止,如果最后一个都没有匹配到内容则返回500,如果想要提前终止就得flag参数来帮忙实现。

rewrite模块常见指令

if (条件) {} #设定条件,再进行重写
set #后面+值,可以给变量赋值
return #返回状态码信息,并且支持自定义信息
rewrite #重写 (这个是重点)
rewrite_log # rewrite日志

centos 7.6——Nginx中rewrite模块应用(location)——基于域名的跳转等相关推荐

  1. NGINX的rewrite模块

    NGINX的rewrite模块 4.18 Rewrite 模块 Nginx服务器利用 ngx_http_rewrite_module 模块解析和处理rewrite请求,此功能依靠 PCRE(perlc ...

  2. Nginx的rewrite模块疑问排查

    标题索引 追溯原因 实验分析 原理总结 追踪原因 最近心态"一步一印,有印为证",在Nginx的rewrite模块在工作过程中,客户端发起包到服务器解包整体过程浏览器做了什么?服务 ...

  3. Nginx中rewrite的用法详解

    文章目录 一.rewrite简介 1.1 rewrite跳转场景 1.2 rewrite跳转实现过程 1.3 rewrite实际场景 1.4 nginx正则表达式 二.rewrite命令 2.1 re ...

  4. 在simulink中采用模块搭建了基于双二阶广义积分器的三相锁相环,整个仿真环境完全离散化

    三相锁相环. 在simulink中采用模块搭建了基于双二阶广义积分器的三相锁相环,整个仿真环境完全离散化,运行时间更快,主电路与控制部分以不同的步长运行,更加贴合实际. 基于双二阶双二阶广义积分器的三 ...

  5. web服务器 ---nginx 虚拟主机的创建(基于 域名 . 端口 . ip )以及nginx访问控制

    文章目录 前言 一:Nginx服务基础(理论) 二:Nginx虚拟主机实验 2.1:Nginx虚拟主机应用 2.2 具体步骤,配置基于域名的虚拟主机 2.2.1 安装环境软件软件 2.2.2 编译安装 ...

  6. nginx 的 rewrite 模块

    ngxhttprewrite_module 模块用来使用正则表达式(PCRE)改变请求的 URI,返回重定向,并有条件地选择配置. 指令执行顺序 首先顺序执行 server 块中的 rewrite 模 ...

  7. nginx安装rewrite模块

    Linux nginx安装目录找不到? 如果找不到,那应该就是没有完成安装或者编译: 首先要确认是否正确安装了: make&&makeinstall重新编译一次 其次,在确定安装的目录 ...

  8. python中pygame模块下载_基于python中pygame模块的Linux下安装过程(详解)

    pyhthon中pygame模块怎么安装?pyhthon中pygame模块怎么安装?鄙人为初二一名学生,闲来无事 钻研起电这句话还是建议问一下你们代课老师吧,因为你们老师是这方面专家,诺儿那边的话肯定 ...

  9. html判断国家,nginx通过geoip2模块实现判断用户来源国家跳转中英站

    ip数据库文件下载地址:https://dev.maxmind.com/geoip/geoip2/geolite2/ 编译nginx添加第二代geoip2模块,第一代自带 的–with-http_ge ...

最新文章

  1. python基础30个常用代码大全-Python基础小题汇总
  2. 开源!mathAI 手写拍照自动能解高数题,还不快试试?
  3. Javascript中使用正则表达式进行数据验证
  4. python用递归法写斐波那契_python实现斐波那契数列: 递归+备忘录法+动态规划实现...
  5. [JS] Invalid Date
  6. php在线编辑器_8款功能强大的Java编辑器,建议收藏!「渡一教育」
  7. 人生第一个过万 Star 的 GitHub 项目诞生
  8. css中属性选择器用法总结c
  9. svn版本管理软件——创建svn私人仓库
  10. 中国居民身份证、通行证(含香港、澳门、台湾)资料整理,含编码规则
  11. Ubuntu18.04配置运行Kintinuous
  12. jena4.1.0安装及使用
  13. Lipschitz 条件
  14. Oracle 按日期分组
  15. 自由宣言-- I Have a Dream 马丁 路德 金
  16. 中文/英文]VC6 sp6补丁下载|VS6 sp6补丁下载
  17. 测试学习-104-在Linux干净卸载mysql
  18. 应对焦虑的时候,需要学会一次只解决一个问题
  19. 《财报就像一本故事书》刘顺仁(一) 山西出版集团山西人民出版社
  20. 【教程】php在图片上添加文字

热门文章

  1. 2015.10.26软件更新公告
  2. 网安大事件丨Fortinet对Apache Log4j漏洞利用的全面复盘与防御
  3. 如何用 js 获取虚拟键盘高度?(适用所有平台)
  4. 不同VLAN下实现网络互相通信(配置port trunk pvid vlan进行数据转发)
  5. ant design vue的ApiSelect选择器使用注意点
  6. JDBC--预编译的statement对数据库进行添加数据的操作
  7. 《算法笔记》4.4小节——算法初步->贪心
  8. 电子邮件内容安全刻不容缓
  9. 常见的加密方式有哪几种,各有哪些优缺点
  10. java项目编码转换不产生乱码,Java源码乱码问题 编码转换器