NginxRewrite相關(guān)功能

Nginx服務(wù)器利?ngx_http_rewrite_module 模塊解析和處理rewrite請(qǐng)求,此功能依靠 PCRE(perl compatible regularexpression),因此編譯之前要安裝PCRE庫,rewrite是nginx服務(wù)器的重要功能之?,?于實(shí)現(xiàn)URL的重寫,URL的重寫是?常有?的功能,?如它可以在我們改變?站結(jié)構(gòu)之后,不需要客?端修改原來的書簽,也?需其他?站修改我們的鏈接,就可以設(shè)置為訪問,另外還可以在?定程度上提??站的安全性。

1 ngx_http_rewrite_module模塊指令

官方文檔:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html

1.1 if指令

# ?于條件匹配判斷,并根據(jù)條件判斷結(jié)果選擇不同的Nginx配置,可以配置在server或location塊中進(jìn)?配置,Nginx的if語法僅能使?if做單次判斷,不?持使?if else或者if elif這樣的多重判斷,?法如下:

if (條件匹配) {
  action
}

# 使?正則表達(dá)式對(duì)變量進(jìn)?匹配,匹配成功時(shí)if指令認(rèn)為條件為true,否則認(rèn)為false,變量與表達(dá)式之間使?以下符號(hào)鏈接:
=   #?較變量和字符串是否相等,相等時(shí)if指令認(rèn)為該條件為true,反之為false。
!=  #?較變量和字符串是否不相等,不相等時(shí)if指令認(rèn)為條件為true,反之為false。
~   #表?在匹配過程中區(qū)分??寫字符,(可以通過正則表達(dá)式匹配),滿?匹配條件為真,不滿?為假。
!~  #為區(qū)分??寫字符且匹配結(jié)果不匹配,不滿?為真,滿?為假。
~*  #表?在匹配過程中不區(qū)分??寫字符,(可以通過正則表達(dá)式匹配),滿?匹配條件為真,不滿?為假。
!~* #為不區(qū)分??字符且匹配結(jié)果不匹配,滿?為假,不滿?為真。
-f 和 ! -f  #判斷請(qǐng)求的?件是否存在和是否不存在
-d 和 ! -d  #判斷請(qǐng)求的?錄是否存在和是否不存在。
-x 和 ! -x  #判斷?件是否可執(zhí)?和是否不可執(zhí)?。
-e 和 ! -e  #判斷請(qǐng)求的?件或?錄是否存在和是否不存在(包括?件,?錄,軟鏈接)。

# 示例
[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  listen 443 ssl;
  ssl_certificate /apps/nginx/certs/www.hechunping.tech.crt;
  ssl_certificate_key /apps/nginx/certs/www.hechunping.tech.key;
  server_name www.hechunping.tech;
  location /pc {
    root html;
    index index.html;
    if ( $scheme = http ) { # 如果請(qǐng)求使用的是http協(xié)議,則輸出scheme is http
      echo "scheme is $scheme";
    }
    if ( $scheme = https ) { # 如果請(qǐng)求使用的是https協(xié)議,則輸出scheme is https
      echo "scheme is $scheme";
    }
    if ( !-f $request_filename ) { # 如果當(dāng)前請(qǐng)求的資源文件的路徑不存在,則輸出file is not exist
      echo "file is not exist";
    }
  }
}
[root@CentOS7-01 ~]#ls /apps/nginx/html/pc/
index.html
[root@CentOS7-01 ~]#curl http://www.hechunping.tech/pc/index.html
scheme is http
[root@CentOS7-01 ~]#curl -k /apps/nginx/certs/www.hechunping.tech.key https://www.hechunping.tech/pc/index.html
curl: (3) <url> malformed
scheme is https
[root@CentOS7-01 ~]#curl http://www.hechunping.tech/pc/index111.html
file is not exist

注: 如果$變量的值為空字符串或是以0開頭的任意字符串,則if指令認(rèn)為該條件為false,其他條件為true。

1.2 set指令

