序
本文主要解析一下nginx http模塊配置參數(shù)壳鹤。主要分socket相關(guān)參數(shù)柿估,對(duì)clinet請(qǐng)求的buffer參數(shù)以及對(duì)response的buffer參數(shù)铸屉。
socket
名稱 | 默認(rèn)配置 | 作用域 | 官方說明 | 中文解讀 | 模塊 |
---|---|---|---|---|---|
sendfile | sendfile off; | http, server, location, if in location | Enables or disables the use of sendfile(). | 設(shè)置為on可以啟用Linux上的sendfile系統(tǒng)調(diào)用來發(fā)送文件,它減少了內(nèi)核態(tài)與用戶態(tài)之間的兩次內(nèi)存復(fù)制楣嘁,這樣就會(huì)從磁盤中讀取文件后直接在內(nèi)核態(tài)發(fā)送到網(wǎng)卡設(shè)備鹉勒,提高了發(fā)送文件的效率帆锋。 | 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. | 對(duì)keepalive連接是否使用TCP_NODELAY選項(xiàng),true的話禽额,會(huì)禁用Nagle算法锯厢,盡快發(fā)送數(shù)據(jù),而不論包的大小绵疲。 | 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選項(xiàng)時(shí)盔憨,確定是否開啟FreeBSD系統(tǒng)上的TCP_NOPUSH或Linux系統(tǒng)上的TCP_CORK功能。開啟此選項(xiàng)允許在Linux和FreeBSD 4.x上將響應(yīng)頭和正文的開始部分一起發(fā)送讯沈;一次性發(fā)送整個(gè)文件郁岩。 | ngx_http_core_module |
client buffer
名稱 | 默認(rèn)配置 | 作用域 | 官方說明 | 中文解讀 | 模塊 |
---|---|---|---|---|---|
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. | 一個(gè)keepalive連接在閑置超過一定時(shí)間后(默認(rèn)的是75秒)婿奔,服務(wù)器和瀏覽器都會(huì)去關(guān)閉這個(gè)連接。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. | 客戶端與服務(wù)器建立連接后將開始接收HTTP頭部问慎,在這個(gè)過程中萍摊,如果在一個(gè)時(shí)間間隔(超時(shí)時(shí)間)內(nèi)沒有讀取到客戶端發(fā)來的字節(jié),則認(rèn)為超時(shí)如叼,并向客戶端返回408(Request timed out)響應(yīng)冰木。 | 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. | 連續(xù)兩次讀取body的超時(shí)時(shí)間 | 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. | 這個(gè)超時(shí)時(shí)間是發(fā)送響應(yīng)的超時(shí)時(shí)間,即Nginx服務(wù)器向客戶端發(fā)送了數(shù)據(jù)包笼恰,但客戶端一直沒有去接收這個(gè)數(shù)據(jù)包踊沸。如果某個(gè)連接超過send_timeout定義的超時(shí)時(shí)間,那么Nginx將會(huì)關(guān)閉這個(gè)連接社证。 | 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接收用戶請(qǐng)求中HTTP header部分(包括HTTP行和HTTP頭部)時(shí)分配的內(nèi)存buffer大小逼龟。有時(shí),請(qǐng)求中的HTTP header部分可能會(huì)超過這個(gè)大小追葡,這時(shí)large_client_header_buffers定義的buffer將會(huì)生效腺律。 | 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接收一個(gè)超大HTTP頭部請(qǐng)求的buffer個(gè)數(shù)和每個(gè)buffer的大小。如果HTTP請(qǐng)求行(如GET/index HTTP/1.1)的大小超過上面的單個(gè)buffer宜肉,則返回Request URI too large(414)匀钧。請(qǐng)求中一般會(huì)有許多header,每一個(gè)header的大小也不能超過單個(gè)buffer的大小谬返,否則會(huì)返回Bad request(400)榴捡。當(dāng)然,請(qǐng)求行和請(qǐng)求頭部的總和也不可以超過buffer個(gè)數(shù)*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. | 瀏覽器在發(fā)送含有較大HTTP包體的請(qǐng)求時(shí)吊圾,其頭部會(huì)有一個(gè)Content-Length字段,client_max_body_size是用來限制Content-Length所示值的大小的翰蠢。因此项乒,這個(gè)限制包體的配置非常有用處,因?yàn)椴挥玫萅ginx接收完所有的HTTP包體梁沧,這有可能消耗很長時(shí)間檀何,就可以告訴用戶請(qǐng)求過大不被接受。例如廷支,用戶試圖上傳一個(gè)10GB的文件频鉴,Nginx在收完包頭后,發(fā)現(xiàn)Content-Length超過client_max_body_size定義的值恋拍,就直接發(fā)送413(Request Entity Too Large)響應(yīng)給客戶端垛孔。 | 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 | 用于指定臨時(shí)緩存文件的存儲(chǔ)路徑,這里需要注意的是proxy_temp_path和proxy_cache_path指定的路徑必須在同一磁盤分區(qū) | 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包體的內(nèi)存緩沖區(qū)大小施敢。也就是說周荐,HTTP包體會(huì)先接收到指定的這塊緩存中狭莱,之后才決定是否寫入磁盤。注意 如果用戶請(qǐng)求中含有HTTP頭部Content-Length概作,并且其標(biāo)識(shí)的長度小于定義的buffer大小腋妙,那么Nginx會(huì)自動(dòng)降低本次請(qǐng)求所使用的內(nèi)存buffer,以降低內(nèi)存消耗讯榕。 | ngx_http_core_module |
proxy buffer
名稱 | 默認(rèn)配置 | 作用域 | 官方說明 | 中文解讀 | 模塊 |
---|---|---|---|---|---|
proxy_buffering | proxy_buffering on; | http, server, location | Enables or disables buffering of responses from the proxied server. | 是否開啟對(duì)后端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. | 指定一個(gè)連接到代理服務(wù)器的超時(shí)時(shí)間骤素,單位為秒,需要注意的是這個(gè)時(shí)間最好不要超過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. | 決定讀取后端服務(wù)器應(yīng)答的超時(shí)時(shí)間谆甜,單位為秒,它決定nginx將等待多久時(shí)間來取得一個(gè)請(qǐng)求的應(yīng)答集绰。超時(shí)時(shí)間是指兩次連續(xù)讀操作之間的超時(shí)時(shí)間规辱。某些情況下代理服務(wù)器將花很長的時(shí)間來獲得頁面應(yīng)答(例如如當(dāng)接收一個(gè)需要很多計(jì)算的報(bào)表時(shí)),可以在不同的location里面設(shè)置不同的值栽燕。 | 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. | 設(shè)置代理服務(wù)器轉(zhuǎn)發(fā)請(qǐng)求的超時(shí)時(shí)間罕袋,單位為秒,超時(shí)時(shí)間為兩次連續(xù)寫操作之間的超時(shí)時(shí)間碍岔,如果超過這個(gè)時(shí)間代理服務(wù)器沒有數(shù)據(jù)轉(zhuǎn)發(fā)到被代理服務(wù)器浴讯,nginx將關(guān)閉連接 | 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,默認(rèn)proxy_buffer_size 被設(shè)置成 proxy_buffers 里一個(gè)buffer 的大小,當(dāng)然可以設(shè)置更小些蔼啦。 | 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. | 前面一個(gè)是num,后面一個(gè)是每個(gè)buffer的size.Nginx將會(huì)盡可能的讀取后端服務(wù)器的數(shù)據(jù)到buffer榆纽,直到proxy_buffers設(shè)置的所有buffer們被寫滿或者數(shù)據(jù)被讀取完(EOF),此時(shí)Nginx開始向客戶端傳輸數(shù)據(jù)捏肢,會(huì)同時(shí)傳輸這一整串buffer們奈籽。如果數(shù)據(jù)很大的話,Nginx會(huì)接收并把他們寫入到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. | 默認(rèn)情況下proxy_max_temp_file_size值為1024MB,也就是說后端服務(wù)器的文件不大于1G都可以緩存到nginx代理硬盤中,如果超過1G辩棒,那么文件不緩存狼忱,而是直接中轉(zhuǎn)發(fā)送給客戶端.如果proxy_max_temp_file_size設(shè)置為0,表示不使用臨時(shí)緩存一睁。 | 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設(shè)置的buffer被寫入钻弄,直到buffer里面的數(shù)據(jù)被完整的傳輸完(傳輸?shù)娇蛻舳耍@個(gè)buffer將會(huì)一直處 在busy狀態(tài)者吁,我們不能對(duì)這個(gè)buffer進(jìn)行任何別的操作窘俺。所有處在busy狀態(tài)的buffer size加起來不能超過proxy_busy_buffers_size,所以proxy_busy_buffers_size是用來控制同時(shí)傳輸?shù)娇蛻舳说腷uffer數(shù)量的砚偶。 | 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 | 指定緩沖代理服務(wù)器response的臨時(shí)文件夾 | ngx_http_proxy_module |
實(shí)例
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; ##設(shè)置servers hash表的大小,默認(rèn)512;如果配置了多個(gè)虛擬server批销,比如24個(gè)域名對(duì)應(yīng)一個(gè)地址,則需要考慮改此參數(shù)
##------client端請(qǐng)求設(shè)置----------##
keepalive_timeout 65;
client_header_timeout 60; ##讀取整個(gè)header的超時(shí)時(shí)間
client_body_timeout 60; ##連續(xù)兩次讀取body的超時(shí)時(shí)間
send_timeout 10; ##這個(gè)超時(shí)時(shí)間是發(fā)送響應(yīng)的超時(shí)時(shí)間,即Nginx服務(wù)器向客戶端發(fā)送了數(shù)據(jù)包染坯,但客戶端一直沒有去接收這個(gè)數(shù)據(jù)包均芽。如果某個(gè)連接超過send_timeout定義的超時(shí)時(shí)間,那么Nginx將會(huì)關(guān)閉這個(gè)連接单鹿。timeout between two successive write operations for a client receiving a response.
client_header_buffer_size 128k; ##太小了會(huì)報(bào)400,Request Header Or Cookie Too Large,默認(rèn)1kb. 定義了正常情況下Nginx接收用戶請(qǐng)求中HTTP header部分(包括HTTP行和HTTP頭部)時(shí)分配的內(nèi)存buffer大小掀宋。有時(shí),請(qǐng)求中的HTTP header部分可能會(huì)超過這個(gè)大小仲锄,這時(shí)large_client_header_buffers定義的buffer將會(huì)生效劲妙。
large_client_header_buffers 4 128k; ##定義了Nginx接收一個(gè)超大HTTP頭部請(qǐng)求的buffer個(gè)數(shù)和每個(gè)buffer的大小。如果HTTP請(qǐng)求行(如GET/index HTTP/1.1)的大小超過單個(gè)buffer儒喊,則返回\"Request URI too large\"(414)镣奋。請(qǐng)求中一般會(huì)有許多header,每一個(gè)header的大小也不能超過單個(gè)buffer的大小怀愧,否則會(huì)返回\"Bad request\"(400)侨颈。當(dāng)然,請(qǐng)求行和請(qǐng)求頭部的總和也不可以超過buffer個(gè)數(shù)*buffer大小芯义。
client_max_body_size 20m; ## 客戶端請(qǐng)求body的最大大小,超過則報(bào)413 Request Entity Too Large,通常用來設(shè)置文件上傳大小
## client_body_temp_path client_body_temp; ##默認(rèn)是/usr/local/openresty/nginx/client_body_temp,可以自定義支持多級(jí)目錄,client_body_temp_path client_body_temp 1 2;
client_body_buffer_size 128k; ##在接收HTTP包體時(shí)哈垢,如果包體的大小大于client_body_buffer_size,則會(huì)以一個(gè)遞增的整數(shù)命名并存放到client_body_temp_path指定的目錄中
##-------proxy后端的設(shè)置---------##
proxy_buffering on; #默認(rèn)是on
proxy_connect_timeout 10; ##控制連接超時(shí),如果連接不上,直接502,Connection refused
proxy_read_timeout 60; ##控制連續(xù)兩次讀取后端響應(yīng)的超時(shí),如果后端長時(shí)間未響應(yīng)則504,Connection timed out
proxy_send_timeout 10; ##控制連續(xù)兩次向后端發(fā)送請(qǐng)求的超時(shí),一般比較少發(fā)生
proxy_buffer_size 128k; ##緩存response的第一部分,通常是header,默認(rèn)proxy_buffer_size 被設(shè)置成 proxy_buffers 里一個(gè)buffer 的大小扛拨,當(dāng)然可以設(shè)置更小些耘分。
proxy_buffers 128 128k; ##前面一個(gè)是num,后面一個(gè)是一個(gè)buffer的size,因此總共的buffers大小為=128*128k.Nginx將會(huì)盡可能的讀取后端服務(wù)器的數(shù)據(jù)到buffer,直到proxy_buffers設(shè)置的所有buffer們被寫滿或者數(shù)據(jù)被讀取完(EOF)绑警,此時(shí)Nginx開始向客戶端傳輸數(shù)據(jù)求泰,會(huì)同時(shí)傳輸這一整串buffer們。如果數(shù)據(jù)很大的話计盒,Nginx會(huì)接收并把他們寫入到temp_file里去拜秧,大小由proxy_max_temp_file_size 控制。
proxy_max_temp_file_size 1024m; ##默認(rèn)情況下proxy_max_temp_file_size值為1024MB,也就是說后端服務(wù)器的文件不大于1G都可以緩存到nginx代理硬盤中章郁,如果超過1G枉氮,那么文件不緩存,而是直接中轉(zhuǎn)發(fā)送給客戶端.如果proxy_max_temp_file_size設(shè)置為0暖庄,表示不使用臨時(shí)緩存聊替。
proxy_busy_buffers_size 128k; ##一旦proxy_buffers設(shè)置的buffer被寫入,直到buffer里面的數(shù)據(jù)被完整的傳輸完(傳輸?shù)娇蛻舳耍┡嗬@個(gè)buffer將會(huì)一直處 在busy狀態(tài)惹悄,我們不能對(duì)這個(gè)buffer進(jìn)行任何別的操作。所有處在busy狀態(tài)的buffer size加起來不能超過proxy_busy_buffers_size肩钠,所以proxy_busy_buffers_size是用來控制同時(shí)傳輸?shù)娇蛻舳说腷uffer數(shù)量的泣港。
proxy_temp_file_write_size 128k; ##指定每次寫temp file的大小
proxy_temp_path proxy_temp; ##默認(rèn)值為/usr/local/openresty/nginx/proxy_temp
//......
}