nginx http模块配置参数解读

本文主要解析一下nginx http模块配置参数。主要分socket相关参数,对clinet请求的buffer参数以及对response的buffer参数。

socket

名称 默认配置 作用域 官方说明 中文解读 模块
sendfile sendfile off; http, server, location, if in location Enables or disables the use of sendfile(). 设置为on可以启用Linux上的sendfile系统调用来发送文件,它减少了内核态与用户态之间的两次内存复制,这样就会从磁盘中读取文件后直接在内核态发送到网卡设备,提高了发送文件的效率。 ngx_http_core_module
tcp_nodelay tcp_nodelay on; http, server, location Enables or disables the use of the TCP_NODELAY option. The option is enabled only when a connection is transitioned into the keep-alive state. 对keepalive连接是否使用TCP_NODELAY选项,true的话,会禁用Nagle算法,尽快发送数据,而不论包的大小。 ngx_http_core_module
tcp_nopush tcp_nopush off; http, server, location Enables or disables the use of the TCP_NOPUSH socket option on FreeBSD or the TCP_CORK socket option on Linux. The options are enabled only when sendfile is used. Enabling the option allows 1、sending the response header and the beginning of a file in one packet, on Linux and FreeBSD 4.x; 2、sending a file in full packets. 在打开sendfile选项时,确定是否开启FreeBSD系统上的TCP_NOPUSH或Linux系统上的TCP_CORK功能。开启此选项允许在Linux和FreeBSD 4.x上将响应头和正文的开始部分一起发送;一次性发送整个文件。 ngx_http_core_module

client buffer

