linux高阶-Nginx(九)-Rewrite相关功能

Nginx Rewrite相关功能

1. ngx_http_rewrite_module模块指令

  • Nginx服务器利用ngx_http_rewrite_module 模块解析和处理rewrite请求,此功能依靠 PCRE(perl compatibleregularex pression),因此编译之前要安装PCRE库,rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是非常有用的功能,比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的链接,就可以设置为访问,另外还可以在一定程度上提高网站的安全性。

1.1 if指令

  • 用于条件匹配判断,并根据条件判断结果选择不同的Nginx配置,可以配置在server或location块中进行配置,Nginx的if语法仅能使用if做单次判断,不支持使用if else或者if elif这样的多重判断,用法如下:
if (条件匹配) {
action
}
  • 使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为true,否则认为false,变量与表达式之间使用以下符号链接:
=:             #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false。
!=:             #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false。
~:             #表示在匹配过程中区分大小写字符,(可以通过正则表达式匹配),满足匹配条件为真,不满足为假。
!~:            #为区分大小写字符且匹配结果不匹配,不满足为真,满足为假。
~*:             #表示在匹配过程中不区分大小写字符,(可以通过正则表达式匹配),满足匹配条件为真,不满足为假。
!~*:            #为不区分大小字符且匹配结果不匹配,满足为假,不满足为真。
-f 和 ! -f:     #判断请求的文件是否存在和是否不存在
-d 和 ! -d:     #判断请求的目录是否存在和是否不存在。
-x 和 ! -x:     #判断文件是否可执行和是否不可执行。
-e 和 ! -e:     #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)
#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server {
  listen 80;
#  listen 443 ssl;
#  ssl_certificate /apps/nginx/certs/www.bokebi.tech.crt;
#  ssl_certificate_key /apps/nginx/certs/www.bokebi.tech.key;
  server_name www.bokebi.tech;
  location /pc {
    root html;
    index index.html;
    if ( $scheme = http ) { # 如果请求使用的是http协议,则输
出scheme is http
      echo "scheme is $scheme";
    }                                                       
#    if ( $scheme = https ) { # 如果请求使用的是https协议,则
输出scheme is https
#      echo "scheme is $scheme";
#    }
    if ( !-f $request_filename ) { # 如果当前请求的资源文件>的路径不存在,则输出file is not exist
      echo "file is not exist";
    }
  }
}

注: 如果$变量的值为空字符串或是以0开头的任意字符串,则if指令认为该条件为false,其他条件为true。
  • 浏览器访问www.bokebi.tech/pc/index.html

在这里插入图片描述

在这里插入图片描述

1.2 set指令

  • 指定key并给其定义一个变量,变量可以调用Nginx内置变量赋值给key,另外set定义格式为set $key v a l u e , 及 无 论 是 k e y 还 是 v a l u e 都 要 加 value,及无论是key还是value都要加 valuekeyvalue符号。
#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server {
  listen 80;
  server_name www.bokebi.tech;
  location /pc {
    root html;
    set $name bokebi;
    echo $name;
    set $port $server_port;
    echo $port;
  }
}
  • 浏览器访问www.bokebi.tech/pc

在这里插入图片描述

1.3 break指令

  • 用于中断当前相同作用域(location)中的其他Nginx配置,与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效,位于后面的指令配置就不再生效了,Nginx服务器在根据配置处理请求的过程中遇到该指令的时候,回到上一层作用域继续向下读取配置,该指令可以在server块和location块以及if块中使用,使用语法如下:
#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server {
  listen 80;
  server_name www.bokebi.tech;
  location /pc {
	  root html;
	  set $name bokebi;
	  echo $name;
	  break; #这个示例中,只会echo $name 变量的值,因为它在break的前面
	  set $port $server_port;
	  echo $port;
  }
}
  • 浏览器访问www.bokebi.tech/pc

在这里插入图片描述

