Variables

Thecoremodulesupportsbuilt-invariables,whosenamescorrespondwiththenamesofvariablesinApache.

Firstofall,therearevariableswhichrepresentheaderlinesintheclientrequest,forexample,$http_user_agent,$http_cookie,andsoforth.Notethatbecausethesecorrespondtowhattheclientactuallysends,theyarenotguaranteedtoexistandtheirmeaningisdefinedelsewhere(e.g.inrelevantstandards).

Furthermore,thereareothervariables:

$arg_PARAMETER

ThisvariablecontainsthevalueoftheGETrequestvariablePARAMETERifpresentinthequerystring

$args

ThisvariableistheGETparametersinrequestline,e.g.foo=123&bar=blahblah;Thisvariablecouldbechanged.

$binary_remote_addr

Theaddressoftheclientinbinaryform;

$body_bytes_sent

Theamountofbytessentaspartofthebodyoftheresponse.Isaccurateevenwhenconnectionsareabortedorinterrupted.

$content_length

ThisvariableisequaltolineContent-Lengthintheheaderofrequest;

$content_type

ThisvariableisequaltolineContent-Typeintheheaderofrequest;

$cookie_COOKIE

ThevalueofthecookieCOOKIE;

$document_root

Thisvariableisequaltothevalueofdirectiverootforthecurrentrequest;

$document_uri

Thesameas$uri.

$host

ThisvariableisequaltolineHostintheheaderofrequestornameoftheserverprocessingtherequestiftheHostheaderisnotavailable.

Thisvariablemayhaveadifferentvaluefrom$http_hostinsuchcases:1)whentheHostinputheaderisabsentorhasanemptyvalue,$hostequalstothevalueofserver_namedirective;2)whenthevalueofHostcontainsportnumber,$hostdoesn'tincludethatportnumber.$host'svalueisalwayslowercasesince0.8.17.

$hostname

Settothemachine'shostnameasreturnedbygethostname

$http_HEADER

ThevalueoftheHTTPrequestheaderHEADERwhenconvertedtolowercaseandwith'dashes'convertedto'underscores',e.g.$http_user_agent,$http_referer...;

$is_args

Evaluatesto"?"if$argsisset,""otherwise.

$limit_rate

Thisvariableallowslimitingtheconnectionrate.

$nginx_version

TheversionofNginxthattheserveriscurrentlyrunning;

$query_string

Thesameas$argsexceptthatthisvariableisreadonly.

$remote_addr

Theaddressoftheclient.

$remote_port

Theportoftheclient;

$remote_user

Thisvariableisequaltothenameofuser,authenticatedbytheAuthBasicModule;

$request_filename

Thisvariableisequaltopathtothefileforthecurrentrequest,formedfromdirectivesrootoraliasandURIrequest;

$request_body

Thisvariable(0.7.58+)containsthebodyoftherequest.Thesignificanceofthisvariableappearsinlocationswithdirectivesproxy_passorfastcgi_pass.

$request_body_file

Clientrequestbodytemporaryfilename;

$request_completion

Setto"OK"ifrequestwascompletedsuccessfully.Emptyifrequestwasnotcompletedorifrequestwasnotthelastpartofaseriesofrangerequests.

$request_method

Thisvariableisequaltothemethodofrequest,usuallyGETorPOST.

Thisvariablealwaysevaluatestothemethodnameofthemainrequest,notthecurrentrequest,whenthecurrentrequestisasubrequest.

$request_uri

Thisvariableisequaltothe*original*requestURIasreceivedfromtheclientincludingtheargs.Itcannotbemodified.Lookat$uriforthepost-rewrite/alteredURI.Doesnotincludehostname.Example:"/foo/bar.php?arg=baz"

$scheme

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

rewrite^$scheme://example.com$uriredirect;

$sent_http_HEADER

ThevalueoftheHTTPresponseheaderHEADERwhenconvertedtolowercaseandwith'dashes'convertedto'underscores',e.g.$sent_http_cache_control,$sent_http_content_type....

$server_addr

Theserveraddress.Generallynginxmakesasystemcalltoobtainthisvalue.Toimproveefficiencyandavoidthissystemcall,specifyanaddresswiththelistendirectiveandtousethebindparameter.

$server_name

Thenameoftheserver.

$server_port

Thisvariableisequaltotheportoftheserver,towhichtherequestarrived;

$server_protocol

Thisvariableistheprotocoloftherequest.Commonexamplesare:HTTP/1.0orHTTP/1.1

$uri

ThisvariableisthecurrentrequestURI,withoutanyarguments(see$argsforthose).Thisvariablewillreflectanymodificationsdonesofarbyinternalredirectsortheindexmodule.Notethismaybedifferentfrom$request_uri,as$request_uriiswhatwasoriginallysentbythebrowserbeforeanysuchmodifications.Doesnotincludetheprotocolorhostname.Example:/foo/bar.html

$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

参考地址:http://wiki.nginx.org/NginxHttpCoreModule#Variables