nginx http_core模块内嵌变量

此页面版本:2016-06-15
本文介绍nginx的http_core模块的内嵌变量

ngx_http_core_module模块支持内嵌变量与Apache服务器变量名称一致。首先,这些变量体现请求头域,如$http_user_agent,$http_cookie等。还有其他的变量:

$arg_name
在请求中name参数


$args
请求的所有参数


$binary_remote_addr
客户端地址的二进制形式,值的长度总是4个字节


$body_bytes_sent
发送到客户端的字节数,不算响应头。这个变量与Apache的mod_log_config模块的“%B”参数兼容。


$bytes_sent
发送到带客户端的字节数(1.3.8+,1.2.5+)


$connection
连接序列号(1.3.8+,1.2.5+)


$connection_requests
当前连接中的请求数(1.3.8+,1.2.5+)


$content_length
请求头中的“Content-Length”


$content_type
请求头中的“Content-Type”


$cookie_name
名称为name的cookie的值


$document_root
当前请求的root或alias指令的值


$document_uri
同$uri


$host
按此优先级:请求中的Host字段,或服务器名称匹配请求的值


$hostname
主机名


$http_name
任意请求头域,变量名的最后一部分name是将域转为小写并将“-”替换为“_”


$https
如果通过SSL连接则值为“on”,否则是空字符串


$is_args
如果请求有参数则值为“?”,否则是空字符串


$limit_rate
设置此值开启响应的限速


$msec
当前时间的秒数,精确到毫秒(1.3.9+, 1.2.6+)


$nginx_version
nginx版本


$pid
工作进程的PID


$pipe
如果请求是流水线(pipelined)值为“p”,否则为“.”(1.3.12+, 1.2.7+)


$proxy_protocol_addr
代理协议头中的客户端地址,如果没有则为空字符串(1.5.12+)
代理协议必须提前在listen指令的proxy_protocol参数开启。


$proxy_protocol_port
代理协议头中的客户端端口号,如果没有则为空字符串(1.11.0+)
代理协议必须提前在listen指令的proxy_protocol参数开启。


$query_string
同$args


$realpath_root
将一个绝对路径与符号连接解析成相对路径


$remote_addr
客户端地址


$remote_port
客户端端口


$remote_user
Basic验证的用户名


$request
完整的请求行


$request_body
请求内容


$request_body_file
带有请求内容的临时文件名称


$request_completion
如果请求完成了,值为“OK”,否则为空字符串


$request_filename
当前请求的文件路径,基于root和alias指令与请求URI


$request_id
唯一的请求标识符,从16个随机字节生成,为16进制(1.11.0)


$request_length
请求长度(包括请求行、头部、正文内容)(1.3.12+, 1.2.7+)


$request_method
请求方法,通常是“GET”或“POST”


$request_time
处理请求的时间秒数,精确到毫秒(1.3.9+, 1.2.6+)


$request_uri
完整的请求URI(带参数)


$scheme
请求模式,“http”或“https”


$sent_http_name
任意响应头域,最后一部分name将名称转为小写并将“-”替换为“_”


$server_addr
服务器接收请求的地址
计算该变量的值需要一次系统调用,要避免系统调用,listen指令必须定义地址并使用bind参数


$server_name
处理请求的服务器名称


$server_port
处理请求的服务器端口


$server_protocol
请求协议,通常是“HTTP/1.0”,“HTTP/1.1”或“HTTP/2.0”


$status
响应状态码(1.3.2+, 1.2.2+)


$tcpinfo_rtt, $tcpinfo_rttvar, $tcpinfo_snd_cwnd, $tcpinfo_rcv_space
客户端TCP连接的信息,在支持TCP_INFO socket选项的系统中可用


$time_iso8601
ISO 8601格式的本地时间(1.3.12+, 1.2.7+)


$time_local
普通日志格式的本地时间(1.3.12+, 1.2.7+)


$uri
当前请求的标准化URI
$uri的值在处理请求时可能会发生变化,如内部重定向或使用索引文件

Nginx中文文档-ngx_http_core_module(T-V)

本文档包含以下指令:tcp_nodelay、tcp_nopush、try_files、types、types_hash_bucket_size、types_hash_max_size、underscores_in_headers、variables_hash_bucket_size、variables_hash_max_size

tcp_nodelay

语法:tcp_nodelay on | off
默认:tcp_nodelay on
上下文: http, server, location