指定key并給其定義?個(gè)變量,變量可以調(diào)?Nginx內(nèi)置變量賦值給key,另外set定義格式為set $key $value,則?論是key還是value都要加$符號(hào)。

# 示例
[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  server_name www.hechunping.tech;
  location /pc {
    set $name hechunping;
    echo $name;
    set $port $server_port;
    echo $port;
  }
}
[root@CentOS7-01 ~]#curl http://www.hechunping.tech/pc
hechunping
80

1.3 break指令

?于中斷當(dāng)前相同作?域(location)中的其他Nginx配置,與該指令處于同?作?域的Nginx配置中,位于它前?的配置?效,位于后?的指令配置就不再?效了,Nginx服務(wù)器在根據(jù)配置處理請(qǐng)求的過程中遇到該指令的時(shí)候,回到上?層作?域繼續(xù)向下讀取配置,該指令可以在server塊和location塊以及if塊中使?,使?語法如下:

[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  server_name www.hechunping.tech;
  location /pc {
    set $name hechunping;
    echo $name;
    break; #這個(gè)示例中,只會(huì)echo $name變量的值,因?yàn)樗赽reak的前面
    set $port $server_port;
    echo $port;
  }
}
[root@CentOS7-01 ~]#curl http://www.hechunping.tech/pc
hechunping

1.4 return指令

從nginx版本0.8.2開始?持,return?于完成對(duì)請(qǐng)求的處理,并直接向客?端返回響應(yīng)狀態(tài)碼,?如其可以指定重定向URL(對(duì)于特殊重定向狀態(tài)碼,301/302等) 或者是指定提??本內(nèi)容(對(duì)于特殊狀態(tài)碼403/500等),處于此指令后的所有配置都將不被執(zhí)?,return可以在server、if和location塊進(jìn)?配置,?法如下:

return code;        #返回給客?端指定的HTTP狀態(tài)碼
return code (text); #返回給客?端的狀態(tài)碼及響應(yīng)體內(nèi)容,可以調(diào)?變量
return code URL;    #返回給客?端的URL地址

# 示例,80端口跳轉(zhuǎn)https
[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  server_name www.hechunping.tech;
  return 301 https://$host$request_uri;
}
server {
  listen 443 ssl;
  server_name www.hechunping.tech;
  ssl_certificate /apps/nginx/certs/www.hechunping.tech.crt;
  ssl_certificate_key /apps/nginx/certs/www.hechunping.tech.key;
}
[root@CentOS7-01 ~]#systemctl reload nginx
[root@CentOS7-01 ~]#curl --head http://www.hechunping.tech
HTTP/1.1 301 Moved Permanently
Server: HCPWS/1.1
Date: Mon, 06 Jan 2020 12:27:18 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Keep-Alive: timeout=65
Location: https://www.hechunping.tech/

#注:如果return后面還有指令,將不被執(zhí)行

1.5 rewrite_log指令

設(shè)置是否開啟記錄ngx_http_rewrite_module模塊?志記錄到error_log?志?件當(dāng)中,可以配置在http、server、location或if當(dāng)中,需要?志級(jí)別為notice 。
# 示例
error_log  logs/error.log  notice; # 主配置文件中修改即可
[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  server_name www.hechunping.tech;
  location /pc {
    root html;
    index index.html;
    rewrite_log on;
    rewrite ^(.*)$ https://www.baidu.com$1;
  }
}
[root@CentOS7-01 ~]#systemctl reload nginx
[root@CentOS7-01 ~]#curl http://www.hechunping.tech/pc
<html>
<head><title>302 Found</title></head>
<body>
<center><h2>302 Found</h2></center>
<hr><center>nginx</center>
</body>
</html>
[root@CentOS7-01 ~]#tail -n1 -f /apps/nginx/logs/error.log
2020/01/06 20:53:01 [notice] 3274#0: *5 rewritten redirect: "https://www.baidu.com/pc", client: 127.0.0.1, server: www.hechunping.tech, request: "GET /pc HTTP/1.1", host: "www.hechunping.tech"

2 rewrite指令

