# Nginx Rewrite 场景实战

# 1.Rewrite 基本介绍

# 1.1 什么是 Rewrite

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

比如说京东,google、亚马逊都在使用

域名重写后域名
www.z.cnwww.amazon.cn
www.g.cnwww.google.cn
www.360buy.comwww.jd.com
58.combj.58.com
# 1.2 Rewrite 应用场景
  • 1. 地址跳转,用户访问 www.jd.com 这个 URL 时,将其定向至一个新的域名 m.jd.com
  • 2. 协议跳转,将用户通过 http 的请求协议重新跳转至 https 协议 (实现 https 主要手段)。
  • 3.URL 静态化,将动态 URL 地址显示为静态 URL 的一种技术,能提高搜索引擎抓取,并且能减少动态 URL 对外暴露过多的参数。
# 1.3 Rewrite 重写原理

2.png

# 1.4 Rewrite 重写模块
  • set 设置变量
  • if 语句判断
  • return 返回返回值或 URL
  • rewrite 重定向 URL

# 2.Rewrite 重写模块

# 2.1 if 条件判断指令
# 2.1.1 语法示例
Syntax: if (condition) { ... }
Default: —
Context: server, location
# ~ 模糊匹配
# ~* 不区分大小写的匹配
# !~ 不匹配
# = 精确匹配
# 2.1.2 场景示例
url.hmallleasing.com/index.html?id=1234 --》 proxy-->192.168.1.8:8080
# 需求:匹配 Nginx 请求中包含 id=2356 的,然后代理到 192.168.1.8 的 8080 端口
[root@web01 conf.d]# cat url.hmallleasing.com.conf
server {
	listen 80;
	server_name url.hmallleasing.com;
	default_type application/json;
	root /code;
	
	location / {
		index index.html;
		#\d 表示数字,{4} 表示 4 次,{4,8} 表示数字出现的次数是 4 到 8 次,如 gid=12345678 就符合该条件。
		if ( $request_uri ~* 'id=\d{4}' ) {
			proxy_pass http://192.168.1.8:8080;
		}
	}
}
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl reload nginx
[root@web01 conf.d]# curl -L -HHost:url.hmalllleasing.com http://192.168.1.7/?id=2356
# 2.2 set 设定变量指令
# 2.2.1 语法示例
Syntax: set $variable value;
Default: —
Context: server, location, if
# 2.2.2 场景示例
# 需求:通过 user_agent(Header)拦截压测测试工具
# user_agent:
# chrome ,firefox, curl/7.29.0
# 1. 确认来请求的用户是使用的 curl 命令,如果是则做一个标记,设置为 1;
# 2. 判断标记,如果标记的值假设为 1,我们就拒绝,如果不为 1 则不处理;
[root@web01 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	index index.html;
		
	location / {
		if ($http_user_agent ~* "firefox|curl|Wget|ApacheBench") {
			set $deny_user_agent 1;
		}
		if ($deny_user_agent = 1){
			return 403;
		}
	}
}
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl reload nginx
#默认 curl 测试拒绝场景(-A 指定 user_agent)
[root@web01 conf.d]# curl -L -I -A "curl" -HHost:url.hmallleasing.com http://192.168.1.7/
HTTP/1.1 403 Forbidden
Server: nginx/1.26.1
Date: Thu, 04 Sep 2025 07:36:43 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
#默认 chrome 测试拒绝场景(-A 指定 user_agent)
[root@web01 conf.d]# curl -L -I -A "chrome" -HHost:url.hmallleasing.com http://192.168.1.7/
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Thu, 04 Sep 2025 07:37:42 GMT
Content-Type: text/html
Content-Length: 6
Last-Modified: Thu, 04 Sep 2025 07:32:58 GMT
Connection: keep-alive
ETag: "68b940aa-6"
Accept-Ranges: bytes
# 2.3 return 返回数据指令
# 2.3.1 语法示例
Syntax: return code;
return code [text];
return URL;
Default: —
Context: server, location, if
# 2.3.2 场景实践
# 需求:客户端使用 IE 浏览器访问站点,则返回段字符串或返回错误
[root@web01]# cat url.hmallleasing.com.conf
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	location / {
		index index.html;
		default_type text/html;
		if ( $http_user_agent ~* "MSIE|firefox" ) {
			# 返回字符串
			return 200 "Change browser";
			# 返回状态码
			return 500;
			# 返回 URL,进行站点跳转
			return 302 https://www.jd.com 
		}
	}
}
# 模拟测试 1
[root@web01 conf.d]# curl -L -A "firefox" -HHost:url.hmallleasing.com http://192.168.1.7/
Change browser
# 模拟测试 2
[root@web01 conf.d]# curl -I -L -A  "firefox" -HHost:url.hmallleasing.com http://192.168.1.7/
HTTP/1.1 500 Internal Server Error
Server: nginx/1.26.1
Date: Thu, 04 Sep 2025 07:52:36 GMT
Content-Type: text/html
Content-Length: 177
Connection: close
# 模拟测试 3
[root@web01 conf.d]# curl -I -L -A  "firefox" -HHost:url.hmallleasing.com http://192.168.1.7/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.26.1
Date: Thu, 04 Sep 2025 07:51:32 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: http://www.qq.com
HTTP/1.1 302 Moved Temporarily
Server: stgw
Date: Thu, 04 Sep 2025 07:51:52 GMT
Content-Type: text/html
Content-Length: 137
Connection: keep-alive
Location: https://www.qq.com/
HTTP/1.1 200 OK
Date: Thu, 04 Sep 2025 07:51:52 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 0
Connection: keep-alive
Server: tRPC-Gateway
Inews_trace_id: 40396647090904155152
Inews-Trace-Id: 40396647090904155152
X-Upstream-Latency: 2
X-Proxy-Latency: 1
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors none