名称 默认配置 作用域 官方说明 中文解读 模块
keepalive_timeout keepalive_timeout 75s; http, server, location The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections. 一个keepalive连接在闲置超过一定时间后(默认的是75秒),服务器和浏览器都会去关闭这个连接。0表示禁用客户端的keep-alive连接 ngx_http_core_module
client_header_timeout client_header_timeout 60s; http, server Defines a timeout for reading client request header. If a client does not transmit the entire header within this time, the 408 (Request Time-out) error is returned to the client. 客户端与服务器建立连接后将开始接收HTTP头部,在这个过程中,如果在一个时间间隔(超时时间)内没有读取到客户端发来的字节,则认为超时,并向客户端返回408(Request timed out)响应。 ngx_http_core_module
client_body_timeout client_body_timeout 60s; http, server, location Defines a timeout for reading client request body. The timeout is set only for a period between two successive read operations, not for the transmission of the whole request body. If a client does not transmit anything within this time, the 408 (Request Time-out) error is returned to the client. 连续两次读取body的超时时间 ngx_http_core_module
send_timeout send_timeout 60s; http, server, location Sets a timeout for transmitting a response to the client. The timeout is set only between two successive write operations, not for the transmission of the whole response. If the client does not receive anything within this time, the connection is closed. 这个超时时间是发送响应的超时时间,即Nginx服务器向客户端发送了数据包,但客户端一直没有去接收这个数据包。如果某个连接超过send_timeout定义的超时时间,那么Nginx将会关闭这个连接。 ngx_http_core_module
client_header_buffer_size client_header_buffer_size 1k; http, server Sets buffer size for reading client request header. For most requests, a buffer of 1K bytes is enough. However, if a request includes long cookies, or comes from a WAP client, it may not fit into 1K. If a request line or a request header field does not fit into this buffer then larger buffers, configured by the large_client_header_buffers directive, are allocated. 定义了正常情况下Nginx接收用户请求中HTTP header部分(包括HTTP行和HTTP头部)时分配的内存buffer大小。有时,请求中的HTTP header部分可能会超过这个大小,这时large_client_header_buffers定义的buffer将会生效。 ngx_http_core_module
large_client_header_buffers large_client_header_buffers 4 8k; http, server Sets the maximum number and size of buffers used for reading large client request header. A request line cannot exceed the size of one buffer, or the 414 (Request-URI Too Large) error is returned to the client. A request header field cannot exceed the size of one buffer as well, or the 400 (Bad Request) error is returned to the client. Buffers are allocated only on demand. By default, the buffer size is equal to 8K bytes. If after the end of request processing a connection is transitioned into the keep-alive state, these buffers are released. 定义了Nginx接收一个超大HTTP头部请求的buffer个数和每个buffer的大小。如果HTTP请求行(如GET/index HTTP/1.1)的大小超过上面的单个buffer,则返回Request URI too large(414)。请求中一般会有许多header,每一个header的大小也不能超过单个buffer的大小,否则会返回Bad request(400)。当然,请求行和请求头部的总和也不可以超过buffer个数*buffer大小。 ngx_http_core_module
client_max_body_size client_max_body_size 1m; http, server, location Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size. 浏览器在发送含有较大HTTP包体的请求时,其头部会有一个Content-Length字段,client_max_body_size是用来限制Content-Length所示值的大小的。因此,这个限制包体的配置非常有用处,因为不用等Nginx接收完所有的HTTP包体,这有可能消耗很长时间,就可以告诉用户请求过大不被接受。例如,用户试图上传一个10GB的文件,Nginx在收完包头后,发现Content-Length超过client_max_body_size定义的值,就直接发送413(Request Entity Too Large)响应给客户端。 ngx_http_core_module
client_body_temp_path client_body_temp_path client_body_temp; http, server, location Defines a directory for storing temporary files holding client request bodies. Up to three-level subdirectory hierarchy can be used under the specified directory. For example, in the following configuration 用于指定临时缓存文件的存储路径,这里需要注意的是proxy_temp_path和proxy_cache_path指定的路径必须在同一磁盘分区 ngx_http_core_module
client_body_buffer_size client_body_buffer_size 8k或16k; http, server, location Sets buffer size for reading client request body. In case the request body is larger than the buffer, the whole body or only its part is written to a temporary file. By default, buffer size is equal to two memory pages. This is 8K on x86, other 32-bit platforms, and x86-64. It is usually 16K on other 64-bit platforms. 定义了Nginx接收HTTP包体的内存缓冲区大小。也就是说,HTTP包体会先接收到指定的这块缓存中,之后才决定是否写入磁盘。注意 如果用户请求中含有HTTP头部Content-Length,并且其标识的长度小于定义的buffer大小,那么Nginx会自动降低本次请求所使用的内存buffer,以降低内存消耗。 ngx_http_core_module

proxy buffer

