服務(wù)器相關(guān)
- $server_name:虛擬主機(jī)名(server_name指令的配置)
- $document_root:root或alias命令配置的路徑,可能是軟鏈接。
- $realpath_root:當(dāng)前請求對應(yīng)到的真實(shí)路徑,絕對路徑。
- $request_filename:當(dāng)前請求的文件路徑,基于root或alias的配置組合出的路徑
- $host:如果請求頭中有Host就為該值,否則為server_name(nginx配置中的server)
- $limit_rate:limit_rate指令的設(shè)置值。
變量用途舉例,記錄日志:
http {
log_format about_server_log 'server_addr:$server_addr,server_name:$server_name,server_port:$server_port\n'
'document_root:$document_root,realpath_root:$realpath_root,request_filename:$request_filename\n'
'host:$host,hostname:$hostname,nginx_version:$nginx_version\n'
'pid:$pid,limit_rate:$limit_rate';
server {
listen 80;
server_name localhost;
access_log logs/server.log about_server_log;
location / {
root /var/www/html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://localhost:8009/api/;
limit_rate 100k;
}
}
}
請求http://xxx.xxx.xxx.xxx/5.jpg
時(shí),$document_root值為root指令配置的路徑,為軟鏈接,$realpath_root的值為真實(shí)物理路徑。請求http://xxx.xxx.xxx.xxx/api/student/11
時(shí)由代理服務(wù)響應(yīng),$request_filename值是基于root的配置與$uri組合得出的:
客戶端相關(guān)
- $binary_remote_addr:客戶端IP的二進(jìn)制
- $remote_user:Http Basic認(rèn)證時(shí)的用戶名
- $request:http請求,包括方法,uri,協(xié)議及版本
- $request_completion:請求完成時(shí)值為OK,否則空字符串
- $args或$query_string:請求參數(shù)
- $arg_name:請求參數(shù)中的name值
- $is_args:如果有請求參數(shù)值為?否則為空字符串
- $scheme:請求協(xié)議,http或https
- $https:連接協(xié)議如果為https值為on,否則為空字符串
- $content_length:請求頭中的Content-Length
- $content_type:請求頭中的Content-Type
- $cookie_name:取cookie中key為name的值,name也只可以是其它名稱
- $server_protocol:http協(xié)議版本
用于if判斷舉例:
server {
listen 80;
server_name localhost;
location /api/ {
# 傳遞Http Basic認(rèn)證的用戶名給上游服務(wù)
proxy_set_header X-User $remote_user;
# 判斷請求方法
if ($request_method != "GET") {
return 405 "method not allowed";
}
# 檢查能處理的Content-Type類型
if ($content_type != "application/json") {
return 415 "Unsupported Media Type";
}
proxy_pass http://localhost:8009/api/;
}
}
server {
listen 81;
server_name localhost;
location / {
# 將http請求重定向到https:
if ($scheme = "http") {
return 301 https://$host:443$request_uri;
}
...
}
}
響應(yīng)相關(guān)
- $sent_http_name:響應(yīng)頭中name值
- $body_bytes_sent:響應(yīng)體字節(jié)數(shù)
- $bytes_sent:響應(yīng)字節(jié)數(shù)
代理相關(guān)
- $upstream_addr:上游服務(wù)器的IP和端口
- $upstream_status:上游服務(wù)器的響應(yīng)狀態(tài)碼
- $upstream_response_time:上游服務(wù)的響應(yīng)時(shí)長
- $proxy_host:proxy_pass指令中目標(biāo)服務(wù)器的主機(jī)名和端口
- $proxy_port:proxy_pass指令中目標(biāo)服務(wù)器的端口
閱讀原文:原文鏈接
該文章在 2025/7/1 23:50:06 編輯過