Linux基礎命令之Nginx中的rewrite功能(重新)

一、什么是Rewrite

????????Rewrite也稱URL Rewrite,即URL重寫,就是把傳入Web的請求重定向到其他URL的過程。

????????1. URL Rewrite最常見的應用是URL偽靜態化,是將動態頁面顯示為靜態頁面方式的一種技術。比如http://www.123.com/news/index.php?id=123 使用URLRewrite 轉換后可以顯示為 http://www.123.com/news/123.html

????????2. 從安全角度上講,如果在url中暴露太多的參數,無疑會造成一定量的信息泄漏,可能會被一些黑客利用,對你的系統造成一定的破壞,所以靜態化的url地址可以給我們帶來更高的安全性。

????????3. 實現網站地址跳轉、盜鏈。

二、Rewrite相關指令

????????Nginx Rewrite相關指令有if、rewrite、set、return等。

????????1、if指令

????????if 的語法 if (condition) { … }

????????應用于server和location環境內(if 與條件之間必須有空格)

????????if 可以支持如下條件判斷匹配符號

????????~ 為區分大小寫匹配

????????~* 為不區分大小寫匹配

????????!~和!~* 分別為區分大小寫不匹配及不區分大小寫不匹配

????????-f 和!-f 用來判斷是否存在文件

????????-d 和!-d 用來判斷是否存在目錄

????????-e 和!-e 用來判斷是否存在文件或目錄

????????-x 和!-x 用來判斷文件是否可執行

????????在匹配過程中可以引用一些Nginx的全局變量,更多的變量請參考 ????????http://wiki.nginx.org/NginxHttpCoreModule 的 Variables 部分

????????$args 請求中的參數;

????????$document_root 針對當前請求的根路徑設置值;

????????$host 請求信息中的"Host",如果請求中沒有Host行,則等于設置的服務器名;

????????$limit_rate 對連接速率的限制;

????????$request_method 請求的方法,比如"GET"、"POST"等;

????????$remote_addr 客戶端地址;

????????$remote_port 客戶端端口號;

????????$remote_user 客戶端用戶名,認證用;

????????$request_filename 當前請求的文件路徑名(帶root指定的路徑,即網站的主目錄)

????????$request_uri 當前請求的文件路徑名(不帶root指定的路徑)

????????$query_string 與$args相同;

????????$scheme 所用的協議,比如http或者是https

????????$server_protocol 請求的協議版本,"HTTP/1.0"或"HTTP/1.1";

????????$server_addr 服務器地址,如果沒有用listen指明服務器地址,使用這個變量將發起一次系統調用以取得地址(造成資源浪費);

????????$server_name 請求到達的服務器名;

????????$document_uri 與$uri一樣,URI地址;

????????$server_port 請求到達的服務器端口號;

????????last stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI; 相當于Apache里的[L]標記,表示完成rewrite

????????break stops processing the current set of ngx_http_rewrite_module directives as with the break directive; 本條規則匹配完成后,終止匹配,不再匹配后面的規則

????????redirect returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://” or “https://”; 返回302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址

????????permanent returns a permanent redirect with the 301 code.返回301永久重定向,瀏覽器地址會顯示跳轉后URL地址

rewrite /(.*)\.php$ /$1.html break;

(.*)任意字符長度

.php$ 以php結尾

$1引用前邊的(.*)

任意請求php結尾的網頁轉到html網頁 然后跳出

if ( !-f $request_filename) {? 如果請求的文件不存在則執行以下操作

rewrite ^/(.*)$ /test.html last; 任意不存在的網頁請求裝給test.html網頁 last 繼續替換

}

2、使用rewrite命令實現防盜鏈

1.盜鏈(html)

[root@server3 ~]# vim /var/www/html/index.html
<a href="http://192.168.50.103/aa.txt">nginx</a>

2.防盜鏈nginx