名称 默认配置 作用域 官方说明 中文解读 模块
proxy_buffering proxy_buffering on; http, server, location Enables or disables buffering of responses from the proxied server. 是否开启对后端response的缓冲 ngx_http_proxy_module
proxy_connect_timeout proxy_connect_timeout 60s; http, server, location Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds. 指定一个连接到代理服务器的超时时间,单位为秒,需要注意的是这个时间最好不要超过75秒。 ngx_http_proxy_module
proxy_read_timeout proxy_read_timeout 60s; http, server, location Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the proxied server does not transmit anything within this time, the connection is closed. 决定读取后端服务器应答的超时时间,单位为秒,它决定nginx将等待多久时间来取得一个请求的应答。超时时间是指两次连续读操作之间的超时时间。某些情况下代理服务器将花很长的时间来获得页面应答(例如如当接收一个需要很多计算的报表时),可以在不同的location里面设置不同的值。 ngx_http_proxy_module
proxy_send_timeout proxy_send_timeout 60s; http, server, location Sets a timeout for transmitting a request to the proxied server. The timeout is set only between two successive write operations, not for the transmission of the whole request. If the proxied server does not receive anything within this time, the connection is closed. 设置代理服务器转发请求的超时时间,单位为秒,超时时间为两次连续写操作之间的超时时间,如果超过这个时间代理服务器没有数据转发到被代理服务器,nginx将关闭连接 ngx_http_proxy_module
proxy_buffer_size proxy_buffer_size 4k或8k; http, server, location Sets the size of the buffer used for reading the first part of the response received from the proxied server. This part usually contains a small response header. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform. It can be made smaller, however. 缓存response的第一部分,通常是header,默认proxy_buffer_size 被设置成 proxy_buffers 里一个buffer 的大小,当然可以设置更小些。 ngx_http_proxy_module
proxy_buffers proxy_buffers 8 4k或8k; http, server, location Sets the number and size of the buffers used for reading a response from the proxied server, for a single connection. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform. 前面一个是num,后面一个是每个buffer的size.Nginx将会尽可能的读取后端服务器的数据到buffer,直到proxy_buffers设置的所有buffer们被写满或者数据被读取完(EOF),此时Nginx开始向客户端传输数据,会同时传输这一整串buffer们。如果数据很大的话,Nginx会接收并把他们写入到temp_file里去,大小由proxy_max_temp_file_size 控制。 ngx_http_proxy_module
proxy_max_temp_file_size proxy_max_temp_file_size 1024m; http, server, location When buffering of responses from the proxied server is enabled, and the whole response does not fit into the buffers set by the proxy_buffer_size and proxy_buffers directives, a part of the response can be saved to a temporary file. This directive sets the maximum size of the temporary file. The size of data written to the temporary file at a time is set by the proxy_temp_file_write_size directive.The zero value disables buffering of responses to temporary files. 默认情况下proxy_max_temp_file_size值为1024MB,也就是说后端服务器的文件不大于1G都可以缓存到nginx代理硬盘中,如果超过1G,那么文件不缓存,而是直接中转发送给客户端.如果proxy_max_temp_file_size设置为0,表示不使用临时缓存。 ngx_http_proxy_module
proxy_busy_buffers_size proxy_busy_buffers_size 8k或16k; http, server, location When buffering of responses from the proxied server is enabled, limits the total size of buffers that can be busy sending a response to the client while the response is not yet fully read. In the meantime, the rest of the buffers can be used for reading the response and, if needed, buffering part of the response to a temporary file. By default, size is limited by the size of two buffers set by the proxy_buffer_size and proxy_buffers directives. 一旦proxy_buffers设置的buffer被写入,直到buffer里面的数据被完整的传输完(传输到客户端),这个buffer将会一直处 在busy状态,我们不能对这个buffer进行任何别的操作。所有处在busy状态的buffer size加起来不能超过proxy_busy_buffers_size,所以proxy_busy_buffers_size是用来控制同时传输到客户端的buffer数量的。 ngx_http_proxy_module
proxy_temp_file_write_size proxy_temp_file_write_size 8k或16k; http, server, location Limits the size of data written to a temporary file at a time, when buffering of responses from the proxied server to temporary files is enabled. By default, size is limited by two buffers set by the proxy_buffer_size and proxy_buffers directives. The maximum size of a temporary file is set by the proxy_max_temp_file_size directive. 指定每次写temp file的大小 ngx_http_proxy_module
proxy_temp_path proxy_temp_path proxy_temp; http, server, location Defines a directory for storing temporary files with data received from proxied servers. Up to three-level subdirectory hierarchy can be used underneath the specified directory. For example, in the following configuration 指定缓冲代理服务器response的临时文件夹 ngx_http_proxy_module

实例

