nginx / Web Service

15.4 nginx主配置文件(ngx_http_core_module:http全局配置段)

 

跟客户端相关的配置;
文件操作相关的配置;
=================================

include       /etc/nginx/mime.types;
default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

sendfile       on;
tcp_nopush     on;
tcp_nodelay	   on;


keepalive_timeout  65;
types_hash_max_size 2048;

说明:

sendfile:

Syntax:		sendfile on | off;
Default:	sendfile off;
Context:	http, server, location, if in location

传统的文件传输中用到了系统调用(read/write方式),具体调用流程如下描述:
1> 调用read函数,文件数据被copy到内核缓冲区;
2> read函数返回,文件数据从内核缓冲区copy到用户缓冲区;
3> write函数调用,将文件数据从用户缓冲区copy到内核与socket相关的缓冲区;
4> 数据从socket缓冲区copy到相关协议引擎;

sendfile开启后,数据不用进过用户空间,直接在内核空间完成传输,流程如下:
1> sendfile系统调用利用DMA引擎将文件数据拷贝到内核缓冲区,之后数据被拷贝到内核socket缓冲区中;
2> DMA引擎将数据从内核socket缓冲区拷贝到协议引擎中;

tcp_nopush

Syntax:		tcp_nopush on | off;
Default:	tcp_nopush off;
Context:	http, server, location

只有在sendfile开启时有效;
把响应报文的首部和请求的数据的起始部分封装成一个包文件发给客户端;服务器端发送一个完整数据包的文件;

tcp_nodelay

Syntax:		tcp_nodelay on | off;
Default:	tcp_nodelay on;
Context:	http, server, location

仅在长连接开启时才生效;
客户端向服务器请求数据,如果多次请求的数据都比较小,服务器端不会对所请求的数据做统一打包再发给客户端,客户端不会感觉到延迟;

keepalive_timeout:<与客户端请求相关的配置>

Syntax:		keepalive_timeout timeout [header_timeout];
Default:	keepalive_timeout 65s;
Context:	http, server, location

长连接超时时间,默认是65秒;0表示禁止长连接;

keepalive_requests:<与客户端请求相关的配置>

Syntax:		keepalive_requests number;
Default:	keepalive_requests 100;
Context:	http, server, location

长连接的最多请求次数,默认值是100;
备注:在开启长连接功能后,客户端请求数达到100个或者65秒后就断开连接,再次请求数据时需要重新建立连接;

send_timeout:<与客户端请求相关的配置>

Syntax:		send_timeout time;
Default:	send_timeout 60s;
Context:	http, server, location

向客户端发送响应报文的超时时长,此处是指2次写操作之间的间隔时长;

client_body_buffer_size:<与客户端请求相关的配置>

Syntax:		client_body_buffer_size size;
Default:	client_body_buffer_size 8k|16k;
Context:	http, server, location

用于接收客户端请求报文的body部分的缓冲区大小;默认为16K,超出此大小时,其将被暂存到磁盘上的由client_body_temp_path指令所定义的位置;

client_body_temp_path:<与客户端请求相关的配置>

Syntax:		client_body_temp_path path [level1 [level2 [level3]]];
Default:	client_body_temp_path client_body_temp;
Context:	http, server, location

设定用于存储客户端请求报文的body部分的临时存储路径及子目录结构和数量;
举例:
client_body_temp_path /var/cache/nginx/client_body_temp 2 1 1
2:表示用2位16进制数字标识一级子目录;
1:表示用1位16进制数字标识二级子目录;
1:表示用1位16进制数字标识三级子目录;

types_hash_max_size:

Syntax:		types_hash_max_size size;
Default:	types_hash_max_size 1024;
Context:	http, server, location

nginx为了加速对资源的访问,把各种’mime’类型经过hash计算后存储在内存中,
客户端请求某种对象资源时直接比较hash列表即可;

limit_rate:<与客户端进行限制相关的配置>

Syntax:		limit_rate rate;
Default:	limit_rate 0;
Context:	http, server, location, if in location

限制响应给客户端的传输速率,单位bytes/second,0表示不限速;

limit_except:<与客户端进行限制相关的配置>

Syntax:		limit_except method ... { ... }
Default:	—
Context:	location

Limits  allowed  HTTP  methods  inside  a  location.
The  method  parameter  can  be  one  of  the  following: GET, HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, or PATCH.
Allowing  the  GET  method  makes  the  HEAD  method  also  allowed.

limit_except GET {
       allow   192.168.1.0/32;
       deny   all;
}

Please  note  that  this  will  limit  access  to  all  methods  except  GET  and  HEAD.

*****************************************************************************************

<文件操作优化的相关配置>:
===============================
aio:

Syntax:		aio on | off | threads[=pool];
Default:	aio off;
Context:	http, server, location

是否开启异步文件I/O;

举例:
location /video/ {
        aio   on;
        directio   512;
        output_buffers  1  128k;
}

directio:

Syntax:		directio size | off;
Default:	directio off;
Context:	http, server, location

在Linux主机上启用O_DIRECT标记,此处表示文件大于等于给定的文件大小时使用;
举例:directio   4m;

open_file_cache:

Syntax:		open_file_cache off;
      open_file_cache max=N [inactive=time];
Default:	open_file_cache off;
Context:	http, server, location

nginx可以缓存以下3类信息:
 1> 文件的描述符、文件大小、最近一次的修改时间;
 2> 打开的文件的目录结构;
 3> 没有找到的或者没有权限访问的文件的相关信息;

max=N 可缓存的缓存项上限;达到上限后会使用LRU算法(最近最少)实现缓存管理;
inactive=time 缓存项的非活动时长;
在此处指定的时长内未被命中的或命中次数少于open_file_cache_min_uses指令所指定的缓存项即为非活动项;
举例:
      open_file_cache max=1000 inactive=20s;
      open_file_cache_valid 30s;
      open_file_cache_min_uses 2;
      open_file_cache_errors on;

open_file_cache_valid:

Syntax:		open_file_cache_valid time;
Default:	open_file_cache_valid 60s;
Context:	http, server, location

缓存项有效性的检查频率;

open_file_cache_min_uses:

Syntax:		open_file_cache_min_uses number;
Default:	open_file_cache_min_uses 1;
Context:	http, server, location

在open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次才被归类为活动项;

open_file_cache_errors:

Syntax:		open_file_cache_errors on | off;
Default:	open_file_cache_errors off;
Context:	http, server, location

是否缓存查找时发生错误的文件类的信息;

Leave a Reply

Your email address will not be published. Required fields are marked *