通過正則表達(dá)式的匹配來改變URI,可以同時(shí)存在?個(gè)或多個(gè)指令,按照順序依次對(duì)URI進(jìn)?匹配,rewrite主要是針對(duì)??請(qǐng)求的URL或者是URI做具體處理,以下是URL和URI的具體介紹:
URI(universal resource identifier):通?資源標(biāo)識(shí)符,標(biāo)識(shí)?個(gè)資源的路徑,可以不帶協(xié)議。
URL(uniform resource location):統(tǒng)?資源定位符,是?于在Internet中描述資源的字符串,是URI的?集,主要包括傳輸協(xié)議(scheme)、主機(jī)(IP、端?號(hào)或者域名)和資源具體地址(?錄和?件名)等三部分,?般格式為scheme://主機(jī)名[:端?號(hào)][/資源路徑],如:http://www.a.com:8080/path/file/index.html就是?個(gè)URL路徑,URL必須帶訪問協(xié)議。
每個(gè)URL都是?個(gè)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的官?介紹地址:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite, rewrite可以配置在server、location、if,其具體使??式為:
rewrite regex replacement [flag];

rewrite將??請(qǐng)求的URI基于regex所描述的模式進(jìn)?檢查,匹配到時(shí)將其替換為表達(dá)式指定的新的URI。 注意:如果在同?級(jí)配置塊中存在多個(gè)rewrite規(guī)則,那么會(huì)?下?下逐個(gè)檢查;被某條件規(guī)則替換完成后,會(huì)重新?輪的替換檢查,隱含有循環(huán)機(jī)制,但不超過10次;如果超過,提?500響應(yīng)碼,[flag]所表?的標(biāo)志位?于控制此循環(huán)機(jī)制,如果替換后的URL是以http://或https://開頭,則替換結(jié)果會(huì)直接以重向返回給客?端, 即永久重定向301

2.1 rewrite flag使?介紹

利?nginx的rewrite的指令,可以實(shí)現(xiàn)url的重新跳轉(zhuǎn),rewrtie有四種不同的flag,分別是redirect(臨時(shí)重定向)、permanent(永久重定向)、break和last。其中前兩種是跳轉(zhuǎn)型的flag,后兩種是代理型,跳轉(zhuǎn)型是指有客?端瀏覽器重新對(duì)新地址進(jìn)?請(qǐng)求,代理型是在WEB服務(wù)器內(nèi)部實(shí)現(xiàn)跳轉(zhuǎn)的。

Syntax: rewrite regex replacement [flag]; #通過正則表達(dá)式處理??請(qǐng)求并返回替換后的數(shù)據(jù)包。 
Default: —
Context: server, location, if

redirect;
#臨時(shí)重定向,重寫完成后以臨時(shí)重定向?式直接返回重寫后?成的新URL給客?端,由客?端重新發(fā)起請(qǐng)求;使?相對(duì)路徑,或者h(yuǎn)ttp://或https://開頭,狀態(tài)碼:302
permanent;
#重寫完成后以永久重定向?式直接返回重寫后?成的新URL給客?端,由客?端重新發(fā)起請(qǐng)求,狀態(tài)碼:301
last;
#重寫完成后停?對(duì)當(dāng)前URI在當(dāng)前l(fā)ocation中后續(xù)的其它重寫操作,?后對(duì)新的URL啟動(dòng)新?輪重寫檢查,不建議在location中使?
break;
#重寫完成后停?對(duì)當(dāng)前URL在當(dāng)前l(fā)ocation中后續(xù)的其它重寫操作,?后直接將匹配結(jié)果返還給客?端即結(jié)束循環(huán)并返回?cái)?shù)據(jù)給客?端,建議在location中使?

2.2 rewrite之永久與臨時(shí)重定向

需求:將訪問源域名www.hechunping.tech的請(qǐng)求永久重定向到www.hechunping.com

2.2.1 永久重定向