# 3. Rewrite 重写 Flag

rewrite 主要是用来重写 URL 或者跳转 URL 的指令。

#rewrite 表达式可以应用在 server,location, if 标签下
# 关键字 正则 替代内容 flag 标记
Syntax: rewrite regex replacement [flag];
Default: --
Context: server, location, if
#flag
last      #本条规则匹配完成后,继续向下匹配新的 location URI 规则
break     #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect  #返回 302 临时重定向,地址栏会显示跳转后的地址
permanent #返回 301 永久重定向,地址栏会显示跳转后的地址
# **3.1 ** 测试代码准备
[root@web01]# cat url.hmallleasing.com.conf
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
    location / {
		rewrite /1.html /2.html;
		rewrite /2.html /3.html;
	}
	
	location /2.html {
		rewrite /2.html /a.html;
	}
	
	location /3.html {
		rewrite /3.html /b.html;
	}
}
[root@web01 conf.d]# nginx -tl
[root@web01 conf.d]# systemctl reload nginx
#准备对应代码
[root@web01]# echo "1.html" >/code/1.html
[root@web01]# echo "2.html" >/code/2.html
[root@web01]# echo "3.html" >/code/3.html
[root@web01]# echo "a.html" >/code/a.html
[root@web01]# echo "b.html" >/code/b.html
#测试结果:当请求 / 1.html,最终将会访问 /b.html
[root@web01 conf.d]# curl -HHost:url.hmallleasing.com http://192.168.1.7/1.html
b.html
# 3.2 Break 与 last
# 3.2.1 为代码添加 Break
#测试结果:当请求 / 1.html,最终会访问 / 2.html
#因为:在 location {} 内部,遇到 break,本 location {} 内以及后面的所有 location {} 内的所有指令都不再执行。
[root@web01 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	#网站维护示例
	#rewrite  ^(.*)$ /weihu.html break;
	
    location / {
		rewrite /1.html /2.html break;
		rewrite /2.html /3.html;
	}
	
	location /2.html {
		rewrite /2.html /a.html;
	}
	
	location /3.html {
		rewrite /3.html /b.html;
	}
}
[root@web01 conf.d]# curl -HHost:url.hmallleasing.com http://192.168.1.7/1.html
2.html
# 3.2.2 为代码添加 last
# 测试结果:当请求 / 1.html,最终会访问 /a.html
# 因为:在 location {} 内部,遇到 last,本 location {} 内后续指令不再执行,
# 而重写后的 url 会对所在的 server {...} 标签重新发起请求,从头到尾匹配一遍规则,哪个匹配则执行哪个。
[root@web01 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
    location / {
		rewrite /1.html /2.html last;
		rewrite /2.html /3.html;
	}
	
	location /2.html {
		rewrite /2.html /a.html;
	}
	
	location /3.html {
		rewrite /3.html /b.html;
	}
}
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl reload nginx
[root@web01 conf.d]# curl -HHost:url.hmallleasing.com http://192.168.1.7/1.html
a.html
# 3.2.3 break 与 last 区别
  • 当 rewrite 规则遇到 break 后,本 location {} 与其他 location {} 的所有规则都不执行。
  • 当 rewrite 规则遇到 last 后,本 location {} 里后续规则不执行,但重写后的 url 会再次从头开始匹配所有 Location,哪个匹配执行哪个。
