目录

  • 一、Nginx跳转的实现方式
  • 一、rewrite概述
  • 一、rewrite跳转实现
  • 二、常用的Nginx正则表达式
  • 三、rewrite的优先级
  • 四、rewrite的flag标记
  • 二、location匹配
  • 一、location分类
  • 二、location常用匹配规则
  • 三、location优先级
  • 四、location的实际使用
  • location和rewrite的区别
  • 实例
  • 一、基于域名跳转
  • 二、基于客户端ip地址跳转
  • 三、基于旧域名跳转到新域名后的加目录
  • 四、基于参数匹配(多余的)的跳转
  • 五、基于目录下所有PHP结尾的文件跳转
  • 六、基于最普通的一条url


一、Nginx跳转的实现方式

1、使用rewrite进行匹配跳转
2、if匹配全局变量跳转
3、使用location匹配在跳转

一、rewrite概述

rewrite 功能就是:使用 nginx 提供的全局变量或自己设置的变量,结合正则表达式和标记位实现 URL(我们访问的完整域名+路径) 重写以及重定向

一、rewrite跳转实现

①Nginx:支持URL重写、支持if条件判断(通过布尔值判断,成立为true,不成立为false),不支持else
②跳转:location跳转下一个location,循环最多10次,超过后Nginx将返回500错误
③PCRE支持:Perl兼容正则表达式的语法规则匹配
④重写模块set指令:创建新的变量并设定其值

二、常用的Nginx正则表达式

^ :匹配输入字符串的起始位置
$ :匹配输入字符串的结束位置
* :匹配前面的字符零次或多次
+ :匹配前面的字符一次或多次
? :匹配前面的字符零次或一次
. :匹配除“\n(回车)”之外的任何单个字符
\ :转义符,将后面的字符标记为一个特殊字符或一个原义字符或一个向后引用。
\d : 匹配春数字
\w :匹配字母或数字或下滑线或汉字
\s :匹配任意的空白符
\b :匹配单词的开始或结束
{n} :重复n次
{n,} :至少重复n次
{n,m} :重复n到m次
() :表达式的开始和结束位置
| : 或运算符
[]:定义匹配的字符范围
[n] :匹配单个字符n
[a-z0-9A-Z] :匹配所有大小写字母或数字

三、rewrite的优先级

1、server模块里面的rewrite指令
2、选定的location中的rewrite指令
3、选定的location中if中的rewrite指令

四、rewrite的flag标记

last:本条规则匹配完成后,继续向下匹配新的location URL规则,一般用在server和if中
break:本条规则匹配完成即终止,不在匹配后面的任何规则,一般用在location中
redirect:返回302临时重定向,浏览器地址栏会显示跳转后的URL地址
permanent:返回301永久重新向,浏览器地址栏会显示跳转后的URL地址
last与break的区别:
last:终止当前location的rewrite检测,还会继续重试location匹配并处理区块中的rewrite规则
break:终止当前location的rewrite检测,不再进行location匹配

二、location匹配

一、location分类

精准匹配:location = / {…}
一般匹配:location / {…}
正则匹配:location ~ / {…}

二、location常用匹配规则

= :进行普通字符精准匹配(完全匹配)
^~ :表示普通字符匹配,使用前缀匹配。匹配成功,则不再匹配其他location
~ :区分大小写匹配
~* :不区分大小写匹配
!~ :区分大小写匹配取非
!~* :不区分大小写匹配取非
@ :定义一个location,使用在内部定向的时候

三、location优先级

匹配文件:
(location = 完整路径) > (location ^~ 完整路径) > (location ~* 完整路径) > (location ~ 完整路径) > (location 完整路径) > (location /)
匹配文件的目录:
(location = 完整路径) > (location ^~ 完整路径) > (location ~ 完整路径) > (location ~* 完整路径) > (location 完整路径) > (location /)
相同类型的表达式,字符串长的优先匹配

四、location的实际使用