http {
    include       mime.types;
    default_type  application/octet-stream;

    access_log  /dev/stdout main;

    ##------socket配置----------##
    sendfile        on;
    tcp_nodelay     on;
    #tcp_nopush     on;
    #server_names_hash_max_size 1024; ##设置servers hash表的大小,默认512;如果配置了多个虚拟server,比如24个域名对应一个地址,则需要考虑改此参数


    ##------client端请求设置----------##
    keepalive_timeout  65;
    client_header_timeout  60; ##读取整个header的超时时间
    client_body_timeout    60; ##连续两次读取body的超时时间
    send_timeout           10; ##这个超时时间是发送响应的超时时间,即Nginx服务器向客户端发送了数据包,但客户端一直没有去接收这个数据包。如果某个连接超过send_timeout定义的超时时间,那么Nginx将会关闭这个连接。timeout between two successive write operations for a client receiving a response.

    client_header_buffer_size    128k; ##太小了会报400,Request Header Or Cookie Too Large,默认1kb. 定义了正常情况下Nginx接收用户请求中HTTP header部分(包括HTTP行和HTTP头部)时分配的内存buffer大小。有时,请求中的HTTP header部分可能会超过这个大小,这时large_client_header_buffers定义的buffer将会生效。
    large_client_header_buffers  4 128k; ##定义了Nginx接收一个超大HTTP头部请求的buffer个数和每个buffer的大小。如果HTTP请求行(如GET/index HTTP/1.1)的大小超过单个buffer,则返回\"Request URI too large\"(414)。请求中一般会有许多header,每一个header的大小也不能超过单个buffer的大小,否则会返回\"Bad request\"(400)。当然,请求行和请求头部的总和也不可以超过buffer个数*buffer大小。
    client_max_body_size           20m; ## 客户端请求body的最大大小,超过则报413 Request Entity Too Large,通常用来设置文件上传大小
    ## client_body_temp_path      client_body_temp; ##默认是/usr/local/openresty/nginx/client_body_temp,可以自定义支持多级目录,client_body_temp_path  client_body_temp 1 2;
    client_body_buffer_size      128k;  ##在接收HTTP包体时,如果包体的大小大于client_body_buffer_size,则会以一个递增的整数命名并存放到client_body_temp_path指定的目录中


    ##-------proxy后端的设置---------##
    proxy_buffering on; #默认是on
    proxy_connect_timeout    10; ##控制连接超时,如果连接不上,直接502,Connection refused
    proxy_read_timeout       60; ##控制连续两次读取后端响应的超时,如果后端长时间未响应则504,Connection timed out
    proxy_send_timeout       10; ##控制连续两次向后端发送请求的超时,一般比较少发生
    proxy_buffer_size        128k; ##缓存response的第一部分,通常是header,默认proxy_buffer_size 被设置成 proxy_buffers 里一个buffer 的大小,当然可以设置更小些。
    proxy_buffers 128 128k; ##前面一个是num,后面一个是一个buffer的size,因此总共的buffers大小为=128*128k.Nginx将会尽可能的读取后端服务器的数据到buffer,直到proxy_buffers设置的所有buffer们被写满或者数据被读取完(EOF),此时Nginx开始向客户端传输数据,会同时传输这一整串buffer们。如果数据很大的话,Nginx会接收并把他们写入到temp_file里去,大小由proxy_max_temp_file_size 控制。
    proxy_max_temp_file_size 1024m; ##默认情况下proxy_max_temp_file_size值为1024MB,也就是说后端服务器的文件不大于1G都可以缓存到nginx代理硬盘中,如果超过1G,那么文件不缓存,而是直接中转发送给客户端.如果proxy_max_temp_file_size设置为0,表示不使用临时缓存。
    proxy_busy_buffers_size 128k; ##一旦proxy_buffers设置的buffer被写入,直到buffer里面的数据被完整的传输完(传输到客户端),这个buffer将会一直处 在busy状态,我们不能对这个buffer进行任何别的操作。所有处在busy状态的buffer size加起来不能超过proxy_busy_buffers_size,所以proxy_busy_buffers_size是用来控制同时传输到客户端的buffer数量的。
    proxy_temp_file_write_size 128k; ##指定每次写temp file的大小
    proxy_temp_path proxy_temp; ##默认值为/usr/local/openresty/nginx/proxy_temp

    //......
}

doc

相关文章
相关标签/搜索