nginx中ngx_http_core_module模块

http核?心模块
指令:
套接字相关的配置
3.1 server{ }
设置虚拟服务器?的配置
Syntax: server { ... }
Default: —
Context: http
server {
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root /PATH/TO/DOCUMENT_ROOT;
}
3.2 server_name
设置虚拟服务器?的名称
Syntax: server_name name ...;
Default: server_name "";
Context: server
server {
server_name example.com www.exam
ple.com;
}
3.3 listen
设置IP的地址和端?口,或服务器?将接受请求的UNIX
域套接字的路路径
Syntax:listen
PORT|address[:port]|unix:/PATH/TO/SOC
KET_FILE
listen address[:port]
[default_server] [ssl] [http2 | spdy]
[backlog=number] [rcvbuf=size]
[sndbuf=size]
Default: listen *:80 | *:8000;
Context: server
listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000;
附加参数
default_server # 设定为默认虚拟主机
ssl # 限制仅能够通过ssl连接提供服务
backlog=number # 超过并发连接数后,新
请求进?入后援队列列的?长度
rcvbuf=size # 接收缓冲区?大?小
sndbuf=size # 发送缓冲区?大?小
3.4 tcp_nodelay
在 keepalived 模式下的连接是否启
?用 TCP_NODELAY 选项,当为 off 时延迟发送,合并
多个请求后再?一起发送。默认是 On ,不不延迟发送
Syntax: tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location
3.5 sendfile
是否启?用 sendfile 功能,在内核中封装报?文直接发
送默认 Off
Syntax: sendfile on | off;
Default: sendfile off;
Context: http, server, location, if
in location
3.6 server_tokens
是否在错误?页?面和“服务器?”响应头字段中显
示 nginx 版本
Syntax: server_tokens on | off |
build | string;
Default: server_tokens on;
Context: http, server, location
定义路路径相关的配置:
3.7 root
设置web资源的路路径映射;?用于指明请求的 URL 所
对应的?文档的?目录路路径
Syntax: root path;
Default: root html;
Context: http, server, location, if
in location
server {
root /data/web/;
}
3.8 location
在?一个 server 中 location 配置段可存在多个,?用
于实现从 URI 到?文件系统的路路径映射; ngnix会根据
?用户请求的 URI 来检查定义的所有 location ,并找
出?一个最佳匹配,?而后应?用其配置
Syntax: location [ = | ~ | ~* | ^~ ]
uri { ... }
location @name { ... }
Default: —
Context: server, location
位置可以由前缀字符串串或正则表达式定义
= :对 URI 做精确匹配;
location = / {
...
}
http://www.example.com/ 匹配
http://www.example.com/index.html 不不
匹配
^~ :对 URI 的最左边部分做匹配检查,不不区
分字符?大?小写
~ :对 URI 做正则表达式模式匹配,区分字
符?大?小写
~* :对 URI 做正则表达式模式匹配,不不区分
字符?大?小写
不不带符号 :匹配起始于此 URI 的所有的 URI
匹配优先级从?高到低:
= , ^~ , ~/~* , 不不带符号
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
""/""请求将匹配配置A,"/index.html"请求
将匹配配置B,"/documents/document.html
"请求将匹配配置C,"/images/1.gif"请求将
匹配配置D,/documents/1.jpg"请求将匹配
配置E.
3.9 alias
路路径别名,?文档映射的另?一种机制
location /images/ {
alias /date/web/;
}
注意: location 中使?用 root 指令和 alias 指令的
意义不不同
root ,给定的路路径对应于 location 中
的 /URI/ 左侧的 /
alias ,给定的路路径对应于 location中
的 /URI/ 右侧的 /
location /images/ {
alias /date/web/;
}
URI:http://www.example.com/images/c
at.jpg
相对应的?文件路路径: /date/web/cat.jpg
------------------------两者区别-----
---------------------
location /images/ {
root /date/web/;
}
URI:http://www.example.com/images/c
at.jpg
相对应的?文件路路径: /date/web/images/c
at.jpg
3.10 error_page
定义错误?页, 以指定的响应状态码进?行行响应
Syntax: error_page code ... [=
[response]] uri;
Default: —
Context: http, server, location, if
in location
error_page 404 /404.html;
error_page 500 502 503 504
/50x.html;
可以使?用URL重定向进?行行错误处理理:
error_page 403 http://example.com/fo
rbidden.html;
error_page 404 =301 http://example.c
om/notfound.html;
3.11 try_files
按顺序检查?文件是否存在,返回第?一个找到的?文件
或?文件夹(结尾加斜线表示为?文件夹),如果所有
的?文件或?文件夹都找不不到,会进?行行?一个内部重定向
到最后?一个参数。只有最后?一个参数可以引起?一个
内部重定向,之前的参数只设置内部 URI 的指向。
最后?一个参数是回退 URI 且必须存在,否则会出现
内部500错误
Syntax: try_files file ... uri;
try_files file ... =code;
Default: —
Context: server, location
location /images/ {
try_files $uri
/images/default.gif; # 注意这?里里是uri,
不不是本地?目录
}
location = /images/default.gif {
expires 30s;
}
定义客户端请求的相关配置:
3.12 keepalive_timeout
设定保持连接超时时?长, 0表示禁?止?长连接
Syntax: keepalive_timeout timeout
[header_timeout];
Default: keepalive_timeout 75s;`
Context: http, server, location
3.13 keepalive_requests
在?一次?长连接上所允许请求的资源的最?大数量量
Syntax: keepalive_requests number;
Default: keepalive_requests 100;
Context: http, server, location
3.14 keepalive_disable
对哪种浏览器?禁?用?长连接
Syntax: keepalive_disable none |
browser ...;
Default: keepalive_disable msie6;
Context: http, server, location
3.15 send_timeout time
向客户端发送响应报?文的超时时?长,此处是指两次
写操作之间的间隔时?长,?而?非整个响应过程的传输
时?长
Syntax: send_timeout time;
Default: send_timeout 60s;
Context: http, server, location
3.16 client_body_buffer_size
?用于接收每个客户端请求报?文的body部分的缓冲区
?大?小;默认为16k;超出此?大?小时,其将被暂存到磁
盘上的由 client_body_temp_path 指令所定义的位

Syntax: client_body_buffer_size
size;
Default: client_body_buffer_size
8k|16k;
Context: http, server, location
3.17 client_body_temp_path
设定?用于存储客户端请求报?文的body部分的临时存
储路路径及?子?目录结构和数量量
Syntax: client_body_temp_path path
[level1 [level2 [level3]]];
Default: client_body_temp_path
client_body_temp;
Context: http, server, location
client_body_temp_path /spool/nginx/c
lient_temp 1 2;
临时?文件的路路径可能如下所示:
/spool/nginx/client_temp/7/45/000001
23457
对客户端进?行行限制的相关配置:
3.18 limit_rate
对客户端进?行行限制的相关配置
Syntax: limit_rate rate;
Default: limit_rate 0;
Context: http, server, location, if
in location
server {
if ($slow) {
set $limit_rate 4k;
}
...
}
3.19 limit_except
限制客户端使?用除了了指定的请求?方法之外的其它?方

Syntax: limit_except method ... { ...
}
Default: —
Context: location
limit_except GET {
allow 192.168.1.0/32;
deny all;
}
?文件操作优化的配置:
3.20 aio
是否启?用aio功能
Syntax: aio on | off |
threads[=pool];
Default: aio off;
Context: http, server, location
location / video / {
aio on;
output_buffers 1 64k;
}
3.21 directio
是否同步(直接)写磁盘,?而?非写缓存,在Linux主
机启?用 O_DIRECT 标记, 则?文件?大于等于给定?大?小
时使?用,例例如directio 4m
Syntax: directio size | off;
Default: directio off;
Context: http, server, location
3.22 open_file_cache
是否缓存查找时发?生错误的?文件?一类的信息
Syntax: open_file_cache off;
open_file_cache max=N
[inactive=time];
Default: open_file_cache off;
Context: http, server, location
3.22 open_file_cache_min_uses
open_file_cache指令的inactive参数指定的时?长内,
?至少被命中此处指定的次数?方可被归类为活动项
Syntax: open_file_cache_min_uses
number;
Default: open_file_cache_min_uses 1;
Context: http, server, location
3.22 open_file_cache_valid
缓存项有效性的检查频率
Syntax: open_file_cache_valid time;
Default: open_file_cache_valid 60s;
Context: http, server, location

相关文章