启用或禁用TCP_NODELAY选项。该功能仅在连接已过渡到keep-alive状态下有效。

tcp_nopush

语法:tcp_nopush on | off
默认:tcp_nopush off
上下文:http, server, location

启用或禁用tcp_nopush功能,在FreeBSD系统上通过TCP_NOPUSH socket选项实现,在Linux通过TCP_CORK socket选项实现。只有在sendfile生效状态下才能启用。启用该选项将允许:

  • Linux和FreeBSD 4.*系统下在一个包中发送响应头和文件开头
  • 在满包中发送一个文件

try_files

语法:try_files fileuri;
try_files file … =code;
默认:—
上下文:server, location

按指定顺序检测文件是否存在,并返回第一个找到的文件,该过程在当前上下文中执行。文件的路径是由root和alias指令以及file参数构成。有可能会通过在名字后面添加“/”检测目录是否存在,例如“$uri/”。如果没有找到任何的文件,则会产生一个内部重定向到最后一个参数uri指定的文件:

location /images/ {
    try_files $uri /images/default.gif;
}

location = /images/default.gif {
    expires 30s;
}

最后一个参数可以指向一个location,如下面的例子所示。从0.7.51版本开始,最后一个参数也可以是一个code

location / {
    try_files $uri $uri/index.html $uri.html =404;
}

代理Mongrel的例子:

location / {
    try_files /system/maintenance.html
              $uri $uri/index.html $uri.html
              @mongrel;
}

location @mongrel {
    proxy_pass http://mongrel;
}

Drupal/FastCGI举例:

location / {
    try_files $uri $uri/ @drupal;
}

location ~ \.php$ {
    try_files $uri @drupal;

    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME     $fastcgi_script_name;
    fastcgi_param QUERY_STRING    $args;

    ... other fastcgi_param's
}

location @drupal {
    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to/index.php;
    fastcgi_param SCRIPT_NAME     /index.php;
    fastcgi_param QUERY_STRING    q=$uri&$args;

    ... other fastcgi_param's
}

下面的例子中,

location / {
    try_files $uri $uri/ @drupal;
}

try_files指令等价于:

location / {
    error_page 404 = @drupal;
    log_not_found off;
}

下面的例子,

location ~ \.php$ {
    try_files $uri @drupal;

    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name;

    ...
}

try_files指令在将请求发送到FastCGI服务器之前检测文件是否存在。
Wordpress 和 Joomla的例子:

location / {
    try_files $uri $uri/ @wordpress;
}

location ~ \.php$ {
    try_files $uri @wordpress;

    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name;
    ... other fastcgi_param's
}

location @wordpress {
    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to/index.php;
    ... other fastcgi_param's
}

types

语法:types { … }
默认:types {
text/html html;
image/gif gif;
image/jpeg jpg;
}
上下文:http, server, location

响应的MIME与文件扩展的对照表,大小写不敏感。多个扩展名可以映射为同一个类型:

types {
    application/octet-stream bin exe dll;
    application/octet-stream deb;
    application/octet-stream dmg;
}

在conf/mime.types文件中定义了一个比较完整的映射表。
要定义一个特殊的location,所有的MIME类型都是application/octet-stream,可以用下面的配置:

location /download/ {
    types        { }
    default_type application/octet-stream;
}

types_hash_bucket_size

语法:types_hash_bucket_size size
默认:types_hash_bucket_size 64
上下文:http, server, location

设置一个类型哈希表的大小。1.5.13版本之前,默认值的大小取决于CPU缓存行。

types_hash_max_size

语法:types_hash_max_size size
默认:types_hash_max_size 1024
上下文:http, server, location

设置类型哈希表最大的大小。

underscores_in_headers

语法:underscores_in_headers on | off
默认:underscores_in_headers off
上下文:http, server

启用或禁用客户端请求头域中的下划线。如果禁用了此功能,客户端请求头域中的下划线会被认为是无效的。
如果该指令定义在sever块中,只对默认的服务器有效。该值同样作用于监听了同一端口和IP的虚拟主机。

variables_hash_bucket_size

语法:variables_hash_bucket_size size
默认:variables_hash_bucket_size 64
上下文:http

设置一个变量哈希表的大小。

variables_hash_max_size

语法:variables_hash_max_size size
默认:variables_hash_max_size 1024
上下文:http

设置最大的变量哈希表大小。1.5.13版本以前,默认值是512.