[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  server_name www.hechunping.tech;
  location / {
    root html;
    index index.html;
    rewrite ^(.*)$ http://www.hechunping.com$1 permanent;
  }
}
[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/www.hechunping.com.conf 
server {
  listen 80;
  server_name www.hechunping.com;
  location / {
    root html/hechunping;
    index index.html;
  }
}
[root@CentOS7-01 ~]#cat /apps/nginx/html/hechunping/index.html 
hechunping web
[root@CentOS7-01 ~]#systemctl reload nginx

# 訪問測試

Nginx Rewrite相關(guān)功能

目前創(chuàng)新互聯(lián)已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、瓊山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

2.2.2 臨時(shí)重定向

將www.hechunping.tech域名配置文件rewrite指令后面的permanent去掉,或者指定為redirect即可

# 訪問測試

Nginx Rewrite相關(guān)功能

2.2.3 兩者區(qū)別

永久重定向:返回的狀態(tài)碼是301,告訴瀏覽器域名是固定重定向到當(dāng)前?標(biāo)域名,后期不會(huì)更改了。拿上面的例子來說就是將www.hechunping.tech永久重定向到www.hechunping.com。
臨時(shí)重定向:返回的狀態(tài)碼是302,告訴瀏覽器域名不是固定重定向到當(dāng)前?標(biāo)域名,后期可能隨時(shí)會(huì)更改,因此瀏覽器不會(huì)緩存當(dāng)前域名的解析記錄,?瀏覽器會(huì)緩存永久重定向的DNS解析記錄,這也是臨時(shí)重定向與永久重定向最?的本質(zhì)區(qū)別。

2.3 rewrite之break與last

需求:訪問break的請(qǐng)求被轉(zhuǎn)發(fā)?test1,?訪問test1傳遞請(qǐng)求再次被轉(zhuǎn)發(fā)?test2,以此測試last和break分別有什么區(qū)別:

2.3.1 break

[root@CentOS7-01 ~]#mkdir /apps/nginx/html/{break,test{1,2}}
[root@CentOS7-01 ~]#echo "break" > /apps/nginx/html/break/index.html
[root@CentOS7-01 ~]#echo "test1" > /apps/nginx/html/test1/index.html
[root@CentOS7-01 ~]#echo "test2" > /apps/nginx/html/test2/index.html
[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf
server {
  listen 80;
  server_name www.hechunping.tech;
  location /break {
    root html;
    index index.html;
    rewrite ^/break/(.*)$ /test1/$1 break; 
    rewrite ^/test1/(.*)$ /test2/$1 break;
  }
}
[root@CentOS7-01 ~]#systemctl reload nginx
[root@CentOS7-01 ~]#curl http://www.hechunping.tech/break/index.html
test1  

2.3.2 last

[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  server_name www.hechunping.tech;
  location /break {
    root html;
    index index.html;
    rewrite ^/break/(.*)$ /test1/$1 last;
  }
  location /test1 {
    root html;
    index index.html;
    rewrite ^/test1/(.*)$ /test2/$1 break;
  }
}
[root@CentOS7-01 ~]#systemctl reload nginx
[root@CentOS7-01 ~]#curl  http://www.hechunping.tech/break/index.html
test2   

2.3.3 兩者區(qū)別

break:匹配成功后不再向下匹配,也不會(huì)跳轉(zhuǎn)到其他的location,即直接結(jié)束匹配并給客?端返回結(jié)果數(shù)據(jù)。break適?于不改變客?端訪問?式,但是要將訪問的?的URL做單次重寫的場景,?如有V1/V2兩個(gè)版本的?站前端??并存,舊版本的?站數(shù)據(jù)已經(jīng)保存到了static不能丟失,但是要將訪問新版本的資源重寫到新的靜態(tài)資源路徑到新的?錄newstatic:
location /static {
root /data/nginx;
index index.html;
rewrite ^/static/(.*) /newstatic/$1 break;
}

last:對(duì)某個(gè)location的URL匹配成功后會(huì)停?當(dāng)前l(fā)ocation的后續(xù)rewrite規(guī)則,并結(jié)束當(dāng)前l(fā)ocation,然后將匹配?成的新URL跳轉(zhuǎn)?其他location繼續(xù)匹配,直到?jīng)]有l(wèi)ocation可匹配后將最后?次location的數(shù)據(jù)返回給客?端。last適?于要不改變客?端訪問?式但是需做多次?的URL重寫的場景,場景不是很多。