1.4 return指令

  • 从nginx版本0.8.2开始支持,return用于完成对请求的处理,并直接向客户端返回响应状态码,比如其可以指定重定向URL(对于特殊重定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令后的所有配置都将不被执行,return可以在server、if和location块进行配置,用法如下:
return code; #返回给客户端指定的HTTP状态码
return code (text); #返回给客户端的状态码及响应体内容,可以调用变量
return code URL; #返回给客户端的URL地址

#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server{
  listen 80;
  server_name www.bokebi.tech;
  location /{
    root /apps/nginx/html/pc;
    default_type text/html;
    index index.html index.htm;
    if( $scheme = http ){
    #return 666;
    #return 666 "not allow http";
    #return 301 http://www.baidu.com;
    return 500 "service error";                             
    echo "if ---> $scheme"; #return后面的将不再执行
    }   
    if ( $scheme = https ){
    echo "if ---> $scheme";
    }   
  }
}
--------------------------------
//测试实验结果
curl --head http://www.bokebi.tech
HTTP/1.1 500 Internal Server Error
Server: nginx/1.16.1
Date: Sat, 11 Jan 2020 02:12:26 GMT
Content-Type: text/html
Content-Length: 13
Connection: keep-alive

1.5 rewrite_log指令

  • 设置是否开启记录ngx_http_rewrite_module模块日志记录到error_log日志文件当中,可以配置在http、server、location或if当中,需要日志级别为notice 。
#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server {
  listen 80;
  server_name www.bokebi.tech;
  location /pc {
  root html;
  index index.html index.htm;
  rewrite_log on;
  rewrite ^(.*)$ https://www.baidu.com$1;
  }
}
--------------------------------
//测试实验结果
curl http://www.bokebi.tech/pc

<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

//查看错误日志文件
2020/01/11 10:39:01 [notice] 1550#0: *36 rewritten redirect: "https://www.baidu.com/pc", client: 172.20.26.44, server: www.bokebi.tech, request: "GET /pc HTTP/1.1", host: "www.bokebi.tech"

2. rewrite指令

  • 通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI做具体处理,以下是URL和URI的具体介绍:

  • URI(universal resource identifier):通用资源标识符,标识一个资源的路径,可以不带协议。

  • URL(uniform resource location):统一资源定位符,是用于在Internet中描述资源的字符串,是URI的子集,主要包括传输协议(scheme)、主机(IP、端口号或者域名)和资源具体地址(目录和文件名)等三部分,一般格式为
    scheme://主机名[:端口号][/资源路径],如:http://www.a.com:8080/path/file/index.html就是一个

  • URL路径,URL必须带访问协议。

  • 每个URL都是一个URI,但是URI不都是URL。

#例如
http://example.org:8080/path/to/resource.txt #URI/URL
ftp://example.org/resource.txt #URI/URL
/absolute/path/to/resource.txt #URI
rewrite regex replacement [flag];
  • rewrite将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为表达式指定的新的URI。 注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查,隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制,如果替换后的URL是以http://或https://开头,则替换结果会直接以重向返回给客户端, 即永久重定向301

2.1 rewrite flag使用介绍

  • 利用nginx的rewrite的指令,可以实现url的重新跳转,rewrtie有四种不同的flag,分别是redirect(临时重定向)、permanent(永久重定向)、break和last。其中前两种是跳转型的flag,后两种是代理型,跳转型是指有客户端浏览器重新对新地址进行请求,代理型是在WEB服务器内部实现跳转的。
  • Syntax: rewrite regex replacement [flag]; #通过正则表达式处理用户请求并返回替换后的数据包。 Default: —Context: server, location, if
redirect;
#临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求;使用相对
路径,或者http://或https://开头,状态码:302
permanent;
#重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求,状态码:301
last;
#重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URL启动新一轮重写检查,不建议在
location中使用
break;
#重写完成后停止对当前URL在当前location中后续的其它重写操作,而后直接将匹配结果返还给客户端即结束循环并
返回数据给客户端,建议在location中使用

2.2 rewrite案例-域名永久与临时重定向

  • 要求:
    • 因业务需要,将访问源域名 www.magedu.net 的请求永久重定向到www.magedu.com 。
    • 临时重定向不会缓存域名解析记录(A记录),但是永久重定向会缓存。
