nginx http模塊配置參數解讀

本文主要解析一下nginx http模塊配置參數。主要分socket相關參數,對clinet請求的buffer參數以及對response的buffer參數。html

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 一、sending the response header and the beginning of a file in one packet, on Linux and FreeBSD 4.x; 二、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

相關文章
相關標籤/搜索