2.4 rewrite之?動(dòng)跳轉(zhuǎn)https

[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  server_name www.hechunping.tech;
  if ($scheme = http ){
    rewrite ^(.*)$ https://$server_name$request_uri permanent;
  }
}
server {
  listen 443 ssl;
  server_name www.hechunping.tech;
  ssl_certificate /apps/nginx/certs/www.hechunping.tech.crt;
  ssl_certificate_key /apps/nginx/certs/www.hechunping.tech.key;
  ssl_session_cache shared:sslcache:20m;
  ssl_session_timeout 10m;
  location / {
    root /apps/nginx/html/pc;
    index index.html;
  }
}
[root@CentOS7-01 ~]#systemctl reload nginx

訪問測試

Nginx Rewrite相關(guān)功能

2.5 rewrite之判斷?件是否存在

[root@CentOS7-01 ~]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  server_name www.hechunping.tech;
  location / {
    root /apps/nginx/html/pc;
    index index.html;
    if ( !-f $request_filename ) {
      rewrite ^(.*)$ http://www.hechunping.tech/index.html;
    }
  }
}
[root@CentOS7-01 ~]#systemctl reload nginx
[root@CentOS7-01 ~]#ls /apps/nginx/html/pc/
index.html
[root@CentOS7-01 ~]#cat /apps/nginx/html/pc/index.html 
pc web

訪問測試

Nginx Rewrite相關(guān)功能

從上圖結(jié)果中可以發(fā)現(xiàn),請(qǐng)求的uri sdafda不存在,而后做了302臨時(shí)重定向到http://www.hechunping.tech/index.html

3 Nginx防盜鏈

防盜鏈基于客?端攜帶的referer實(shí)現(xiàn),referer是記錄打開?個(gè)??之前記錄是從哪個(gè)??跳轉(zhuǎn)過來的標(biāo)記信息,如果別?只鏈接了???站圖?或某個(gè)單獨(dú)的資源,?不是打開了?站的整個(gè)??,這就是盜鏈,referer就是之前的那個(gè)?站域名,正常的referer信息有以下?種:

none:請(qǐng)求報(bào)?沒有referer?部,?如??直接在瀏覽器輸?域名訪問web?站,就沒有referer信息。
blocked:請(qǐng)求報(bào)文有referer?部,但?有效值,?如為空。
server_names:referer?部中包含本主機(jī)名及即nginx 監(jiān)聽的server_name。
arbitrary_string:?定義指定字符串,但可使?*作通配符。
regular expression:被指定的正則表達(dá)式模式匹配到的字符串,要使?~開頭,例如: ~.*\.hechunping\.com。