#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server {
  listen 80;
  server_name www.bokebi.tech;
  location / {
  root html;
  index index.html index.htm;
#	rewrite ^(.*)$ https://www.baidu.com1$1 permanent;
#	rewrite ^(.*)$ https://www.baidu.com$1 redirect;
# rewrite / http://www.baidu.com permanent; #永久重定向
#	rewrite / http://www.magedu.com redirect; #临时重定向
  }
}

在这里插入图片描述

2.2.1 永久重定向
  • 域名永久重定向,京东早期的域名 www.360buy.com 由于与360公司类似,于是后期永久重定向到了 www.jd.com ,永久重定向会缓存DNS解析记录

  • 浏览器访问www.bokebi.tech

在这里插入图片描述

2.2.1 临时重定向
  • 域名临时重定向,告诉浏览器域名不是固定重定向到当前目标域名,后期可能随时会更改,因此浏览器不会缓存当前域名的解析记录,而浏览器会缓存永久重定向的DNS解析记录,这也是临时重定向与永久重定向最大的本质区别。

  • 浏览器访问www.bokebi.tech

在这里插入图片描述

2.2.3 二者区别
  • 永久重定向:返回的状态码是301,告诉浏览器域名是固定重定向到当前⽬标域名,后期不会更改了。拿上面的例子来说就是将www.hechunping.tech永久重定向到www.hechunping.com。
  • 临时重定向:返回的状态码是302,告诉浏览器域名不是固定重定向到当前⽬标域名,后期可能随时会更改,因此浏览器不会缓存当前域名的解析记录,⽽浏览器会缓存永久重定向的DNS解析记录,这也是临时重定向与永久重定向最⼤的本质区别。

2.3 rewrite案例-break与last

  • 访问about的请求被转发至images,而访问images传递请求再次被转发至images1,以此测试last和break分别有什么区别
2.3.1 break案例
  • break测试案例:当客户端访问break的时候,测试通过rewrite将URL重写为test1,然后再通过rewrite将test1重写为test2测试两条write规则最终哪一条生效,并且测试重写后的URL会不会到其他location重新匹配。
#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server {
  listen 80;
  server_name www.bokebi.tech;
  location /break {
  root html;
  index index.html index.htm;
  rewrite ^/break/(.*)$ /test1/$1 break;
  rewrite ^/test1/(.*)$ /test2/$1 break;
  }
}
------------------------------------
//测试实验结果
curl http://www.bokebi.tech/break/index.html

test1
2.3.2 last案例
  • last:对某个location的URL匹配成功后会停止当前location的后续rewrite规则,并结束当前location,然后将匹配生成的新URL跳转至其他location继续匹配,直到没有location可匹配后将最后一次location的数据返回给客户端。
#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server {
  listen 80;
  server_name www.bokebi.tech;
  location /break {
  root html;
  index index.html index.htm;
  rewrite ^/break/(.*)$ /test1/$1 last;
  }
  location /test1{
  root html;
  index index.html index.htm;
  rewrite ^/test1/(.*)$ /test2/$1 break;
  }
}

------------------------------------
//测试实验结果
curl http://www.bokebi.tech/break/index.html

test2
2.3.3 二者区别
  • break适用于不改变客户端访问方式,但是要将访问的目的URL做单次重写的场景,比如有V1/V2两个版本的网站前端页面并存,旧版本的网站数据已经保存到了statics不能丢失,但是要将访问新版本的资源重写到新的静态资源路径到
    新的目录static

  • last适用于要不改变客户端访问方式但是需做多次目的URL重写的场景,场景不是很多。

2.4 rewrite案例-自动跳转https

#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server{
	listen 80;
	server_name www.bokebi.tech;
	if ( $scheme = http ){
	rewrite ^(.*)$ https://server_name$request_uri permanent;
	}
}
server {
	listen 80;
	server_name www.bokebi.tech;
	ssl_certificate /apps/nginx/certs/www.bokebi.tech.crt;
	ssl_certificate_key /apps/nginx/certs/www.bokebi.tech.key;
	ssl_session_cache shared:sslcache:20m;
	ssl_session_timeout 10m;
	location / {
		root /apps/nginx/html/pc;
		index index.html;
	}
}

2.5 rewrite案例-判断文件是否存在

  • 要求:当用户访问到公司网站的时输入了一个错误的URL,可以将用户重定向至官网首页。
