Nginx 配置文件简介-1(Nginx 核心功能模块)
Nginx 核心功能模块
更多详情请点击 官方网站核心模块
Module: ngx_http_core_module
Nginx 核心模块负责 Nginx 全局应用,常用全局参数介绍如下:
absolute_redirect
语法:absolute_redirect on | off
默认值: absolute_redirect on;
应用位置: http,server,location
作用: 如果禁用了,nginx 发出的重定向将会是相对的。针对发生301 的URL 是否是完整的。还是相对的。
案例:猛击 here
alias
语法:alias path;
默认值:无
应用位置: location
作用: 定义指定目录的替换目录,也就是给目录起个别名。 http://test/image/, 服务器会映射到 /cars/images 中取文件
案例:
location /image/ {   # 访问时规则里有 / 要带 /, 没有 / ,nginx 会自动做 301 添加 /
    alias /cars/images/;  # 注意此处设置绝对路径
}
client_body_buffer_size
  语法:client_body_buffer_size size;
默认值:client_body_buffer_size 8k|16k;
应用位置:http,server,location
作用:当客户端 post 数据过大时,为了性能需要调大此参数,需要根据 nginx 日志调整。
client_body_temp_path 
  语法:client_body_temp_path path [level1 [level2 [level3]]];
默认值:client_body_temp_path client_body_temp;
应用位置:http,server,location
作用:定义一个目录,用于存储保存客户端请求体的临时文件。只有当上传的请求体超出缓存区大小时,才会写到临时文件中。
client_body_timeout
  语法: client_body_timeout time;
默认值:client_body_timeout 60s;
应用位置: http,server,location
作用:定义客户端请求体超时时间。
client_max_body_size
  语法:client_max_body_size size;
默认值:client_max_body_size 1m;
应用位置: http,server,location
作用:设置nginx允许客户端上传文件最大的值
client_header_buffer_size
  语法:client_header_buffer_size size;
默认值:client_header_buffer_size 1k;
应用位置:http,server
作用:设置客户端请求头的缓存大小,如果请求行或请求头字段不适合此缓冲区,则分配由large_client_header_buffers指令配置的较大缓冲区。
client_header_timeout
  语法:client_header_timeout time;
默认值:client_header_timeout 60s;
应用位置:http,server
作用:定义客户端请求头超时时间
default_type
  语法:default_type mime-type;
默认值:default_type text/plain;
默认值:http,serve,location
作用:定义默认响应的 mime 类型, 我们可以通过删除 mime.types 的文件类型搭建nginx 下载服务器。
etag
语法:etag on | off
默认值:etag on;
应用位置:http,server,location
作用:启用或禁用静态资源的“ETag”响应标头字段的自动生成,和浏览器缓存有关。
if_modified_since 
  语法:if_modified_since off | exact | before;
默认值:if_modified_since exact;
应用位置:http,server,location
作用:指定如何将响应的修改时间与“If-Modified-Since”请求头字段中的时间进行比较。
internal
语法: internal;
默认值: 没有
应用位置:location
作用:指定一个 location 位置,只能被内部请求访问
案例:
error_page 404 /404.html;
location = /404.html {
    internal;
}
keepalive_disable
  语法:keepalive_disable none | browser ...;
默认值:keepalive_disable msie6;
应用位置:http,server,location
作用:禁用与浏览器保持活动链接
keepalive_requests
  语法:keepalive_requests number;
默认值:keepalive_requests 100;
应用位置:http,server,location
作用:设置保持一个活动连接的最大请求数量,到达请求数量后,将关闭连接,反向代理如果有问题,一定要注意此参数。
keepalive_timeout 
  语法:keepalive_timeout timeout [header_timeout];
默认值:keepalive_timeout 75s;
应用位置: http,server,location
作用:设置客户端与服务器保持长连接的时间。 第二个可选参数,会在 http 响应头中添加数据
large_client_header_buffers
  语法:large_client_header_buffers number size;
默认值:large_client_header_buffers 4 8k;
应用位置:http,server
作用:设置读取大客户端请求头的最大数量和缓冲区大小。
limit_except
  语法:limit_except method ... { ... }
默认值: 没有
应用位置:location
作用:在 location 代码块中限制某些 http 方法,如: GET POST HEAD 等
案例: # 放行 GET 方法,同时也放行了 HEAD 方法. 也就是除了 GET 和 HEAD 方法,其余的方法按照代码块的限制执行。
limit_except GET {
    allow 192.168.1.0/32;
    deny  all;
}
limit_rate
  语法:limit_rate rate;
