zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Nginx中全局变量整理小结

Nginx 整理 小结 全局变量
2023-06-13 09:14:24 时间

Variables

Thecoremodulesupportsbuilt-invariables,whosenamescorrespondwiththenamesofvariablesinApache.

Firstofall,therearethevariables,whichrepresentthelinesofthetitleoftheclientrequest,forexample,$http_user_agent,$http_cookie,andsoforth.

Furthermore,thereareothervariables:
Editsection:$arg_PARAMETER$arg_PARAMETER

ThisvariablecontainsthevalueoftheGETrequestvariablePARAMETERifpresentinthequerystring
Editsection:$args$args

Thisvariableisequaltoargumentsinthelineofrequest;
Editsection:$binary_remote_addr$binary_remote_addr

Theaddressoftheclientinbinaryform;
Editsection:$body_bytes_sent$body_bytes_sent

(undocumented)
Editsection:$content_length$content_length

ThisvariableisequaltolineContent-Lengthintheheaderofrequest;
Editsection:$content_type$content_type

ThisvariableisequaltolineContent-Typeintheheaderofrequest;
Editsection:$cookie_COOKIE$cookie_COOKIE

ThevalueofthecookieCOOKIE;
Editsection:$document_root$document_root

Thisvariableisequaltothevalueofdirectiverootforthecurrentrequest;
Editsection:$document_uri$document_uri

Thesameas$uri.
Editsection:$host$host

ThisvariableisequaltolineHostintheheaderofrequestornameoftheserverprocessingtherequestiftheHostheaderisnotavailable.

Thisvariablemayhaveadifferentvaluefrom$http_hostwhentheHostinputheaderisabsentorhasanemptyvalue.
Editsection:$http_HEADER$http_HEADER

ThevalueoftheHTTPheaderHEADERwhenconvertedtolowercaseandwith‘dashes"convertedto‘underscores",e.g.$http_user_agent,$http_referer…;
Editsection:$is_args$is_args

Evaluatesto“?”if$argsisset,“”otherwise.
Editsection:$limit_rate$limit_rate

Thisvariableallowslimitingtheconnectionrate.
Editsection:$query_string$query_string

Thesameas$args.
Editsection:$remote_addr$remote_addr

Theaddressoftheclient.
Editsection:$remote_port$remote_port

Theportoftheclient;
Editsection:$remote_user$remote_user

Thisvariableisequaltothenameofuser,authenticatedbytheAuthBasicModule;
Editsection:$request_filename$request_filename

Thisvariableisequaltopathtothefileforthecurrentrequest,formedfromdirectivesrootoraliasandURIrequest;
Editsection:$request_body$request_body

Thisvariable(0.7.58+)containsthebodyoftherequest.Thesignificanceofthisvariableappearsinlocationswithdirectivesproxy_passorfastcgi_pass.
Editsection:$request_body_file$request_body_file

Clientrequestbodytemporaryfilename;
Editsection:$request_completion$request_completion

(undocumented)
Editsection:$request_method$request_method

Thisvariableisequaltothemethodofrequest,usuallyGETorPOST.

Beforeandincluding0.8.20,thisvariablealwaysevaluatestothemethodnameofthemainrequest,notthecurrentrequestifthecurrentrequestisasubrequest.
Editsection:$request_uri$request_uri

ThisvariableisequaltothecompleteinitialURItogetherwiththearguments;
Editsection:$scheme$scheme

TheHTTPscheme(i.e.http,https).Evaluatedonlyondemand,forexample:

rewrite^(.+)$$scheme://example.com$1redirect;

Editsection:$server_addr$server_addr

Equaltotheserveraddress.Asarule,forobtainingthevalueofthisvariableisdoneonesystemcall.Inordertoavoidsystemcall,itisnecessarytoindicateaddressesindirectiveslistenandtouseparameterbind.
Editsection:$server_name$server_name

Thenameoftheserver.
Editsection:$server_port$server_port

Thisvariableisequaltotheportoftheserver,towhichtherequestarrived;
Editsection:$server_protocol$server_protocol

Thisvariableisequaltotheprotocolofrequest,usuallythisHTTP/1.0orHTTP/1.1.
Editsection:$uri$uri

ThisvariableisequaltocurrentURIintherequest,itcandifferfrominitial,forexamplebyinternalredirects,orwiththeuseofindexitisfilewithinternalredirects.

参考:

http://www.givingtree.com.cn/entry/Nginx-Location%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95

http://wiki.nginx.org/NginxHttpCoreModule#Variables


$args此变量与请求行中的参数相等

$content_length等于请求行的“Content_Length”的值。

$content_type等同与请求头部的”Content_Type”的值

$document_root等同于当前请求的root指令指定的值

$document_uri与$uri一样

$host与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样

$limit_rate允许限制的连接速率

$request_method等同于request的method,通常是“GET”或“POST”

$remote_addr客户端ip

$remote_port客户端port

$remote_user等同于用户名,由ngx_http_auth_basic_module认证

$request_filename当前请求的文件的路径名,由root或alias和URIrequest组合而成

$request_body_file

$request_uri含有参数的完整的初始URI

$query_string与$args一样

$server_protocol等同于request的协议,使用“HTTP/1.0”或“HTTP/1.1”

$server_addrrequest到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。

$server_name请求到达的服务器名

$server_port请求到达的服务器的端口号

$uri等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index