nginx各模块配置
一、ngx_http_core_module
1.aio on |off|sendfile; default:off; context:http,server,location;
启用或禁用asynchronous file I/O(AIO,异步文件IO)在FreeBSD和linux下的使用,在FreeBSD中,AIO从4.3开始支持,可随内核启动(options VFS_AIO)或在内核加载模块时启动(kldload aio),在FreeBSD的某些版本中,开启aio可能会产生网络锁定影响效率,在启用aio时需要关闭sendfile,在FreeBSD 5.2.1+和nginx 0.8.12+中,aio可以用来提前加载sendfile:
location /video/ {
sendfile on;
tcp_nopush on;
aio sendfile;
}
上面的配置中,sendfile()是使用SF_NODISKIO方式调用的,不会阻塞于硬盘IO,在数据不存在于内存时返回信息,nginx就采用异步加载的方式来读取一个字节,FreeBSD系统核心接下来加载128k文件进内存,接下来的部分每次16k分块读取(可通过read_ahead参数调整
在linux中,aio从内核2.6.22以上可用,但需要启用directio,不然读取会被阻塞
location /video/ {
aio on;
directio 512;
output_buffers 1 128k;
}
linux 中directio只可用来读取整512字节的块(XFS下是4k),读取不是512字节的块时依然会当成整块,flv文件请求是不从头开始读的,因为从头或尾读文件均会阻塞,在directio使用时sendfile会被默认关闭,不再需要手动关闭
2.alias path; context:location; 定义一个location的替代方法
location /i/ {
alias /data/w3/images/;
}
"/i/top.fig"的请求会得到/data/w3/images/top.gif的返回
path值可以包含$document_root,$realpath_root之外的变量
如果alias在使用正则表达式的location中使用,那么正则表达式需要包含捕获值,在alias中该捕获值须被使用
location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ {
alias /data/w3/images/$1;
}
当location与参数值最后一个相同时:
location /images/ {
alias /data/w3/images/;
}
用root参数替换效果更好
location /images/ {
root /data/w3;
}
3.chunked_transfer_encoding on|off; default:chunked_transfer_encoding on; context: http,server,location 在HTTP/1.1中允许禁用分块传输编码,使用软件来支持也许更好
4.client_body_buffer_size size;client_body_buffer_size 8k|16k;context:http,server,location; 设置读取请求内容的缓冲区大小,当请求内容大于缓冲区大小的,整个请求或部分会被写进一个临时文件,默认的情况下该缓冲区大小是两倍内存片大小,在x86,x86-64和其它32位系统中是8k,在其它64位系统中是16k,这样的话应该默认就好,无需主动设置
5.client_body_in_file_only on|clean|off; default client_body_in_file_only off; context:http,server,location;
该参数决定nginx是否将整个请求内容保存为文件,在调试时该参数很有用,或者在使用 $request_body_file参数及使用ngx_http_perl_module的$request_body_file方法时;当设置该参数on时,临时文件在处理请求时不会被删除,参数为clean时在请求处理完成后临时文件依然保留
6.client_body_in_single_buffer on|off; default client_body_in_single_buffer off;context:http,server,location
定义nginx是否保存整个客户端请求到单独缓冲区,该参数在使用$request_body变量时建议开启,用来记录涉及的copy操作
7.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/client_temp 1 2;临时文件就会存成/spool/nginx/client_temp/7/45/00001234567
8.client_body_timeout time;default:client_body_timeout 60s;context:http,server,location
定义读取客户端请求内容的超时时间,该超时是两个成功读取之间的超时而非整个请求内容的传输超时,如果客户端在该时间间隔内没有传任何东西,那么408错误(request time-cout)将被返回
9.client_header_buffer_size size; default:client_header_buffer_size 1k;context:http,server
设置读取客户端请求头信息的缓冲区大小,对于大部分请求,1k的缓冲区就够,但如果一个请求有很多cookie信息或者是从wap客户端发来的时候,1k也许太小了,如果请求头大于该参数设定值时,会根据配置中large_client_header_bufers参数设置的值来分配更大的缓冲区
10.client_header_timeout time;default:client_header_timeout 60s;context
:http,server
定义读取客户端请求头信息的超时时间,如果在这时间内客户端没有把完整的头信息传输过来,将返回408错误
11.client_max_body_size size;default client_max_body_size 1m;context:http,server,location;
定义客户端请求数据的最大允许值,在请求头的"Content-Length"中指定,如果大于配置的值,413错误(请求内容太大)将被返回,但浏览器是无法正确识别此错误的,把size设置为0意味着不进行大小检查
12.connection_pool_size size;default connection_pool_size 256;context:http,server;
可用来调整每个连接的内存分配,对性能优化效果很小,一般不用。
13.default_type mimi-type;default:default_type text/plain;context:http,server,location
定义返回数据的mime类型
14.directio size|off;default directio off;context:http,server,location
当读取较大文件时自动禁用sendfile,在需要处理大文件时有用,需要接收大文件上传时 directio 4m;
15.directio_alignment size;default:derectio_alignment 512;context:http,server,location
设置directio的队列大小,大部分情况下只需512k,在使用XFS是需要4k
16.disable_symlinks off|on|if_not_owner [from=part];default:disable_symlinks off;context:http,server,location
决定打开文件符号链接时的处理方式,off允许符号链接,on当路径任意部分为符号链接时即禁止访问,if_not_owner当路径某部分为符号链接,且符号链接与目标文件所有者不同时禁止访问,from=part,只检查一部分
17.error_page code ...[=[response]] uri;context:http,server,location,if in location;
定义当出现错误的时候显示的页面,例:
error_page 404 /404.html;
error_page 502 503 504 /50x.html;
error_page 403 www.test.com/forbidden.html
还可把状态码改变:error_page 404=200 /empty.gif
当从代理服务器或fastcgi服务器返回错误时,可能只返回错误代码,可以用返回的代码来响应:error_page 404=/404.php
如果在重定向的时候不需要改变URI,可以将错误处理重定向到特定location:
location /{ error_page 404=@fallback;} location @fallback{ proxy_pass http://test;}
18.etag on|off;default:etag on;context:http,server,location
启用或禁用静态资源的自动产生etag机制
19.if_modified_since off|exact|before;default:if_modified_since exact;context:http,server,location
指定如何通过请求头部的If-Modified-Since来比较返回的修改时间
off的时候不比较,exact时进行严格比较,before修改时间少于或等于请求头的时间值
20.ignore_invalid_headers on|off;default:ignore_invalid_headers on;context:http,server
控制不合法名字的头信息是否被忽略
21.internal;context:location
指定一个只用于内部请求的location,外部客户端请求时返回404,内部请求如下:
被error_page ,index,random_index,try_files这些参数重定向的请求
被负载均衡服务器的返回的X-Accel-Redirect头信息重定向的请求
被ngx_http_addition_module参数及ngx_http_ssi_module参数格式化的子请求
被rewrite参数改变的请求
例:error_page 404 /404.html; location /404.html{ internal; }
22.keepalive_disable none|browser...;default:keepalive_disable msie6;context:http,server,location
对指定浏览器禁用keepalive,msie6则禁用ie6以下版本,safari则禁用在Mac OS 或类似系统上的safari及类safari浏览器,none则对所有浏览器启用keepalive
23.keepalive_requests number;default:keepalive_requests 100;context:http,server,location
设置一个keep-alive的连接能处理的最大请求数量
24.keepalive_timeout timeout[header_timeout];default:keepalive_timeout 75s;context:http,server,location;
第一个参数设置服务器端处理一个keep-alive连接的最长超时时间,第二个可选参数则指定返回头里的Keep-Alive:timeout=time 的值,两个参数的值可以不同,返回头里的timeout会被mozilla和konqueror识别,ie会在60秒内自动关闭keep-alive连接
25.large_client_header_buffers number size;default large_client_header_buffers 4 8k;context:http,server;
设置读取大的客户端请求时缓冲区的最大的数量和大小,一个请求不能超过size,否则会返回414(request-uri too large),请求头也能超过size,否则返回400(bad request),buffer可以根据需要分配,默认8k。当请求处理完后连接转变为keep-alive状态时,缓冲区会被释放
26.limit_except method...{...};context:location;
限制在location中启用的http方法,参数可以是:GET,HEAD,POST,PUT,DELETE,MKCOL,COPY,MOVE,OPTIONS,PROPFIND,PROPPATCH,LOCK,UNLOCK,PATCH,允许GET方法的同时会启用HEAD方法,可以通过ngx_http_access_module和ngx_http_auth_basic_module来限制其它方法的使用:
limit_except GET{ allow 192.168.1.0/32;deny all; }该配置会限制除了get和head之外的一切方法
27.limit_rate rate;default limit_rate 0;context:http,server,location,if in location
限制向客户端传输响应的速率,以byte表示,设置为0时代表无限制,这个limit是针对连接的,所以如果同一客户端开启两个连接,那全部速率要超出参数的2倍
当只需要针对某些客户端进行速率限制时,可用$limit_rate指定:
server{
if($slow) { set $limit_rate 4k; } ...
}
另外还可以通过返回头信息中的X-Accel-Limit-Rate来指定,该属性可通过proxy_ignore_headers和fastcgi_ignore_headers参数禁用
28.limit_rate_after size; default:limit_rate_after 0;context:http,server,location,if in location
设置在传输完指定大小后再开启速率限制:location /flv/{ flv;limit_rate_after 500k;limit_rate 50k; }
29.lingering_close off|on|always;default:lingering_close on;context:http,server,location