現象說明:
在服務器上部署了一套后台環境,使用的是nginx反向代理tomcat架構,在后台里上傳一個70M的視頻文件,上傳到一半就失效了!
原因:nginx配置里限制了上傳文件的大小
client_max_body_size:這個參數的設置限制了上傳文件的大小,可以在http、server、location三個區域里配置
[root@dev-huanqiu ~]# cat /Data/app/nginx/conf/nginx.conf
.......
.......
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
#######
## http setting
#######
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 100; #這個參數表示http連接超時時間,默認是65s。要是上傳文件比較大,在規定時間內沒有上傳完成,就會自動斷開連接!所以適當調大這個時間。
fastcgi_connect_timeout 6000;
fastcgi_send_timeout 6000;
fastcgi_read_timeout 6000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
##
client_header_timeout 120s; #調大點
client_body_timeout 120s; #調大點
client_max_body_size 100m; #主要是這個參數,限制了上傳文件大大小
client_body_buffer_size 256k;
## support more than 15 test environments
server_names_hash_max_size 512;
server_names_hash_bucket_size 128;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary on;
[root@dev-huanqiu ~]# cat /Data/app/nginx/conf/vhosts/admin.wangshibo.conf
server {
listen 80;
server_name admin.wangshibo.com;
#if ($http_x_forwarded_for !~ ^(14.165.97.54|123.110.186.128|123.110.186.68)) {
# rewrite ^.*$ /maintence.php last;
#}
access_log /var/log/wangshibo.log main;
location / {
proxy_pass http://127.0.0.1:8484/;
proxy_connect_timeout 300; #這三個超時時間適量調大點
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_set_header X-Real-IP $remote_addr; # 獲取客戶端真實IP
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 獲取代理者的真實ip
proxy_set_header X-Forwarded-Scheme $scheme; # 解決getScheme,isSecure,sendRedirect
proxy_buffer_size 32k;
proxy_buffers 32 256k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
}
location /static/video {
root /Data/app/tomcat-7-admin-wls/static/video;
}
} ##end server
另外,tomcat的server.xml配置文件中的connectionTimeout超時時間也可以適當調大點,默認是20000,可以改成60000.
Nginx代理請求超時時間
可以參考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout
====================================================================================
注意一點:
keepalive_timeout這個是nginx里關於http連接超時的一個設置,功能是使客戶端到服務器端的連接在設定的時間內持續有效,當出現對服務器的后繼請求時,該功能避免了建立或者重新建立連接。切記這個參數也不能設置過大!
因為客戶端接口訪問其實是一個比較快速的過程,訪問完成了就不需要繼續使用http連接了,如果將該參數值設置過大,就會導致接口訪問完成后http連接並沒有被釋放掉,所以導致連接數越來越大,最終nginx崩潰!
如果http連接數過大時,超過了nginx里對於連接數的配置,比如“worker_connections 65535”,那么對應的nginx報錯日志里會有信息:(socket() failed (24: Too many open files) while connecting to upstream)時不時的出現。
所以,要嚴格控制keepalive_timeout超時時間的設置,調大點的話,就會導致許多無效的http連接占據着nginx的連接數。
總之:
keepalive_timeout參數,對於提供靜態內容的網站來說,這個功能通常是很有用的;
但是對於負擔較重的網站來說,存在一個問題:雖然為客戶保留打開的連接有一定的好處,但它同樣影響了性能,因為在處理暫停期間,本來可以釋放的資源仍舊被占用。當Web服務器和應用服務器在同一台機器上運行時,該功能對資源利用的影響尤其突出。
優點是:在請求大量小文件的時候,長連接的有效使用可以減少重建連接的開銷.
缺點是:當長連接時間過長,比如60s,即使是瀏覽器沒有任何請求,服務器仍然會維護着該瀏覽器的連接,一旦用戶很多,對apache而言,就是需要維護大量的空閑進程.而對使用線程的輕量級web服務器如nginx,會由於超時時間過長而使資源無效占有而引發的損失,已超過了由於重復連接而造成的損失..
=======================================================================================
另外補充下php配置里對上傳大小的限制:
打開php.ini 文件中,主要修改以下幾個參數
;This sets the maximum amount of memory in bytes that a script is allowed to allocate
memory_limit = 32M
;The maximum size of an uploaded file.
upload_max_filesize = 8M
;Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize
post_max_size = 16M