Nginx location和rewrite

1 location

从功能看 rewrite 和 location 似乎有点像,都能实现跳转,主要区别在于 rewrite 是在同一域名内更改获取资源的路径,而 location 是对一类路径做控制访问或反向代理,还可以proxy_pass 到其他机器。
rewrite 对访问的域名或者域名内的URL路径地址重写
location 对访问的路径做访问控制或者代理转发

1.1location的类别

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

1.2location常用的匹配规则

  • = :进行普通字符精确匹配,也就是完全匹配。
  • ^~ :表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其它 location。
  • ~ :区分大小写的匹配。
  • ~* :不区分大小写的匹配。
  • !~ :区分大小写的匹配取非。
  • !~* :不区分大小写的匹配取非。

1.3location优先级

  • 首先精确匹配 =
  • 其次前缀匹配 ^~
  • 其次是按文件中顺序的正则匹配 *
  • 然后匹配不带任何修饰的前缀匹配最后是交给 / 通用匹配

(1)location = / {}
=为精确匹配 / ,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配
再比如 location = /abc,则只匹配/abc ,/abc/或 /abcd不匹配。若 location /abc,则即匹配/abc 、/abcd/ 同时也匹配 /abc/。

(2)location / {}
因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 比如访问 / 和 /data, 则 / 匹配, /data 也匹配,
但若后面是正则表达式会和最长字符串优先匹配(最长匹配)

(3)location /documents/ {}
匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条

(4)location /documents/abc {}
匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条

(5)location ^~ /images/ {}
匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条