正常通過搜索引擎搜索web網(wǎng)站并訪問該網(wǎng)站的referer信息如下:
[root@CentOS7-01 ~]#tail -f /data/nginx/logs/www.hechunping.tech/access.log 
{"@timestamp":"2020-01-07T15:20:43+08:00","host":"192.168.7.71","clientip":"192.168.7.1","size":138,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.hechunping.tech","uri":"/","domain":"www.hechunping.tech","xff":"-","referer":"https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=www.hechunping.tech&rsv_pq=e28a73dc000305f6&rsv_t=56aau6YPtkwVFUsn6WxxW25Eujtv3MwkIOSZI1txkxGY5RgRI3GdmfDHzEM&rqlang=cn&rsv_enter=1&rsv_dl=ib&rsv_sug3=20&rsv_sug1=8&rsv_sug7=101","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0","status":"302"}

3.1 實(shí)現(xiàn)web盜鏈

在www.hechunping.tech這個(gè)網(wǎng)站盜鏈www.hechunpig.com網(wǎng)站的a1.jpg圖片

# www.hechunping.tech網(wǎng)站的nginx配置
[root@CentOS7-01 image]#cat /apps/nginx/conf/vhosts/pc.conf 
server {
  listen 80;
  server_name www.hechunping.tech;
  location / {
    root /apps/nginx/html/pc;
    access_log /data/nginx/logs/www.hechunping.tech/access.log access_json;
    index index.html;
  }
}
[root@CentOS7-01 image]#cat /apps/nginx/html/pc/index.html 
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>盜鏈頁面</title>
</head>
<body>
<a >測試盜鏈</a>
<img src="http://www.hechunping.com/aa.jpg">
</body>
</html>

# www.hechunping.com網(wǎng)站的nginx配置
[root@CentOS7-01 image]#cat /apps/nginx/conf/vhosts/www.hechunping.com.conf 
server {
  listen 80;
  server_name www.hechunping.com;
  location ~* \.(png|jpg)$ {
    root html/image;
    access_log /data/nginx/logs/www.hechunping.com/access.log access_json;
  }
}
[root@CentOS7-01 image]#ls /apps/nginx/html/image/
aa.jpg
[root@CentOS7-01 image]#cat /apps/nginx/html/hechunping/index.html 
hechunping web
[root@CentOS7-01 image]#systemctl reload nginx
#訪問測試
在www.hechunping.com站點(diǎn)的訪問日志中可以看到訪問/aa.jpg是從"referer":"http://www.hechunping.tech這個(gè)地址過來的
[root@CentOS7-01 image]#tail -n1 /data/nginx/logs/www.hechunping.com/access.log 
{"@timestamp":"2020-01-07T17:15:52+08:00","host":"192.168.7.71","clientip":"192.168.7.1","size":425253,"responsetime":0.048,"upstreamtime":"-","upstreamhost":"-","http_host":"www.hechunping.com","uri":"/aa.jpg","domain":"www.hechunping.com","xff":"-","referer":"http://www.hechunping.tech/","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36","status":"200"}

Nginx Rewrite相關(guān)功能
Nginx Rewrite相關(guān)功能

3.2 實(shí)現(xiàn)防盜鏈

基于訪問安全考慮,nginx?持通過ngx_http_referer_module模塊檢查訪問請(qǐng)求的referer信息是否有效,官方文檔:https://nginx.org/en/docs/http/ngx_http_referer_module.html#valid_referers
實(shí)現(xiàn)防盜鏈功能,定義?式如下:

[root@CentOS7-01 image]#cat /apps/nginx/conf/vhosts/www.hechunping.com.conf 
server {
  listen 80;
  server_name www.hechunping.com;
  location ~* \.(png|jpg)$ {
    root html/image;
    access_log /data/nginx/logs/www.hechunping.com/access.log access_json;
    valid_referers none blocked server_names *.hechunping.com api.online.test/v1/hostlist ~\.google\. ~\.baidu\.;
    if ($invalid_referer) {
      return 403;
    }
  }
}
[root@CentOS7-01 image]#ls /apps/nginx/html/image/
aa.jpg
[root@CentOS7-01 image]#systemctl reload nginx

# 訪問測試
在www.hechunping.com站點(diǎn)的訪問日志中可以看到"referer":"http://www.hechunping.tech 狀態(tài)碼403
[root@CentOS7-01 image]#tail -n1 /data/nginx/logs/www.hechunping.com/access.log 
{"@timestamp":"2020-01-07T17:49:43+08:00","host":"192.168.7.71","clientip":"192.168.7.1","size":548,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.hechunping.com","uri":"/aa.jpg","domain":"www.hechunping.com","xff":"-","referer":"http://www.hechunping.tech/","tcp_xff":"","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36","status":"403"}

Nginx Rewrite相關(guān)功能

網(wǎng)頁標(biāo)題:NginxRewrite相關(guān)功能
標(biāo)題URL:http://bm7419.com/article32/iipjsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司用戶體驗(yàn)、移動(dòng)網(wǎng)站建設(shè)App開發(fā)、定制網(wǎng)站、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作