內置變量存放在 ngx_http_core_module 模塊中,變量的命名方式和apache 服務器變量是一致的。總而言之,這些變量表明着客戶端請求頭的內容,例如$http_user_agent, $http_cookie, 等等。下面是nginx支持的全部內置變量:php
$arg_name
請求中的的參數名,即「?」後面的arg_name=arg_value形式的arg_namehtml
$args
請求中的參數值linux
$binary_remote_addr
客戶端地址的二進制形式, 固定長度爲4個字節nginx
$body_bytes_sent
傳輸給客戶端的字節數,響應頭不計算在內;這個變量和Apache的mod_log_config模塊中的「%B」參數保持兼容web
$bytes_sent
傳輸給客戶端的字節數 (1.3.8, 1.2.5)正則表達式
$connection
TCP鏈接的序列號 (1.3.8, 1.2.5)apache
$connection_requests
TCP鏈接當前的請求數量 (1.3.8, 1.2.5)後端
$content_length
「Content-Length」 請求頭字段瀏覽器
$content_type
「Content-Type」 請求頭字段安全
$cookie_name
cookie名稱
$document_root
當前請求的文檔根目錄或別名
$document_uri
同 $uri
$host
優先級以下:HTTP請求行的主機名>」HOST」請求頭字段>符合請求的服務器名
$hostname
主機名
$http_name
匹配任意請求頭字段; 變量名中的後半部分「name」能夠替換成任意請求頭字段,如在配置文件中須要獲取http請求頭:「Accept-Language」,那麼將「-」替換爲下劃線,大寫字母替換爲小寫,形如:$http_accept_language便可。
$https
若是開啓了SSL安全模式,值爲「on」,不然爲空字符串。
$is_args
若是請求中有參數,值爲「?」,不然爲空字符串。
$limit_rate
用於設置響應的速度限制,詳見 limit_rate。
$msec
當前的Unix時間戳 (1.3.9, 1.2.6)
$nginx_version
nginx版本
$pid
工做進程的PID
$pipe
若是請求來自管道通訊,值爲「p」,不然爲「.」 (1.3.12, 1.2.7)
$proxy_protocol_addr
獲取代理訪問服務器的客戶端地址,若是是直接訪問,該值爲空字符串。(1.5.12)
$query_string
同 $args
$realpath_root
當前請求的文檔根目錄或別名的真實路徑,會將全部符號鏈接轉換爲真實路徑。
$remote_addr
客戶端地址
$remote_port
客戶端端口
$remote_user
用於HTTP基礎認證服務的用戶名
$request
表明客戶端的請求地址
$request_body
客戶端的請求主體
此變量可在location中使用,將請求主體經過proxy_pass, fastcgi_pass, uwsgi_pass, 和 scgi_pass傳遞給下一級的代理服務器。
$request_body_file
將客戶端請求主體保存在臨時文件中。文件處理結束後,此文件需刪除。若是須要之一開啓此功能,須要設置client_body_in_file_only。若是將次文件傳遞給後端的代理服務器,須要禁用request body,即設置proxy_pass_request_body off,fastcgi_pass_request_body off, uwsgi_pass_request_body off, or scgi_pass_request_body off 。
$request_completion
若是請求成功,值爲」OK」,若是請求未完成或者請求不是一個範圍請求的最後一部分,則爲空。
$request_filename
當前鏈接請求的文件路徑,由root或alias指令與URI請求生成。
$request_length
請求的長度 (包括請求的地址, http請求頭和請求主體) (1.3.12, 1.2.7)
$request_method
HTTP請求方法,一般爲「GET」或「POST」
$request_time
處理客戶端請求使用的時間 (1.3.9, 1.2.6); 從讀取客戶端的第一個字節開始計時。
$request_uri
這個變量等於包含一些客戶端請求參數的原始URI,它沒法修改,請查看$uri更改或重寫URI,不包含主機名,例如:」/cnphp/test.php?arg=freemouse」。
$scheme
請求使用的Web協議, 「http」 或 「https」
$sent_http_name
能夠設置任意http響應頭字段; 變量名中的後半部分「name」能夠替換成任意響應頭字段,如須要設置響應頭Content-length,那麼將「-」替換爲下劃線,大寫字母替換爲小寫,形如:$sent_http_content_length 4096便可。
$server_addr
服務器端地址,須要注意的是:爲了不訪問linux系統內核,應將ip地址提早設置在配置文件中。
$server_name
服務器名,www.cnphp.info
$server_port
服務器端口
$server_protocol
服務器的HTTP版本, 一般爲 「HTTP/1.0」 或 「HTTP/1.1」
$status
HTTP響應代碼 (1.3.2, 1.2.2)
$tcpinfo_rtt, $tcpinfo_rttvar, $tcpinfo_snd_cwnd, $tcpinfo_rcv_space
客戶端TCP鏈接的具體信息
$time_iso8601
服務器時間的ISO 8610格式 (1.3.12, 1.2.7)
$time_local
服務器時間(LOG Format 格式) (1.3.12, 1.2.7)
$uri
請求中的當前URI(不帶請求參數,參數位於$args),能夠不一樣於瀏覽器傳遞的$request_uri的值,它能夠經過內部重定向,或者使用index指令進行修改,$uri不包含主機名,如」/foo/bar.html」。
1,將www.myweb.com/connect 跳轉到connect.myweb.com
rewrite ^/connect$ http://connect.myweb.com permanent;
rewrite ^/connect/(.*)$ http://connect.myweb.com/$1 permanent;
2,將connect.myweb.com 301跳轉到www.myweb.com/connect/
if ($host = "connect.myweb.com"){
rewrite ^/(.*)$ http://www.myweb.com/connect/$1 permanent;
}
3,myweb.com 跳轉到www.myweb.com
if ($host != 'www.myweb.com' ) {
rewrite ^/(.*)$ http://www.myweb.com/$1 permanent;
}
4,www.myweb.com/category/123.html 跳轉爲 category/?cd=123
rewrite "/category/(.*).html$" /category/?cd=$1 last;
5,www.myweb.com/admin/ 下跳轉爲www.myweb.com/admin/index.php?s=
if (!-e $request_filename){
rewrite ^/admin/(.*)$ /admin/index.php?s=/$1 last;
}
6,在後面添加/index.php?s=
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?s=/$1 last;
}
7,www.myweb.com/xinwen/123.html 等xinwen下面數字+html的連接跳轉爲404
rewrite ^/xinwen/([0-9]+)\.html$ /404.html last;
8,http://www.myweb.com/news/radaier.html 301跳轉 http://www.myweb.com/strategy/
rewrite ^/news/radaier.html http://www.myweb.com/strategy/ permanent;
9,重定向 連接爲404頁面
rewrite http://www.myweb.com/123/456.php /404.html last;
10, 禁止htaccess
location ~//.ht {
deny all;
}
11, 能夠禁止/data/下多級目錄下.log.txt等請求;
location ~ ^/data {
deny all;
}
12, 禁止單個文件
location ~ /www/log/123.log {
deny all;
}
13, http://www.myweb.com/news/activies/2014-08-26/123.html 跳轉爲 http://www.myweb.com/news/activies/123.html
rewrite ^/news/activies/2014\-([0-9]+)\-([0-9]+)/(.*)$ http://www.myweb.com/news/activies/$3 permanent;
14,nginx多條件重定向rewrite
若是須要打開帶有play的連接就跳轉到play,不過/admin/play這個不能跳轉
if ($request_filename ~ (.*)/play){ set $payvar '1';}
if ($request_filename ~ (.*)/admin){ set $payvar '0';}
if ($payvar ~ '1'){
rewrite ^/ http://play.myweb.com/ break;
}
15,http://www.myweb.com/?gid=6 跳轉爲http://www.myweb.com/123.html
if ($request_uri ~ "/\?gid\=6"){return http://www.myweb.com/123.html;}
正則表達式匹配,其中:
* ~ 爲區分大小寫匹配
* ~* 爲不區分大小寫匹配
* !~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配
文件及目錄匹配,其中:
* -f和!-f用來判斷是否存在文件
* -d和!-d用來判斷是否存在目錄
* -e和!-e用來判斷是否存在文件或目錄
* -x和!-x用來判斷文件是否可執行
flag標記有:
* last 至關於Apache裏的[L]標記,表示完成rewrite
* break 終止匹配, 再也不匹配後面的規則
* redirect 返回302臨時重定向 地址欄會顯示跳轉後的地址
* permanent 返回301永久重定向 地址欄會顯示跳轉後的地址