默认值:limit_rate 0; # 为 0 表示不限速
应用位置:http,server,location,if in location
作用:限制向客户端传输的响应速率,单位是每个连接。
listen
  语法:listen address[:port] [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];listen 
    port
    [default_server]
    [ssl]
    [http2 | spdy]
    [proxy_protocol]
    [setfib=number]
    [fastopen=number]
    [backlog=number]
    [rcvbuf=size]
    [sndbuf=size]
    [accept_filter=filter]
    [deferred]
    [bind]
    [ipv6only=on|off]
    [reuseport]
    [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];listen 
    unix:path
    [default_server]
    [ssl]
    [http2 | spdy]
    [proxy_protocol]
    [backlog=number]
    [rcvbuf=size]
    [sndbuf=size]
    [accept_filter=filter]
    [deferred]
    [bind]
    [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
默认值:listen *:80 | *:8000;
应用位置:server
作用:设置IP的地址和端口,或者将要接受请求的 UNIX 套接字路径
案例:
#设置 IP 地址或端口 listen 127.0.0.1:8000; listen 127.0.0.1; listen 8000; listen *:8000; listen localhost:8000; # 设置UNIX 套接字 listen unix:/var/run/nginx.sock;
location
  语法: location [ = | ~ | ~* | ^~ ] uri { ... }    location @name { ... }
默认值:没有
应用位置:server,location
配置案例:
location = / {
    [ configuration A ]
}
location / {
    [ configuration B ]
}
location /documents/ {
    [ configuration C ]
}
location ^~ /images/ {
    [ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}
location /user/ {
    proxy_pass http://user.example.com;
}
location = /user {
    proxy_pass http://login.example.com;
}
log_not_found
  语法:log_not_found on | off;
默认值:log_not_found on;
应用位置: http,server,location
作用: 是否在 error log 显示 404 错误
log_subrequest
  语法:log_subrequest on | off;
默认值:log_subrequest off;
应用位置:http,server,location
作用:是否在 nginx access log 中记录子请求(子请求就是在当前请求中 nginx 再生成一个请求)
merge_slashes
  语法:merge_slashes on | off;
默认值:merge_slashes on;
应用位置:http,server
作用:启用或禁用将URI中的两个或多个相邻斜杠压缩为单个斜杠。
msie_padding
  语法:msie_padding on | off;
默认值:msie_padding on;
应用位置:http,server,location
作用:此指令关闭或开启MSIE浏览器的msie_padding特性,若启用选项,nginx会为response头部填满512字节,这样就阻止了相关浏览器会激活友好错误界面,因此不会隐藏更多的错误信息。Tengine中默认关闭此功能。
msie_refresh
  语法:msie_refresh on | off;
默认值:msie_refresh off;
应用位置: http,server,location
作用:指令允许或拒绝为MSIE发布一个refresh而不是做一次redirect
open_file_cache
 语法:  open_file_cache off;    open_file_cache 
max=N
[inactive=time];
默认值:open_file_cache off;
 应用位置:http, server, location
 作用:配置可以存储的缓存:
           open_file_cache 指令会对以下信息进行缓存:
                * 打开文件描述符的文件大小和修改时间信息
                * 存在的目录信息
                * 搜索文件的错误信息:文件不存在无权限读取等信息
参数详解:
max: 设置缓存中的最大元素数; 在缓存溢出时,删除最近最少使用(LRU)的元素;
inactive: 定义一个时间,如果在此期间未访问该元素,则从该缓存中删除该元素; 默认情况下,它是60秒;
open_file_cache_errors
   语法:open_file_cache_errors on | off;
默认值:open_file_cache_errors off;
   应用位置:http, server, location
作用:通过open_file_cache启用或禁用文件查找错误的缓存
open_file_cache_min_uses
  语法:open_file_cache_min_uses   number;
默认值:open_file_cache_min_uses 1;
  应用位置:http, server, location
作用:设置在open_file_cache指令的inactive参数配置的时间段内文件访问的最小数量,这是文件描述符在缓存中保持打开所必需的。
表示如果在 60S 内某文件被访问的次数低于 1 次,那就将它从缓存中删除。
open_file_cache_valid
  语法:  open_file_cache_valid time;
默认值:open_file_cache_valid 60s;
  应用位置:http, server, location
作用:这个是指多长时间检查一次缓存的有效信息。也就是说即使我一直访问这个文件,30s后会检查此文件的更改信息是否变化,发现变化就更新
实例代码:
open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;
 output_buffers
  语法:output_buffers number size;
默认值:output_buffers 2 32k;
  应用位置:http, server, location
作用:设置用于从磁盘读取响应的缓冲区的数量和大小。
 recursive_error_pages 
  语法:recursive_error_pages on | off;
默认值:recursive_error_pages off;
  应用位置:http, server, location
作用:启用或禁用重定向错误页面。例如:经测试 404 错误 显示为 500 错误。作用控制 error_pages
reset_timedout_connection
  语法:reset_timedout_connection on | off;
默认值:reset_timedout_connection off;
  应用位置:http, server, location
作用:在客户端停止响应之后,允许服务器关闭连接,释放socket关联的内存。
sendfile
  语法:sendfile on | off;
默认值:sendfile off;
  应用位置; http, server, location, if in location
作用; 发送文件内核态解决,不需要进入用户态
tcp_nopush
  语法:tcp_nopush on | off;
默认值:tcp_nopush off;
  应用位置:http, server, location
作用:当使用sendfile函数时,tcp_nopush才起作用,它和指令tcp_nodelay是互斥的。tcp_nopush = on 会设置调用tcp_cork方法,这个也是默认的,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞
try_files
  语法: try_files file ... uri;    try_files file ... =code;
默认值:空
  应用位置:server, location
作用:检查指定顺序的文件是否存在,并使用第一个找到的文件进行请求处理; 处理在当前上下文中执行。如果未找到任何文件,则会进行内部重定向到最后一个参数中指定的uri。
案例:
location /images/ {
    try_files $uri /images/default.gif;
}
# 先尝试 http://localhost/images/,  如果没有这个uri, 则重定向到 http://localhost/images/default.gif
Embedded Variables(嵌入式变量)
内置变量存放在 ngx_http_core_module 模块中,变量的命名方式和apache 服务器变量是一致的。总而言之,这些变量代表着客户端请求头的内容,例如$http_user_agent, $http_cookie, 等等。
常用变量:
$arg_name 请求中的的参数名,即“?”后面的arg_name=arg_value形式的arg_name $args 请求中的参数值 $binary_remote_addr 客户端地址的二进制形式, 固定长度为4个字节 $body_bytes_sent 传输给客户端的字节数,响应头不计算在内;这个变量和Apache的mod_log_config模块中的“%B”参数保持兼容 $bytes_sent 传输给客户端的字节数 (1.3.8, 1.2.5) $connection TCP连接的序列号 (1.3.8, 1.2.5) $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”。
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号