1.16 nginx全局變量
nginx 常用全局變量
變量 | 說明 |
---|---|
$args | 請求中的參數,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2 |
$content_length | HTTP請求信息裏的"Content-Length" |
$conten_type | HTTP請求信息裏的"Content-Type" |
查看content-length
$document_root | nginx虛擬主機配置文件中的root參數對應的值 |
$document_uri | 當前請求中不包含指令的URI,如www.123.com/1.php?a=1&b=2的$document_uri就是1.php,不包含後面的參數 |
$host | 主機頭,也就是域名 |
$http_user_agent | 客戶端的詳細信息,也就是瀏覽器的標識,用curl -A可以指定 |
$http_cookie |
客戶端的cookie信息 |
$limit_rate | 如果nginx服務器使用limit_rate配置了顯示網絡速率,則會顯示,如果沒有設置, 則顯示0 |
$remote_addr | 客戶端的公網ip |
$remote_port | 客戶端的port |
$remote_user | 如果nginx有配置認證,該變量代表客戶端認證的用戶名 |
$request_body_file | 做反向代理時發給後端服務器的本地資源的名稱 |
$request_method | 請求資源的方式,GET/PUT/DELETE等 |
$request_filename | 當前請求的資源文件的路徑名稱,相當於是$document_root/$document_uri的組合 |
$request_uri | 請求的鏈接,包括$document_uri和$args |
$scheme | 請求的協議,如ftp,http,https |
$server_protocol | 客戶端請求資源使用的協議的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等 |
$server_addr | 服務器IP地址 |
$server_name | 服務器的主機名 |
$server_port | 服務器的端口號 |
$uri | 和$document_uri相同 |
$http_referer | 客戶端請求時的referer,通俗講就是該請求是通過哪個鏈接跳過來的,用curl -e可以指定 |
rewrite實戰1
rewrite/example.md - master - 代碼瀏覽 - nginx - aminglinux https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/example.md?public=true
舉例:
結果
例2:先判斷是否不是就執行跳轉
結果
例3
結果:
在白名單下不在白名單
nginx的location配置
環境準備
重新編譯nginx 添加入第三方模塊
重啓nginx
echo模塊用法
-t 檢測錯誤時報錯有可能echo 未安裝成功
location/ruler.md - master - 代碼瀏覽 - nginx - aminglinux https://coding.net/u/aminglinux/p/nginx/git/blob/master/location/ruler.md?public=true
測試 “/” 和 “~*” 優先級
location / 從域名後開始匹配,匹配到 /abc eho ‘/’
location ~ 不從域名後開始匹配,先匹配目錄/abc/ 目錄
location ~ * 不區分大小寫 域名後包含關鍵字就匹配
測試 “~” 和 “~*” 優先級等同但是誰在上優先匹配
“~*” 和 “^~” 優先級
“=” 和 “^~” 優先級