這是我需要匹配的 URL。
https://example.com/wp-login.php
https://example.com/wp-login.php?action=lostpassword
https://example.com/?s=
https://example.com/?s=xxxxxxx
這是我使用的位置匹配規則,它不起作用,請問有什么問題嗎?
location ~* ^/(?:wp-login\.php|\?s\=) {
deny all;
}
uj5u.com熱心網友回復:
這是 nginx 新手常見的錯誤。location指令和指令都不rewrite適用于整個請求 URI。它們都適用于完全不包含查詢字串部分的規范化請求 URI(有關 URI 規范化的描述可以在location指令檔案中找到)。這意味著您根本無法使用location指令檢查查詢字串。
通常你所要求的可以通過檢查$arg_<name>nginx 變數值來實作,比如
if ($arg_s) {
return 403;
}
不幸的是,沒有辦法將空查詢引數值與缺少這樣的引數區分開來(好吧,至少在不使用像 lua-nginx-module 這樣的第三方模塊的情況下,可以根據空字串或nil值實際檢查差異)。幸運的是,您可以通過$request_uri內部變數獲得原始請求 URI,因此您可以執行類似的操作
if ($request_uri ~ \?(.*&)?s=) {
return 403;
}
location = /wp-login.php {
deny all;
}
甚至使用$request_uri與正則運算式模式匹配的單個變數,例如:
if ($request_uri ~ /wp-login\.php|\?(.*&)?s=) {
return 403;
}
(這應該放在server配置級別)
uj5u.com熱心網友回復:
使用您顯示的示例和嘗試,請嘗試以下正則運算式。
(?:wp-login\.php(?:\?action=\S )?$|\?s=(?:\S )?)$
這是regex 的在線演示。
說明:為上述添加詳細說明。
(?: ##Starting 1st non-capturing group from here.
wp-login\.php ##matching wp-login.php here.
(?:\?action=\S )?$ ##In a non-capturing group matching ?action= followed by non-spaces and keeping this as an optional match.
| ##Putting OR condition either any of above OR following should match.
\?s= ##Matching literal ? followed by s=
(?:\S )? ##In a non-capturing group matching 1 or more non-spaces and keeping this match as an optional one.
)$ ##closing very first non-capturing group here.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490071.html