[root@server3 ~]# ?vim /usr/local/nginx/conf/nginx.conf
#添加如下此 Nginx 配置片段用于防止未經授權的站點直接鏈接到 .txt 文件。只有當請求的 referer 來自 www.robin.com、沒有 referer 頭、或 referer 頭被阻塞時,請求才會被認為是有效的。否則,請求將被重定向到一個指定的錯誤頁面(http://192.168.10.100/gun.html),或者如果取消注釋 return 404; 行,則返回 404 錯誤
location ~* \.(txt)$ {                                #使用正則表達式匹配所有以 .txt 結尾的請求。~* 表示不區分大小寫的匹配valid_referers none blocked www.robin.com;    #定義哪些 referer 是有效的,沒有referer valid_referer=1if ($invalid_referer) {rewrite ^/ http://192.168.50.103/gun.html;#return 404;}}

none 代表沒有referer

blocked 代表有referer但是被防火墻或者是代理給去除了

none the“Referer” field is missing in the request header; (即直接在客戶端瀏覽器地址欄中輸入的)

blocked the“Referer” field is present in the request header, but its value has been deleted by a firewall or proxy server; such values are strings that do not start with “http://” or “https://”;

server_names the “Referer” request header field contains one of the server names;

arbitrary string defines a server name and an optional URI prefix. A server name can have an “*” at the beginning or end. During the checking, the server’s port in the “Referer” field is ignored;

regular expression the first symbol should be a “~”. It should be noted that an expression will be matched against the text starting after the “http://” or “https://”

rewrite 指令根據表達式來重定向URI,或者修改字符串。可以應用于server,location, if環境下

每行rewrite指令最后跟一個flag標記,支持的flag標記有:

last 相當于Apache里的[L]標記,表示完成rewrite,然后繼續匹配后續規則

break 本條規則匹配完成后,終止匹配,不再匹配后面的規則

redirect 返回302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址

permanent 返回301永久重定向,瀏覽器地址會顯示跳轉后URL地址

redirect 和 permanent區別則是返回的不同方式的重定向,對于客戶端來說一般狀態下是沒有區別的。而對于搜索引擎,相對來說301的重定向更加友好,

如果我們把一個地址采用301跳轉方式跳轉的話,搜索引擎會把老地址的相關信息帶到新地址,同時在搜索引擎索引庫中徹底廢棄掉原先的老地址。使用302重定向時,搜索引擎(特別是google)有時會查看跳轉前后哪個網址更直觀,然后決定顯示哪個,如果它覺的跳轉前的URL更好的話,也許地址欄不會更改,那么很有可能出現URL劫持的現像。

參考示例:
例1:
http://www.test.com/test/abc/1.html ? http://www.test.com/ccc/bbb/2.html
location /test{rewrite .* /ccc/bbb/2.html permanent;
}
例2:
http://www.test.com/2015/12/10/2.html ==> http://www.test.com/2014/12/10/2.html
location /2015 {rewrite ^/2015/(.*)$ /2014/$1 permanent; #引用
}
例3:
http://www.test.com/2015/ccc/bbb/2.html ==> http://jd.com/index.php
location /2015{if ($host ~* www.test.com) {rewrite .* http://www.jd.com/index.php permanent;}
}
例4:
#http://www.test.com/kkk/1.html ==> http://jd.com/kkk/1.html
location / {
root html;
index index.html index.htm;
if ($host ~* www.test.com) {rewrite .* http://www.jd.com/$request_uri permanent;}
}
例5:
有時候在訪問一個二級子目錄時會出現目錄后的/無法補齊,如:
[root@localhost html]# elinks --dump www.test.com/www/xxx 無法訪問
[root@localhost html]# elinks --dump www.test.com/www/可以訪問
if ( -d $request_filename) {rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
}
^/(.*)([^/])$表示以/符號開始并緊跟著任何字符,同時不是以/為結束的字符串,在我的url中,(.*)表示的www,([^/])表示的不是以/結束的字符串
例6:
#http://www.test.com/login/robin.html ==> http://www.test.com/reg/login.php?user=robin
location /login {rewrite ^/login/(.*)\.html$ /reg/login.php?user=$1 permanent;
}
例7:
http://www.test.com/uplook/2000-12-20.html ==> http://www.test.com/uplook/2000/11/20.html
location /uplook {rewrite ^/uplook/([0-9]+)-([0-9]+)-([0-9]+)\.html$ /uplook/$1/$2/$3.html permanent
}
set 指令是用于定義一個變量,并且賦值。應用于server,location,if環境。
例8:
#http://robin.test.com ==> http://www.test.com/robin
#http://zorro.test.com ==> http://www.test.com/zorro
[root@root html]# mkdir robin zorro
[root@root html]# echo robin.... > robin/index.html
[root@root html]# echo zorro... > zorro/index.html
a. DNS實現泛解析
*.test.com IN A 網站IP
b. nginx Rewrite
if ($host ~* "^www.test.com$" ) {break;
}
if ($host ~* "^(.*)\.test\.com$" ) {set $user $1;rewrite .* http://www.test.com/$user permanent;
}
return 指令用于返回狀態碼給客戶端,應用于server,location,if環境。
例9:如果訪問的.sh結尾的文件則返回403操作拒絕錯誤
location ~* \.sh$ {return 403;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/68192.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/68192.shtml
英文地址,請注明出處:http://en.pswp.cn/web/68192.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

anaconda使用

anaconda配置鏡像源&#xff1a; 引用&#xff1a;https://zhuanlan.zhihu.com/p/17776864328 # 顯示所有的鏡像源 conda config --show channels # 設置鏡像源 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add c…

DeepSeek 闡述 2025年前端發展趨勢

預測2025年前端的發展趨勢。首先&#xff0c;我需要考慮當前的前端 技術發展情況&#xff0c;以及近幾年的變化趨勢。比如&#xff0c;框架方面&#xff0c;React、Vue、Angular這些主流框架的更新方向和社區活躍度。可能用戶想知道未來哪些技術會更流行&#xff0c;或者需要學…

RK3568平臺開發系列講解(ConfigFS篇)ConfigFS核心數據結構

??返回專欄總目錄 文章目錄 一、數據結構二、結構體關系三、案例3.1、configfs_subsystem 實例3.2、config_group 實例化四、屬性和方法五、config_item實例化沉淀、分享、成長,讓自己和他人都能有所收獲!?? 理解 ConfigFS 的核心數據結構對于深入使用和定制 ConfigFS 非…

【實戰篇】巧用 DeepSeek,讓 Excel 數據處理更高效

一、為何選擇用 DeepSeek 處理 Excel 在日常工作與生活里,Excel 是我們頻繁使用的工具。不管是統計公司銷售數據、分析學生成績,還是梳理個人財務狀況,Excel 憑借其強大的功能,如數據排序、篩選和簡單公式計算,為我們提供了諸多便利。但當面對復雜的數據處理任務,比如從…

微信小程序案例1——制作貓眼電影底部標簽導航欄

文章目錄 一、項目步驟1 新建一個無AppID的movie項目2將準備好的底部標簽導航圖標拷貝到movie項目下面(將圖標文件夾image放到項目文件夾里&#xff09;3 打開App.json配置文件&#xff0c;在pages數組里添加4個頁面路徑:電影“pages/movie/movie”、影院“pages/cinema/cinema…

CSS 偽類(Pseudo-classes)的詳細介紹

CSS 偽類詳解與示例 在日常的前端開發中&#xff0c;CSS 偽類可以幫助我們非常精準地選擇元素或其特定狀態&#xff0c;從而達到豐富頁面表現的目的。本文將詳細介紹以下偽類的使用&#xff1a; 表單相關偽類 :checked、:disabled、:enabled、:in-range、:invalid、:optional、…

docker多個容器的相互通信

在同一臺宿主機上運行多個 Docker 容器時&#xff0c;容器之間可以通過以下幾種方式實現通信&#xff1a; 1. 使用 Docker 默認網絡&#xff08;Bridge 網絡&#xff09; Docker 默認會為每個容器分配一個 bridge 網絡&#xff0c;容器可以通過 IP 地址或容器名稱互相通信。 …

Elasticsearch 開放推理 API 增加了 Azure AI Studio 支持

作者&#xff1a;來自 Elastic Mark Hoy Elasticsearch 開放推理 API 現已支持 Azure AI Studio。在此博客中了解如何將 Azure AI Studio 功能與 Elasticsearch 結合使用。 作為我們持續致力于為 Microsoft Azure 開發人員提供他們選擇的工具的一部分&#xff0c;我們很高興地宣…

基于Bootstrap + Java + Oracle實現的電商平臺

以下是基于Bootstrap Java Oracle實現的電商平臺開發方案&#xff08;簡化版&#xff09;&#xff1a; 一、系統架構設計 前端&#xff1a;Bootstrap 5 jQuery 后端&#xff1a;Java Spring Boot 數據庫&#xff1a;Oracle 19c 自動化&#xff1a;Spring Scheduler Oracle…

JUC學習筆記02

文章目錄 JUC筆記2練習題&#xff1a;手寫線程池代碼解釋&#xff1a;AdvancedThreadPool 類&#xff1a;WorkerThread 內部類&#xff1a;AdvancedThreadPoolExample 類&#xff1a; 線程池的思考CPU密集型IO密集型 練習題&#xff1a;手寫自動重試機練習題&#xff1a;手寫定…

【Unity】從父對象中獲取子對象組件的方式

1.GetComponentInChildren 用于獲取對與指定組件或游戲對象的任何子級相同的游戲對象上的組件類型的引用。 該方法在Unity腳本API的聲明格式為&#xff1a; public T GetComponentInChildren(bool includeInactive false) includeInactive參數&#xff08;可選&#xff09…

Redis性能優化

1.是否使用復雜度過高的命令 首先&#xff0c;第一步&#xff0c;你需要去查看一下 Redis 的慢日志&#xff08;slowlog&#xff09;。 Redis 提供了慢日志命令的統計功能&#xff0c;它記錄了有哪些命令在執行時耗時比較久。 查看 Redis 慢日志之前&#xff0c;你需要設置慢…

baigeiRSA

baigeiRSA 打開附件有兩個&#xff1a; 1.import libnumfrom Crypto.Util import numberfrom secret import flag?size 128e 65537p number.getPrime(size)q number.getPrime(size)n p*q?m libnum.s2n(flag)c pow(m, e, n)?print(n %d % n)print(c %d % c)??2.n…

【csp-j學習完C++語法后,如何進階學習C++算法和數據結構?】

在掌握了 CSP - J 的 C 語法基礎后&#xff0c;接下來的進階學習需要系統地掌握各類算法和數據結構知識&#xff0c;并通過大量練習來鞏固和提高應用能力。以下是一份詳細的進階學習規劃&#xff1a; 第一階段&#xff1a;基礎算法學習&#xff08;1 - 2 個月&#xff09; 排…

QT中解決使用QCustomplot繪制高速大量數據時頻譜圖卡頓問題

[&#xff01;&#xff01;&#xff01;核心方法&#xff01;&#xff01;&#xff01;] 使用帶參數的replot()函數繪制m_pCustomPlot>replot(QCustomPlot::rpQueuedReplot) 1. replot() 方法 void QCustomPlot::replot(QCustomPlot::RefreshPriority refreshPriority rp…

【AI】卷積神經網絡CNN

不定期更新&#xff0c;建議關注收藏點贊。 目錄 零碎小組件經驗總結早期的CNN 零碎小組件 全連接神經網絡 目前已經被替代。 每個神經元都有參與&#xff0c;但由于數據中的特征點變化大&#xff0c;全連接神經網絡把所有數據特征都學習了&#xff0c;故效果不好。感受野&…

YOLOv11-ultralytics-8.3.67部分代碼閱讀筆記-downloads.py

downloads.py ultralytics\utils\downloads.py 目錄 downloads.py 1.所需的庫和模塊 2.def is_url(url, checkFalse): 3.def delete_dsstore(path, files_to_delete(".DS_Store", "__MACOSX")): 4.def zip_directory(directory, compressTrue, ex…

微信小程序~電器維修系統小程序

博主介紹&#xff1a;?程序猿徐師兄、8年大廠程序員經歷。全網粉絲15w、csdn博客專家、掘金/華為云/阿里云/InfoQ等平臺優質作者、專注于Java技術領域和畢業項目實戰? &#x1f345;文末獲取源碼聯系&#x1f345; &#x1f447;&#x1f3fb; 精彩專欄推薦訂閱&#x1f447;…

VDN 微服務架構搭建篇(三)基于 Nacos 的 Spring Cloud Gateway 動態路由管理

VDN 微服務架構搭建篇&#xff08;三&#xff09;&#xff1a;基于 Nacos 的 Spring Cloud Gateway 動態路由管理 在微服務架構中&#xff0c;網關 是整個系統的入口&#xff0c;負責 流量管理、請求路由、安全控制等關鍵功能。 Spring Cloud Gateway 作為 Spring 生態官方推薦…

LLAMA-Factory安裝教程(解決報錯cannot allocate memory in static TLS block的問題)

步驟一&#xff1a; 下載基礎鏡像 # 配置docker DNS vi /etc/docker/daemon.json # daemon.json文件中 { "insecure-registries": ["https://swr.cn-east-317.qdrgznjszx.com"], "registry-mirrors": ["https://docker.mirrors.ustc.edu.c…