(6)location ~* .(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 结尾的请求
然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则

(7)location /images/abc {}
最长字符匹配到 /images/abc,优先级最低,继续往下搜索其它 location

(8)location ~ /images/abc {}
匹配以/images/abc 开头的,优先级次之,只有去掉 location ^~ /images/ 才会采用这一条

(9)location /images/abc/1.html {}
匹配/images/abc/1.html 文件,如果和正则location ~ /images/abc/1.html 相比,正则优先级更高

优先级总结
(location = 完整路径) > (location ^~ 路径) > (location ,* 正则顺序) > (location 部分起始路径) > (location /)
location 匹配
首先看 优先级:精确= > 前缀^~ > 正则,* > 一般 > 通用/
优先级相同:正则看上下顺序,上面的优先;一般匹配看长度,最长匹配的优先
精确、前缀、正则、一般 都没有匹配到,最后再看通用匹配

2 rewrite

rewrite功能
使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求
rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用,如 http://www.my.com/abc/bbs/index.php?a=1&b=2 只对/abc/bbs/index.php重写。

2.1rewrite 跳转实现

  • Nginx:通过ngx_http_rewrite_module 模块支持URL重写、支持if条件判断,但不支持else
  • 跳转:从一个 location跳转到另一个location,循环最多可以执行10次,超过后nginx将返回500错误
  • PCRE支持:perl兼容正则表达式的语法规则匹配重写模块 set 指令:创建新的变量并设其值

2.2rewrite执行顺序

1 、执行 server 块里面的 rewrite 指令。
2 、执行 location 匹配。
3、 执行选定的 location 中的 rewrite 指令

2.3rewrite语法格式

rewrite <regex> <replacement> [flag];
regex :表示正则匹配规则。
replacement :表示跳转后的内容。
flag :表示 rewrite 支持的 flag 标记。

2.4rewrite实验

基于域名

在自己主机上做域名与IP映射

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

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.132.88 www.smr.com www.xu.com

在这里插入图片描述

vim /usr/local/nginx/conf/nginx.conf
-----------------------------------------------------------------------------
server {
	listen       80;
	server_name  www.smr.com;		                  #域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.smr.com-access.log; #日志修改 nginx目录需手动创建
	location / {
	    #添加域名重定向
        if ($host = 'www.smr.com'){#$host为rewrite全局变量,代表请求主机头字段或主机名
			rewrite ^/(.*)$ http://www.xu.com/$1 permanent;  #$1为正则匹配的内容,即“域名/”之后的字符串
        }
        root   html;
        index  index.html index.htm;
    }
}

-------------------------------------------------------------
mkdir -p /var/log/nginx  #手动创建日志存放目录
nginx -t #检查语法是否错误
systemctl restart nginx

在这里插入图片描述
在这里插入图片描述
在浏览器中输入http://www.smr.com 页面网址会跳转成www.xu.com

在这里插入图片描述

基于客户端ip访问
[root@localhost ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.132.88 www.smr.com

在这里插入图片描述

vim /usr/local/nginx/conf/nginx.conf
--------------------------------------------------------------------------
server {
	listen       80;
	server_name  www.smr.com;		                        #域名修改	
	charset utf-8;
	access_log  /var/log/nginx/www.smr.com-access.log;		#日志修改

	#设置是否合法的IP标记
    set $rewrite true;							#设置变量$rewrite,变量值为boole值true
    #判断是否为合法IP
	if ($remote_addr = "192.168.132.88"){		#当客户端IP为192.168.52.100时,将变量值设为false,不进行重写
        set $rewrite false;
    }
	#除了合法IP,其它都是非法IP,进行重写 跳转维护页面
    if ($rewrite = true){			#当变量值为true时,进行重写
        rewrite (.+) /xu.html;	#将域名后边的路径重写成/weihu.html,例如www.smr.com/xu.html
    }
    location = /xu.html {
        root /var/www/html;			#网页返回/var/www/html/weihu.html的内容
    }
	
	location / {
        root   html;
        index  index.html index.htm;
    }
}

------------------------------------------------------------
mkdir -p /var/log/nginx                                #手动创建日志存放目录
mkdir -p /var/www/html                                 #创建weihu主页目录
echo "<h1>guagua taijun</h1>" > /var/www/html/xu.html   #创建主页内容
systemctl restart nginx                                #重启服务

在这里插入图片描述
在这里插入图片描述
本机192.168.132.88使用浏览器访问

在这里插入图片描述
换一台主机192.132.44
域名IP映射
在这里插入图片描述游览器访问
在这里插入图片描述

基于旧域名跳转

现在访问的是 http://bbs.smr.com/post/,现在需要将这个域名下面的访问都跳转到http://www.smr.com/bbs/post/
域名与IP解析

root@localhost ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.132.88 www.smr.com bbs.smr.com

在这里插入图片描述

vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  bbs.smr.com;		#域名修改	
	charset utf-8;
	#添加
	location /post {
        rewrite (.+) http://www.smr.com/bbs$1 permanent;#这里的$1为位置变量,代表/post
    }
	
	location / {
        root   html;
        index  index.html index.htm;
    }
}


mkdir -p /usr/local/nginx/html/bbs/post
echo "guasang"  >> /usr/local/nginx/html/bbs/post/1.html
systemctl restart nginx

在这里插入图片描述
在这里插入图片描述
浏览器中输入bbs.smr.com/post/1.html在这里插入图片描述

基于参数匹配跳转

现在访问http://www.smr.com/100-(100|200)-100.html 跳转到http://www.smr.com页面。

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

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.132.88 www.smr.com
vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.smr.com;		#域名修改	
	charset utf-8;
	
	if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        rewrite (.+) http://www.smr.com permanent;
    }

	location / {
        root   html;
        index  index.html index.htm;
    }
}
$request_uri:包含请求参数的原始URI,不包含主机名,如:http://www.my.com/abc/bbs/index.html?a=1&b=2 中的 /abc/bbs/index.php?a=1&b=2
$uri:这个变量指当前的请求URI,不包括任何参数,如:/abc/bbs/index.html
$document_uri:与$uri相同,这个变量指当前的请求URI,不包括任何传递参数,如:/abc/bbs/index.html

在这里插入图片描述
浏览器输入www.smr.com/100-100-100.html 或 www.smr.com/100-200-100.html 跳转到http://www.smr.com页面。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值