#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/pc.conf

server{
	listen 80;
	server_name www.bokebi.tech;
	location / {
	root /apps/nginx/html/pc;
	index index.html index.html;
	if ( !-f $request_filename ){
		rewrite ^(.*) http://www.bokebi.tech/index.html;
		}
	}
}
  • 浏览器访问www.bokebi.tech

在这里插入图片描述

3.nginx防盗链

  • 防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗链,referer就是之前的那个网站域名,正常的referer信息有以下几种:
none:请求报文首部没有referer首部,比如用户直接在浏览器输入域名访问web网站,就没有referer信息。
blocked:请求报文有referer首部,但无有效值,比如为空。
server_names:referer首部中包含本主机名及即nginx 监听的server_name。
arbitrary_string:自定义指定字符串,但可使用*作通配符。
regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头,例如: ~.*\.magedu\.com。
  • 正常通过搜索引擎搜索web网站并访问该网站的referer信息如下:
172.20.1.11 - - [11/Jan/2020:12:10:50 +0800] "GET /index.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
172.20.1.11 - - [11/Jan/2020:12:10:50 +0800] "GET /favicon.ico HTTP/1.1" 302 145 "http://www.bokebi.tech/index.html" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"

3.1 实现web盗链

  • 在www.bokebi.tech这个网站盗链www.bokebi.net网站的a1.jpg图片
//www.bokebi.tech网站的设置
cat /apps/nginx/conf/conf.d/pc.conf

server{
	listen 80;
	server_name www.bokebi.tech;
	location / {
	root /apps/nginx/html/pc;
	index index.html index.html;
	accesss_log /data/nginx/logs/www.bokebi.tech/access.log_json;
	}
}

//测试页面设置
cat /apps/nginx/html/pc/index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>盗链页面</title>
</head>
<body>
<a href="http://www.bokebi.net/hechunping/index.html">测试盗链</a>
<img src="http://www.bokebi.net/a1.jpg">
</body>
</html>

//www.bokebi.net网站的nginx设置
cat /apps/nginx/conf/conf.d/net.conf

server {
	listen 80;
	server_name www.bokebi.net;
	location ~* \.(png|jpg)$ {
		root html/images;
		access_log /data/nginx/logs/www.bokebi.net/access.log access.json;
	}
}

//查看测试图片
ls /apps/nginx/html/images
a1.jpg

//查看跳转页面信息
cat /apps/nginx/html/net/index.html 
主配置文件中access_json格式
    log_format access_json '{"@timestamp":"$time_iso8601",'    #json格式日志文件
    '"host":"$server_addr",'  '"clientip":"$remote_addr",'
    '"size":$body_bytes_sent,'
    '"responsetime":$request_time,'
    '"upstreamtime":"$upstream_response_time",'
    '"upstreamhost":"$upstream_addr",'
    '"http_host":"$host",'
    '"uri":"$uri",'
    '"domain":"$host",'
    '"xff":"$http_x_forwarded_for",'
    '"referer":"$http_referer",'
    '"tcp_xff":"$proxy_protocol_addr",'
    '"http_user_agent":"$http_user_agent",'
    '"status":"$status"}';
  • 浏览器访问www.bokebi.tech

在这里插入图片描述

  • 点击盗链链接跳转

在这里插入图片描述

  • 分别查看.tech/.net网站的access_json日志
  • 主要看referer后跟的域名,非本机则图片被盗链了

在这里插入图片描述

在这里插入图片描述

3.2 实现防盗链

#示例
//查看子配置文件得
cat /apps/nginx/conf/conf.d/net.conf

server {
	listen 80;
	server_name www.bokebi.net;
	location ~* \.(png|jpg)$ {
		root html/images;
		access_log /data/nginx/logs/www.bokebi.net/access.log access_json;
	    valid_referers none blocked server_names *.bokebi.net api.online.test/v1/hostlist ~\.google\. ~\.baidu\.; #定义有效的referer
		    if ($invalid_referer) { #假如是使用其他的无效的referer访问
			      return 403; #则返回状态码403
		}
	}
}

//查看测试图片信息
ls /apps/nginx/html/images
a1.jpg

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值