nginx rewrite 语法

nginx rewrite 语法

一 定义

Rewrite主要实现url地址重写, 以及地址重定向,就是将用户请求web服
务器的地址重新定向到其他URL的过程。

二 语法格式

reweite fiag 标记
break 中断,结束, 本条规则匹配完成后,重新发出请求,
通过新的uri进行匹配
location内部跳转
last 持续 本条规则匹配完成后,停止匹配,不在匹配后
面的规则
location 内部跳转
redirect 重定向:临时 302 持续的 来回
permanent 重定向:永久 301 持续的 来回

二 简单使用

if常用的条件及格式

条件 取反
= 精确匹配 一模一样 等于 !=
~ 过滤 支持正则 区分大小写 !~
~* 过滤 支持正则 不区分大小写 !~*
-d 判断目录是否存在
-f 判断文件是否存在
-x 判断是否有执行权限
-e 运算符检查文件,目录或符号链接是否存在

--nginx内置变量--

nginx内置变量
$http_user_agent 客户端浏览器
$request_uri 用户请求的uri(包含参数)
$host 用户访问Host内 域名
$remote_addr 用户的ip地地址
$args 只取出请求行里面的参数部分
$http_name name是http请求报文中的内容
$http_host $http_host为何查询不到???
$http_accept_language

2.1 if 判断浏览器类型返回值

#01 书写配置文件
[root@node02 conf.d]# cat if.conf 
server {
        listen 80;
        server_name rewrite.zhangyuzhou.com;
        root /code/rewrite;
        index index.html;
        default_type text/html;
	if ( $http_user_agent ~* "spider|bot" ){
	return 403 "别来了!!";	
}
}

#02 创建站点目录
mkdir -p  /code/rewrite
echo 123 >>/code/rewrite/index.html



#03 访问测试
curl -A zhangbingbingbot   -H:rewrite.zhangyuzhou.com   rewrite.zhangyuzhou.com 10.0.0.102

别来了!!别来了!!

解释含义
如果用户客户端spider或者bot (含包括这两个)访问,则返回403 “别来了”

curl 
-A 指定代理  设置Http请求头“User-Agent”的部分
-H  添加一个http header(http请求头);

2.2 set 变量的使用

##语法
Syntax:	set $variable value;
Default:	—
Context:	server, location, if

# 使用方法
set $zhangyuzhou  "zhang";	 	 设置zhangyuzhou变量 = zhangyuzhou
return 200  $zhangyuzhou ;		 返回码 200  然后加上$zhangyuzhou变量的内容 "zhang"

#01 需求
用户请求rewrite.zhangyuzhou.com.cn   用户访问 /code/rewrite/cn/index.html
用户请求rewrite.zhangyuzhou.com.jp   用户访问 /code/rewrite/jp/index.html


#02 配置文件
[root@node02 conf.d]# cat set.conf 
server {
        listen 80;
        server_name rewrite.zhangyuzhou.com;
        root /code/rewrite;
        index index.html;
        default_type text/html;
       if ( $host ~ '\.cn$' ) {
       rewrite ^/ http://rewrite.zhangyuzhou.com/cn/;
       }
       if ( $host ~ '\.jp$' ) {
      rewrite ^/ http://rewrite.zhangyuzhou.com/jp/;
      }
      }


#03 访问测试
[root@node02 conf.d]# curl  -H Host:rewrite.zhangyuzhou.com.cn  10.0.0.102  -L
zhangyuzhou cn
[root@node02 conf.d]# curl  -H Host:rewrite.zhangyuzhou.com.jp  10.0.0.102  -L
zhangyuzhou j

2.3 return返回功能

#语法: 
	return code [text]; 状态码 内容(文本 定向新的url)
	return  内容(文本 定向新的url)
return code URL;
return URL;
Default:	—
Context:	server, location, if



#01  #需求1:如果用户使用ie浏览器访问rewrite.zhangyuzhou.com则返回值字符串.


#02 书写配置文件
[root@node02 conf.d]# cat return.conf 
server {
	listen 80;
	server_name rewrite.zhangyuzhou.com;
	root /code/rewrite;
	index index.html;
	default_type text/html;
	if ($http_user_agent ~* "MSIE") {
	return 200 "请更换浏览器 使用edge/chrome/fire...";
	}
}

mkdir -p /code/rewrite
echo 1 >/code/rewrite/index.html


[root@node02 conf.d]# curl -A MSIE   -Host:rewrite.zhangyuzhou.com   rewrite.zhangyuzhou.com 10.0.0.102
请更换浏览器 使用edge/chrome/fire...请更换浏览器 使用edge/chrome/fire...



2.4 rewrite跳转功能

  • 基础语法
Syntax: rewrite regex replacement [flag];

rewrite   正则   替换成什么   [标记]; # 把regex 替换成replacement 标记[]
 sed      's#正则#替换成什么#   g'


reweite fiag 标记
break 中断,结束, 本条规则匹配完成后,重新发出请求,
通过新的uri进行匹配
location内部跳转
last 持续 本条规则匹配完成后,停止匹配,不在匹配后
面的规则
location 内部跳转
redirect 重定向:临时 302 持续的 来回
permanent 重定向:永久 301 持续的 来回
  • 多重匹配
#01 匹配
[root@node02 conf.d]# cat url.conf 
server {
	listen 80;
	server_name url.zhangyuzhou.com;
	root /code/url;
	
	location / {
	rewrite /1.html /2.html ;
	rewrite /2.html /3.html ;
	}
	location /2.html {
	rewrite /2.html /4.html ;
}
	location /3.html {
	rewrite /3.html /a.html ;
	}
}


[root@node02 conf.d]# curl  url.zhangyuzhou.com/1.html
a.html url


解释下:匹配 1.html ---2.html ---- 3.html ---a.html

  • break
rewrite 标记
break 跳转完成后,停止匹配其他任何规则,直接返回结果
last 调整完成后,重新使用新的uri,发起请求
#break
break 结束执行,返回结果

#01 配置
[root@node02 conf.d]# cat url.conf 
server {
	listen 80;
	server_name url.zhangyuzhou.com;
	root /code/url;
	
	location / {
	rewrite /1.html /2.html break ;
	rewrite /2.html /3.html ;
	}
	location /2.html {
	rewrite /2.html /4.html ;
}
	location /3.html {
	rewrite /3.html /a.html ;
	}
}

[root@node02 conf.d]# curl  url.zhangyuzhou.com/1.html
2.html url



  • last
#停止处理当前location中的规则, 然后以新的uri在server中进行匹配

[root@node02 conf.d]# cat url.conf 
server {
	listen 80;
	server_name url.zhangyuzhou.com;
	root /code/url;
	
	location / {
	rewrite /1.html /2.html last ;
	rewrite /2.html /3.html ;
	}
	location /2.html {
	rewrite /2.html /4.html ;
}
	location /3.html {
	rewrite /3.html /a.html ;
	}
}


[root@node02 conf.d]# curl  url.zhangyuzhou.com/1.html
4.html url


posted @   宁采臣open  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示