实际使用至少三个必用规则:
1、直接匹配网站根,通过域名访问网站首页比较频繁,使用这个加速处理。
2、处理静态文件请求(Nginx作为http服务器的强项),匹配模式为目录匹配或后缀匹配。
3、通用规则,如转发.php或.jsp后缀的动态请求到后端应用服务器

location和rewrite的区别

rewrite是对同一域名内更改获取资源的路径
location是对一类路径做控制访问或反向代理,也可以proxy_pass到其他设备

实例

一、基于域名跳转

[root@localhost ~]# vim /etc/hosts

添加映射

nginx if跳转proxy nginx url跳转_nginx

[root@localhost ~]# mkdir -p /var/log/nginx/ #创建日志目录
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf进入Nginx主配置文件
server_name  www.xiao.com #修改域名

access_log  /var/log/nginx/www.xiao.com-access.log #开启并对日志保存路径进行修改

if ($host = 'www.xiao.com'){ #$host为rewrite全局变量,代表请求主机头字段或主机名
                rewrite ^/(.*)$ http://www.ku.com/$1 permanent; 
                #^/(.*)$表示根后面的目录,$1表示^/(.*)$,permanent永久跳转
            }
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx

访问www.xiao.com,发现网页跳转到www.ku.com

nginx if跳转proxy nginx url跳转_html_02

二、基于客户端ip地址跳转

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

nginx if跳转proxy nginx url跳转_nginx if跳转proxy_03

set $rewrite true; #设置变量$rewrite,变量为布尔值true
#判断是否为否合法IP,设定ip192.168.238.150为合法地址,不进行跳转,直接访问
if ($remote_addr = "192.168.238.150"){
    set $rewrite false;
}
#设定除了合法地址,其他地址访问,跳转至更新页面
if ($rewrite = true){
    rewrite (.+) /gengxin.html;
}
location = /gengxin.html {
    root /var/www/html; #网页返回/var/www/html/gengxin.html的内容
}
[root@localhost ~]# mkdir -p /var/www/html/
[root@localhost ~]# echo '<h1>this is gengxin web!</h1>' > /var/www/html/gengxin.html
[root@localhost ~]# systemctl restart nginx

用192.168.238.100访问www.xiao.com跳转到更新页面

nginx if跳转proxy nginx url跳转_html_04


用192.168.238.150访问www.xiao.com,正常进入主页

nginx if跳转proxy nginx url跳转_html_05

三、基于旧域名跳转到新域名后的加目录

[root@localhost ~]# mkdir -p /usr/local/nginx/html/bbs/post
[root@localhost ~]# echo "<h1> this is 1.html</h1>" > /usr/local/nginx/html/bbs/post/1.html
[root@localhost ~]# echo "192.168.238.150 bbs.ku.com" >> /etc/hosts
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

nginx if跳转proxy nginx url跳转_nginx_06

[root@localhost ~]# systemctl restart nginx

访问www.xiao.com/post/1.html

nginx if跳转proxy nginx url跳转_nginx_07

四、基于参数匹配(多余的)的跳转

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

nginx if跳转proxy nginx url跳转_Nginx_08

[root@localhost ~]# systemctl restart nginx

访问www.xiao.com/100-100-12345.html,网页跳转到www.xiao.com

nginx if跳转proxy nginx url跳转_nginx_09

五、基于目录下所有PHP结尾的文件跳转

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

nginx if跳转proxy nginx url跳转_nginx_10

[root@localhost ~]# systemctl restart nginx

访问www.xiao.com/upload/123.php网页转到www.xiao.com

nginx if跳转proxy nginx url跳转_Nginx_11

六、基于最普通的一条url

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

nginx if跳转proxy nginx url跳转_nginx if跳转proxy_12

[root@localhost ~]# systemctl restart nginx

访问www.xiao.com/abc/123.html,网页跳转到www.xiao.com

nginx if跳转proxy nginx url跳转_html_13


小小总结:

location的优先级:

1、精准匹配

2、正则前缀匹配

3、~和~*

4、普通匹配