歡迎您光臨本站 註冊首頁

如何利用nginx通過正則攔截指定url請求詳解

←手機掃碼閱讀     madbeef @ 2020-05-18 , reply:0

nginx服務器

nginx是非常出色web服務器,對於靜態文件的處理非常高效,同時它的代理轉發功能和其它後臺服務器搭配起來也非常的簡單高效。

location

我們知道nginx會對請求進行解析,然後回得到關於請求的url等信息,我們只需要對url進行匹配,然後攔截即可。

匹配規則

location / { if ($request_uri ~* ^/?http(.*)$) { return 404; } }


經過這樣的匹配,我們就可以攔截所有請求根目錄的網址並且參數為?httpxxx類似的請求都會顯示404.

防盜鏈

返回http代碼,例如設置nginx防盜鏈:

location ~* .(gif|jpg|png|swf|flv)$ { valid_referers none blocked www.80shihua.com www.menghuiguli.com; if ($invalid_referer) { return 404; } }


nginx常用變量

nginx解析出很多我們常用的變量,我們只需要拿過來使用即可,下面就是nginx常用的變量。具體使用方法,可以參考官方文檔。

$content_length

$content_type

$COOKIE_

$date_gmt

$date_local

$document_root

$document_uri

$fastcgi_path_info

$fastcgi_script_name

$gzip_ratio

$host

$hostname (ngx_http_core_module)

$hostname (ngx_stream_core_module)

$http2

$http_

$protocol

$proxy_host

$proxy_port

$query_string

$realpath_root

$request

$request_body

$request_uri

$scheme

$server_name

$uri


[madbeef ] 如何利用nginx通過正則攔截指定url請求詳解已經有502次圍觀

http://coctec.com/docs/nginx/show-post-234927.html