Interpretation of nginx http module configuration parameters

  nginx

Order

This article mainly analyzes the nginx http module configuration parameters. Mainly divided into socket related parameters, buffer parameters for clinet request and buffer parameters for response.

socket

Name Default configuration action scope Official statement Interpretation in Chinese module
sendfile sendfile off; http, server, location, if in location Enables or disables the use of sendfile(). Set to on to enable sendfile system call on Linux to send files, which reduces two memory copies between kernel state and user state, thus reading files from disk and sending them directly to network card device in kernel state, improving the efficiency of sending files. 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. Whether TCP_NODELAY option is used for keepalive connection, if true, Nagle algorithm will be disabled and data will be sent as soon as possible, regardless of packet size. 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 sendfi le 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. When the sendfile option is turned on, determine whether to turn on TCP_NOPUSH on FreeBSD system or TCP_CORK function on Linux system. Turn on this option to allow the response header and the beginning of the body to be sent together on Linux and FreeBSD 4.x; Send the entire file at once. ngx_http_core_module

client buffer

Name Default configuration action scope Official statement Interpretation in Chinese module
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. After a keepalive connection has been idle for more than a certain period of time (the default is 75 seconds), both the server and the browser will close the connection. 0 indicates that keep-alive connection for the client is disabled 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. After the client establishes a connection with the server, it will begin to receive HTTP headers. In this process, if no bytes sent by the client are read within a time interval (timeout time), it is deemed to have timed out and a 408(Request timed out) response is returned to the client. 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. Timeout for reading body twice in a row 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. This timeout is the timeout for sending a response, that is, the Nginx server sent a packet to the client, but the client never received the packet. If a connection exceeds the timeout defined by send_timeout, Nginx will close the connection. 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. Defines the memory buffer size allocated when Nginx receives the HTTP header part (including HTTP line and HTTP header) of the user request under normal circumstances. Sometimes, the HTTP header part of a request may exceed this size, and the buffer defined by large_client_header_buffers will take effect. 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. Defines the number of buffers and the size of each buffer that Nginx receives a request for an oversized HTTP header. If the size of the HTTP request line (such as GET/index HTTP/1.1) exceeds the above single buffer, Request URI too large(414) is returned. There are usually many headers in the request, and the size of each header cannot exceed the size of a single buffer, otherwise Bad request(400) will be returned. Of course, the sum of the request line and the request header cannot exceed the number of buffers *buffer size. 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. When a browser sends a request containing a large HTTP packet, its header will have a Content-Length field. client_max_body_size is used to limit the size of the value shown in Content-Length. Therefore, the configuration of this restricted bag is very useful, because it may take a long time to tell the user that the request is too large to be accepted without waiting for Nginx to receive all HTTP bags. For example, when a user tries to upload a 10GB file, after receiving the header, Nginx finds that the Content-Length exceeds the value defined by client_max_body_size, and directly sends 413(Request Entity Too Large) response to the client. 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 It is used to specify the storage path of temporary cache files. Note here that the paths specified by proxy_temp_path and proxy_cache_path must be in the same disk partition. ngx_http_core_module
client_body_buffer_size Client_body_buffer_size 8k or 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. Defines the memory buffer size for Nginx to receive HTTP inclusions. In other words, HTTP packets will receive the specified cache before deciding whether to write to disk. Note: If the user request contains HTTP header Content-Length and the length of its identifier is less than the defined buffer size, Nginx will automatically reduce the memory buffer used in this request to reduce memory consumption. ngx_http_core_module

proxy buffer

Name Default configuration action scope Official statement Interpretation in Chinese module
proxy_buffering proxy_buffering on; http, server, location Enables or disables buffering of responses from the proxied server. Whether to turn on buffering for backend 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. Specify a timeout in seconds for connecting to the proxy server. It should be noted that this time should not exceed 75 seconds. 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. Determines the timeout in seconds for reading back-end server responses, which determines how long nginx will wait to obtain a response to a request. The timeout period refers to the timeout period between two consecutive read operations. In some cases, the proxy server will take a long time to obtain the page response (for example, when receiving a report that requires a lot of calculations), and different values can be set in different 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. Set the timeout time for proxy server to forward requests in seconds. The timeout time is the timeout time between two consecutive write operations. If the proxy server has no data to forward to the proxy server beyond this time, nginx will close the connection. ngx_http_proxy_module
proxy_buffer_size Proxy_buffer_size 4k or 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 respon se 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. The first part of the cache response is usually the header. The default proxy_buffer_size is set to the size of a buffer in proxy_buffers, which can be set smaller of course. ngx_http_proxy_module
proxy_buffers Proxy_buffers 8 4k or 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. The first one is num, and the second one is the size.Nginx of each buffer will read the data of the back-end server to the buffer as much as possible until all buffers set by proxy_buffers are full or the data is read out (EOF). At this time, Nginx starts to transmit the data to the client and will transmit the whole string of buffers at the same time. If the data is large, Nginx will receive and write them into temp_file, and the size is controlled by 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 t emporary file at a time is set by the proxy_temp_file_write_size directive.The zero value disables buffering of responses to temporary files. By default, the proxy_max_temp_file_size value is 1024MB, which means that the files of the back-end server can be cached in the nginx proxy hard disk if they are not larger than 1G. If they are larger than 1G, the files are not cached but forwarded directly to the client. If proxy_max_temp_file_size is set to 0, it means that temporary cache is not used. ngx_http_proxy_module
proxy_busy_buffers_size Proxy_busy_buffers_size 8k or 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. Once the buffer set by proxy_buffers is written until the data in the buffer is completely transferred (to the client), the buffer will remain in the busy state, and we cannot perform any other operations on the buffer. All buffers in busy state cannot exceed proxy_busy_buffers_size, so proxy_busy_buffers_size is used to control the number of buffers transmitted to clients simultaneously. ngx_http_proxy_module
proxy_temp_file_write_size Proxy_temp_file_write_size 8k or 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. Specifies the size of temp file for each write. 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 underneat h the specified directory. For example, in the following configuration Specifies the temporary folder for buffering the proxy server response ngx_http_proxy_module

Example

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