# 3.3 redirect 与 permanent
# 3.3.1 为代码添加 redirect
[root@web01]# cat url.hmallleasing.com.conf
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	location / {
		rewrite /1.html /2.html redirect;
		rewrite /2.html /3.html;
	}
}
# 3.3.2 为代码添加 permanent
# 跳转后状态码为 301
[root@web01]# cat url.hmallleasing.com.conf
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	location / {
		rewrite /1.html /2.html permanent;
		rewrite /2.html /3.html;
	}
}
# 3.3.3 redirect 与 permanent 区别
Flag跳转状态码排名情况
redirect临时跳转302对旧网站无影响,新网站会有排名(不会)
permanent永久跳转301新跳转网站有排名,旧网站排名会被清空(缓存下来)

# 4.Rewrite 生产案例实践

# 4.1 Rewrite 跳转示例 1
# 需求:根据用户浏览器请求头中携带的语言调度到不同的页面
# /zh
# /en
url.hmallleasing.com --》hmallleasing.com/zh
url.hmallleasing.com --》hmallleasing.com/en
[root@web01 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	if ($http_accept_language ~* "zh-CN|zh") {
		set $language zh;
	}
	
	if ($http_accept_language ~* "en") {
		set $language en;
	}
	
	rewrite ^/$ /$language; #根据语言跳转对应的站点
	location / {
		index index.html;
	}
}
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl reload nginx
[root@web01 conf.d]# mkdir /code/zh  -p
[root@web01 conf.d]# mkdir /code/en  -p
[root@web01 conf.d]# echo "zh...." > /code/zh/index.html
[root@web01 conf.d]# echo "en...." > /code/en/index.html
# 4.2 Rewrite 跳转示例 2
# 需求:用户通过手机设备访问 url.hmallleasing.com,跳转至 url.hmallleasing.com/m
[root@web01 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	if ($http_user_agent ~* "android|iphone|ipad") {
		rewrite ^/$ /m;
	}
}
[root@web01 conf.d]# echo "pc site" > /code/index.html 
[root@web01 conf.d]# mkdir /code/m
[root@web01 conf.d]# echo "m site" > /code/m/index.html
# 4.3 Rewrite 跳转示例 3
# 需求:用户通过手机设备访问 url.hmallleasing.com 跳转至 m.hmallleasing.com
#1. 准备 PC 站
[root@web01 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	if ($http_user_agent ~* "android|iphone|ipad") {
		#return 302 http://m.hmallleasing.com;
		rewrite ^/$ http://m.hmallleasing.com;
	}
}
[root@web01 conf.d]# echo "web01 pc site" > /code/index.html
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl reload nginx
#2. 准备手机站
[root@web02 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name m.hmallleasing.com;
	root /code/m;
	
	location / {
		index index.html;
	}
}
[root@web02 conf.d]# mkdir /code/m -p
[root@web02 conf.d]# echo "web02-site" > /code/m/index.html
[root@web02 conf.d]# nginx -t
[root@web02 conf.d]# systemctl reload nginx
# 4.4 Rewrite 跳转示例 4
# 需求:用户通过 http 协议请求,能自动跳转至 https 协议。
[root@web02 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	#rewrite ^(.*)$ https://$server_name$1 redirect;
	return 302 https://$server_name$request_uri;
}
server {
	listen 443 ssl;
	server_name url.hmallleasing.com;
        ssl_prefer_server_ciphers on;
        ssl_certificate  /etc/nginx/sslkey/hmallleasing.com.pem;
        ssl_certificate_key  /etc/nginx/sslkey/hmallleasing.com.key;
	
	location / {
		index index.html;
		root /code;
	}
}
[root@web02 ~]# cd /etc/nginx/
[root@web02 nginx]# mkdir sslkey
[root@web02 nginx]# cd ssl/
[root@web02 ssl]# ll
-rw-r--r-- 1 root root 1675 Jun 14 22:01 hmallleasing.com.key
-rw-r--r-- 1 root root 4784 Jun 14 22:01 hmallleasing.com.pem
[root@web02 conf.d]# echo "hello https" > /code/index.html
# 4.5 Rewrite 跳转示例 5
# 需求:网站在维护过程中,希望用户访问所有网站重定向至一个维护页面
[root@web01]# cat url.hmallleasing.com.conf
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	#配置示例
	rewrite ^(.*)$ /wh.html break;
	
	location / {
		index index.html;
	}
}
[root@web02 conf.d]# echo "wh....." > /code/wh.html
[root@web02 conf.d]# nginx -t
[root@web02 conf.d]# systemctl reload nginx
# 4.6 Rewrite 跳转示例 6
# 需求:当服务器遇到 403 404 502 等错误时,自动转到临时维护的静态页。[https://404.life/]
[root@web02 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	location / {
		index index.html;
	}
	#配置示例
	error_page 404 403 502 = @tempdown;	
	location @tempdown {
		rewrite ^(.*)$ /wh.html break;
	}
}
[root@web02 conf.d]# nginx -t
[root@web02 conf.d]# systemctl reload nginx
# 4.7 Rewrite 跳转示例 7
# 公司网站在停机维护时,指定的 IP 能够正常访问,其他的 IP 跳转到维护页。
[root@web02 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	#1. 在 server 层下设定 ip 变量值为 0
	set $ip 0;
	#2. 如果来源 IP 是 10.0.0.101 102 则设定变量为 ip 变量为 1
	if ($remote_addr ~ "192.168.40.1|192.168.40.7") {
		set $ip 1;
	}
	
	#3. 如果来源 IP 不是 10.0.0.101 102、则跳转至 /code/wh.html 这个页面,否则不做任何处理
	if ($ip = 0) {
		rewrite ^(.*)$ /wh.html break;
	}
	#--> 如果想针对某个 location 进行操作,则将如上配置写入 location 中即可
	location / {
		index index.html;
	}
}
[root@web02 conf.d]# nginx -t
[root@web02 conf.d]# systemctl reload nginx
#测试 192.168.40.51 访问
[root@db01 ~]# curl -HHost:url.hmallleasing.com http://192.168.40.8
wh.....
#测试 192.168.40.7 访问
[root@web01 ~]# curl -HHost:url.hmallleasing.com http://192.168.40.8
hello https
# 4.8 Rewrite 跳转示例 8
# 需求:公司网站后台 /admin,只允许公司的出口公网 IP 可以访问,其他的 IP 访问全部返回 500,或直接跳转至首页
#限制访问来源 IP
[[root@web02 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	location / {
		index index.html;
	}
	location /admin {
		set $ip 0;
		if ($remote_addr ~ "192.168.40.1|192.168.40.7") {
			set $ip 1;
		}
	
		if ($ip = 0){
			return 404;
			#rewrite /(.*)$ https://url.hmallleasing.com redirect;
		}
	}
}	
[root@web02 conf.d]# mkdir /code/admin
[root@web02 conf.d]# echo "admin site" > /code/admin/index.html
[root@web02 conf.d]# nginx -t
[root@web02 conf.d]# systemctl reload nginx
#测试 192.168.40.7 访问
[root@web01 ~]# curl -HHost:url.hmallleasing.com http://192.168.40.8/admin/index.html
admin site
#测试 192.168.40.51 访问
[root@db01 ~]# curl -L -HHost:url.hmallleasing.com http://192.168.40.8/admin/index.html
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.26.1</center>
</body>
</html>
# 4.9 Rewrite 跳转示例 9
#需求:请求 api.hmallleasing.com/bbb,实际请求:http://demo:27610/api/bbb
#http://demo:27610/api/bbb 中 api 为请求二级域名,bbb 为请求 uri
[root@web02 conf.d]# cat url.hmallleasing.com.conf 
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	location / {
		index index.html;
		if ( $http_host ~* (.*)\.(.*)\.(.*) ) {
			set $domain $1;
		}
		rewrite ^/(.*) http://demo:27610/$domain$request_uri redirect;
	}
}
[root@web02 conf.d]# nginx -t
[root@web02 conf.d]# systemctl reload nginx
[root@web01 ~]# curl -I -HHost:url.hmallleasing.com http://192.168.40.8/bbb
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.26.1
Date: Fri, 05 Sep 2025 14:26:44 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: http://demo:27610/url/bbb
# 4.10 Rewrite 跳转示例 10

