10.12-10.15 rewrite配置if,break和last的用法,rewrite規則,Nginx全局變量php
Nginx的Rwrite配置html
加粗 域名跳轉(重定向)、URL重寫(僞靜態)、動靜分離(跳轉域名,並接入CDN實現加速)linux
依賴PCRE庫nginx
模塊:ngx_http_rewrite_modulegit
加粗 Rwrite相關指令正則表達式
if (條件) { command } https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md vim
break和last https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md 後端
開啓rewrite日誌記錄:瀏覽器
1.在server加入 rewrite_log on;bash
2.在nginx.conf 配置error_log logs/nginx_error.log notice; notice會記錄rewrite錯誤信息
配置:
vim nginx/conf/nginx.conf error_log logs/nginx_error.log notice; vim nginx/conf/vhost/1.com_default.conf server{ listen 80; server_name *.1.com 1.com; index index.html 80.html; root /data/t-nginx/1.com; rewrite_log on; rewrite /1.html /2.html ; rewrite /2.html /3.html ; } # curl -x127.0.0.1:80 1.com/1.html 3333
3.當咱們請求1.html時,最終訪問到的是3.html,兩條rewrite規則前後執行。
tail nginx/logs/nginx_error.log 2018/10/22 11:21:00 [notice] 20967#0: *1906 "/1.html" matches "/1.html", client: 127.0.0.1, server: *.1.com, request: "GET HTTP://1.com/1.html HTTP/1.1", host: "1.com" 2018/10/22 11:21:00 [notice] 20967#0: *1906 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: *.1.com, request: "GET HTTP://1.com/1.html HTTP/1.1", host: "1.com" 2018/10/22 11:21:00 [notice] 20967#0: *1906 "/2.html" matches "/2.html", client: 127.0.0.1, server: *.1.com, request: "GET HTTP://1.com/1.html HTTP/1.1", host: "1.com" 2018/10/22 11:21:00 [notice] 20967#0: *1906 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: *.1.com, request: "GET HTTP://1.com/1.html HTTP/1.1", host: "1.com"
日誌解釋:
以""爲分界,左邊規則,右邊匹配
return 後面跟狀態碼、URL、text(支持變量)https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/return.md
反饋字符串能夠這樣寫
return 200 "it's 200";
格式:return 狀態碼 "字符串";
rewrite規則 https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/rewrite_ruler.md
* regex是用於匹配URI的正則表達式(/(.*)這種),其不會匹配到$host(域名)
rewrite_log定義rewrite日誌 rewrite_log on; 寫到error_log notice級別
Rwrite相關全局變量
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/variable.md
變量 | 說明 |
---|---|
$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」 |
$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能夠指定 |
return 200 能夠搭配全局變量
格式:
return 200 "全局變量";
例如:
return 200 "$args";
配置參考以下:
server { listen 80; server_name 2.com; root /data/t-nginx/2.com; return 200 "$args"; }
[root@AliKvn vhost]# curl -x127.0.0.1:80 '2.com/2.php?1=a&2=b' 1=a&2=b[root@AliKvn vhost]#
其中1=a&2=b,就是"$args"的變量值了, 其餘變量也能夠用這鐘方法取值
例如:
return 200 "$document_uri";
server { listen 80; server_name 2.com; root /data/t-nginx/2.com; return 200 "$document_uri"; } nginx -s reload [root@AliKvn vhost]# !curl curl -x127.0.0.1:80 '2.com/2.php?1=a&2=b' /2.php[root@AliKvn vhost]#
那麼2.php就是$document_uri了
以上變量,能夠組合在一塊兒。