现有两台服务器,想要实现 http://url.hmallleasing.com/index.php?r=sur/index/sid/613192/lang/zh-Hans 若访问资源为 /index.php?r=survey... 则跳转到 http://sur.hmallleasing.com/index.php?r=survey/index/sid/613192/lang/zh-Hans

请求的域名:http://url.hmallleasing.com/index.php?r=survey/index/sid/613192/lang/zh-Hans

替换后域名:http://sur.hmallleasing.com/index.php?r=survey/index/sid/613192/lang/zh-Hans

# 变量 $args 为?之后的参数
[root@web01 conf.d]# cat /etc/nginx/conf.d/url.hmallleasing.com.conf
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	index index.html;
	
	location / {
		if ($args ~* "r=survey") {
		    # ? 这个尾缀,重定向的目标地址结尾处如果加了?号,则不会再转发传递过来原地址问号内容
			rewrite ^/(.*) http://sur.hmallleasing.com$request_uri? redirect;
		}
	}
}
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl reload nginx
# 测试
[root@web01 conf.d]# curl -I -HHost:url.hmallleasing.com http://192.168.40.7/index.php?r=survey/index/sid/613192/lang/zh-Hans
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.26.1
Date: Sat, 06 Sep 2025 06:31:24 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: http://sur.hmallleasing.com/index.php?r=survey/index/sid/613192/lang/zh-Hans
# 4.11 Rewrite 跳转示例 11

需求:将用户请求 http://url.hmallleasing.net/?id=2,替换为 http://url.hmallleasing.net/id/2.html

  • 1. 必须是请求 id? 判断用户请求的是 id 这个关键参数;
  • 2. 提取整个 uri 中的两个字段, 将左边的做成一个变量,将右边的做成一个变量;
  • 3. 如果来源为 id 成立的话,则执行 rewirte;
[root@web01 conf.d]# cat /etc/nginx/conf.d/url.hmallleasing.com.conf
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	location / {
		if ($args ~* "id") {
			set $OK 1;
		}
		
		if ($args ~* (.*)\=(.*)) {
			set $id $1;
			set $number $2;
		}
		
		if ($OK = "1") {
		# ? 这个尾缀,重定向的目标地址结尾处如果加了?号,则不会再转发传递过来原地址问号内容
		rewrite ^(.*)$ http://${server_name}/${id}/${number}.html? last;
		}
	}
}	
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl reload nginx
#测试
[root@web01 conf.d]#  curl -I -HHost:url.hmallleasing.com http://192.168.40.7/?id=2
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.26.1
Date: Sat, 06 Sep 2025 06:49:24 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: http://url.hmallleasing.com/id/2.html
# 4.12 禁止搜索引擎爬取站点
# 需求:通过 user_agent 防止搜索引擎的爬取
[root@web01 conf.d]# cat /etc/nginx/conf.d/url.hmallleasing.com.conf
server {
	listen 80;
	server_name url.hmallleasing.com;
	root /code;
	
	location / {
  		index index.html;
		if ($http_user_agent ~* "YisouSpider|YoudaoBot|tt") {
			return 403;
		}
	}
}	
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl reload nginx
#测试
[root@web01 conf.d]# curl -I -A "YisouSpider" -HHost:url.hmallleasing.com http://192.168.40.7/
HTTP/1.1 403 Forbidden
Server: nginx/1.26.1
Date: Sat, 06 Sep 2025 06:59:32 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@web01 conf.d]# curl -I -A "ApacheBench" -HHost:url.hmallleasing.com http://192.168.40.7/
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sat, 06 Sep 2025 06:59:32 GMT
Content-Type: text/html
Content-Length: 12
Last-Modified: Sat, 06 Sep 2025 06:58:31 GMT
Connection: keep-alive
ETag: "68bbdb97-c"
Accept-Ranges: bytes
[root@web01 conf.d]# curl -I -A "TT" -HHost:url.hmallleasing.com http://192.168.40.7/
HTTP/1.1 403 Forbidden
Server: nginx/1.26.1
Date: Sat, 06 Sep 2025 06:59:32 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive
[root@web01 conf.d]# curl -I -A "wget" -HHost:url.hmallleasing.com http://192.168.40.7/
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Sat, 06 Sep 2025 06:59:34 GMT
Content-Type: text/html
Content-Length: 12
Last-Modified: Sat, 06 Sep 2025 06:58:31 GMT
Connection: keep-alive
ETag: "68bbdb97-c"
Accept-Ranges: bytes
# 4.13 禁止站点资源被盗用

防盗链,指的是防止资源被其他网站恶意盗用。如何避免网站资源被盗用:可以根据客户端请求所携带的 Referer 信息来验证请求的合法性,因为 Referer 会告诉服务器它是丛哪一个域名点击过来的;

基于 Referer 限制盗链:

  • 优点:规则简单、配置和使用都很方便,
  • 缺点:防盗链所依赖的 Referer 验证信息是可以伪造的,并非 100% 可靠,但基于 Referer 限制盗链的方式,可以限制绝大多数盗链情况。
# 4.13.1 环境准备
角色IP域名
盗链节点(偷取资源)10.0.0.7tou.hmallleasing.com
被盗链节点10.0.0.8image.hmallleasing.com
# 4.13.2 配置被盗链节点
# 在 10.0.0.8 节点配置 Nginx 站点信息
[root@web02 ~]# cat /etc/nginx/conf.d/image.hmallleaing.com.conf
server {
	listen 80;
	server_name image.hmallleasing.com;
	root /code;
	
	location / {
		index index.html;
	}
}
[root@web02 ~]# nginx -t
[root@web02 ~]# systemctl reload nginx
# 准备图片
[root@web02 code]# ll /code/xld.jpg 
-rw-r--r-- 1 root root 15817 Sep  6 15:38 /code/xld.jpg
# 4.13.3 配置盗链节点
# 在 10.0.0.7 节点配置 Nginx 站点信息
[root@web01 conf.d]# cat /etc/nginx/conf.d/tou.hmallleasing.com.conf
server {
	listen 80;
	server_name tou.hmallleasing.com;
	
	location / {
		root /code;
		index index.html;
	}
}
[root@web01 conf.d]# cat /code/index.html
<html>
<head>
	<meta charset="utf-8">
	<title>hmallleasing.com</title>
</head>
	<img src="http://image.hmallleasing.com/xld.jpg"/>
</html>
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl reload nginx

使用浏览器能正常访问到偷链的后的图片资源

2.jpg

# 4.13.4 防盗链配置语法
# 基于 http_referer 防止资源被盗用
Syntax: valid_referers none | blocked | server_names | string ...;
Default: —
Context: server, location
#none: Referer 来源头部为空的情况
#blocked: Referer 来源头部不为空,直接填写允许的域名即可
#server_names: 来源头部包含当前的域名,可以正则匹配
# 4.13.5 防盗链配置实践

配置所有来自 *.hmallleasing.com 都可以访问到 image.oldxu.net 站点的图片如果来源域名不在这个列表中,那么 $invalid_referer 变量值为 1,后续通过 if 判断,进行错误返回;

[root@web02 code]# cat /etc/nginx/conf.d/image.hmallleaing.com.conf 
server {
	listen 80;
	server_name image.hmallleasing.com;
	root /code;
	
	location / {
		index index.html;
	}
	location ~ .*\.(jpg|jpeg|gif|png)$ {
		# 来源域名如何合法,invalid_referer 这个变量被设置为 0,否则为 1
		valid_referers none blocked *.hmallleasing.net;
		if ($invalid_referer) {
			return 403;
			# 当然也可以返回一张图片给用户
			#rewrite ^(.*)$ /error.jpg break;
		}
	}	
	
	   # 允许 google、baidu、等站点能够(盗链)资源,那么则可以通过 server_names 开放
       # location ~ .*\.(jpg|jpeg|gif|png)$ {
       #         # 来源域名如何合法,invalid_referer 这个变量被设置为 0,否则为 1
       #         valid_referers none blocked *.hmallleasing.net server_names ~\.google\.~\.baidu\.;
       #         if ($invalid_referer) {
       #                 return 403;
       #                 # 当然也可以返回一张图片给用户
       #                 #rewrite ^(.*)$ /error.jpg break;
       #         }
       #}
}
[root@web02 code]# nginx -t
[root@web02 code]# systemctl reload nginx

4.13.6 防盗链结果验证

1.jpg

此文章已被阅读次数:正在加载...更新于

请我喝[茶]~( ̄▽ ̄)~*

Xu Yong 微信支付

微信支付

Xu Yong 支付宝

支付宝