2012-11-09 68 views
2

我有在RoR的應用程序,我測試它在apache2上傳文件> 1 GB,它的工作。但我必須使用nginx。我有100%的上傳之後該錯誤nginx的服務器上:Nginx + Unicorn不允許上傳文件> 400mb

2012年11月9日17時17分01秒[錯誤] 1436#0:* 12上游過早關閉 連接,同時從上游讀取響應頭,客戶: 134.19.136.32,服務器:my_domain,請求:「POST /附件HTTP/1.1」,上游: 「http:// unix:/tmp-sock/unicorn.my_domain.sock:/附件」,主機: 「 my_domain「,referrer:」http:// my_domain /「

我覺得問題是與獨角獸設置,但我不知道究竟在哪裏。 BTW:Everthing只能在Firefox中使用。

我的配置文件:

#nginx main config 
user www-data; 
worker_processes 5; 
pid /run/nginx.pid; 

events { 
    worker_connections 768; 
    accept_mutex off; 
    # multi_accept on; 
} 

http { 

    ## 
    # Basic Settings 
    ## 

    sendfile on; 
    tcp_nopush on; 
    tcp_nodelay on; 
    keepalive_timeout 65; 
    types_hash_max_size 2048; 
    # server_tokens off; 

    # server_names_hash_bucket_size 64; 
    # server_name_in_redirect off; 

    include /etc/nginx/mime.types; 
    default_type application/octet-stream; 

    ## 
    # Logging Settings 
    ## 

    access_log /var/log/nginx/access.log; 
    error_log /var/log/nginx/error.log; 

    ## 
    # Gzip Settings 
    ## 

    gzip on; 
    gzip_disable "msie6"; 

    gzip_vary on; 
    gzip_proxied any; 
    gzip_comp_level 6; 
    gzip_buffers 16 8k; 
    gzip_http_version 1.1; 
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 

    ## 
    # nginx-naxsi config 
    ## 
    # Uncomment it if you installed nginx-naxsi 
    ## 

    #include /etc/nginx/naxsi_core.rules; 

    ## 
    # nginx-passenger config 
    ## 
    # Uncomment it if you installed nginx-passenger 
    ## 

    #passenger_root /usr; 
    #passenger_ruby /usr/bin/ruby; 

    ## 
    # Virtual Host Configs 
    ## 
    output_buffers 1 2m; 
    send_timeout 50s; 
    client_body_temp_path /citishare/datastore0; 
    client_max_body_size 204800m; 
    reset_timedout_connection on; 

    include /etc/nginx/conf.d/*.conf; 
    include /etc/nginx/sites-enabled/*; 
} 

#site config 
upstream unicorn { 
    server unix:/tmp-sock/unicorn.citidrive.sock fail_timeout=10000; 
} 

server { 
    listen 80 default deferred; 
    server_name citidrive.citicom.sk; 
    root /home/deployer/apps/citidrive/current/public; 

    proxy_buffering on; 
    proxy_buffer_size 8M; 
    proxy_buffers 2048 8M; 

    location ^~ /assets/ { 
    gzip_static on; 
    expires max; 
    add_header Cache-Control public; 
    } 

    proxy_set_header X-Accel-Mapping  /home/deployer/apps/citidrive/current/public/system/=/private_files/; 
    proxy_set_header X-Accel-Limit-Rate off; 
    location /private_files/ { 
      internal; 
      alias /home/deployer/apps/citidrive/current/public/system/; 
     } 
    try_files $uri/index.html $uri @unicorn; 
    location @unicorn { 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 
     proxy_pass http://unicorn; 
     proxy_read_timeout 500; 
     } 
    error_page 500 502 503 504 /500.html; 
    client_max_body_size 4G; 
    proxy_max_temp_file_size 3072m; 
    keepalive_timeout 80; 
    client_header_timeout 3m; 
    client_body_timeout 3m; 
    send_timeout 3m; 
} 

#unicorn.rb 
root = "/home/deployer/apps/citidrive/current" 
working_directory root 
pid "#{root}/tmp/pids/unicorn.pid" 
stderr_path "#{root}/log/unicorn.log" 
stdout_path "#{root}/log/unicorn.log" 

listen "/tmp-sock/unicorn.citidrive.sock" 
worker_processes 5 
timeout 80 

#unicorn_init.sh 
#!/bin/sh 
### BEGIN INIT INFO 
# Provides:   unicorn 
# Required-Start: $remote_fs $syslog 
# Required-Stop:  $remote_fs $syslog 
# Default-Start:  2 3 4 5 
# Default-Stop:  0 1 6 
# Short-Description: Manage unicorn server 
# Description:  Start, stop, restart unicorn server for a specific application. 
### END INIT INFO 
set -e 

# Feel free to change any of the following variables for your app: 
TIMEOUT=${TIMEOUT-60} 
APP_ROOT=/home/deployer/apps/citidrive/current 
PID=$APP_ROOT/tmp/pids/unicorn.pid 
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production" 
AS_USER=deployer 
set -u 

OLD_PIN="$PID.oldbin" 

sig() { 
    test -s "$PID" && kill -$1 `cat $PID` 
} 

oldsig() { 
    test -s $OLD_PIN && kill -$1 `cat $OLD_PIN` 
} 

run() { 
    if [ "$(id -un)" = "$AS_USER" ]; then 
    eval $1 
    else 
    su -c "$1" - $AS_USER 
    fi 
} 

case "$1" in 
start) 
    sig 0 && echo >&2 "Already running" && exit 0 
    run "$CMD" 
    ;; 
stop) 
    sig QUIT && exit 0 
    echo >&2 "Not running" 
    ;; 
force-stop) 
    sig TERM && exit 0 
    echo >&2 "Not running" 
    ;; 
restart|reload) 
    sig HUP && echo reloaded OK && exit 0 
    echo >&2 "Couldn't reload, starting '$CMD' instead" 
    run "$CMD" 
    ;; 
upgrade) 
    if sig USR2 && sleep 2 && sig 0 && oldsig QUIT 
    then 
    n=$TIMEOUT 
    while test -s $OLD_PIN && test $n -ge 0 
    do 
     printf '.' && sleep 1 && n=$(($n - 1)) 
    done 
    echo 

    if test $n -lt 0 && test -s $OLD_PIN 
    then 
     echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds" 
     exit 1 
    fi 
    exit 0 
    fi 
    echo >&2 "Couldn't upgrade, starting '$CMD' instead" 
    run "$CMD" 
    ;; 
reopen-logs) 
    sig USR1 
    ;; 
*) 
    echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>" 
    exit 1 
    ;; 
esac 
+0

你在'unicorn.rb'中打了80秒的超時時間嗎?也就是說,即使上傳似乎達到100%,在80秒後實際上傳到服務器的數量是多少? 我意識到這並不能解釋你在Firefox中看到什麼 - 只是問。 – jefflunt

+0

你也可以檢查nginx'upstream'指令並確保它按照你想要的方式設置。看起來你的'fail_timeout'是10秒?我並不是非常熟悉應該完成的工作,但請仔細檢查http://wiki.nginx.org/HttpUpstreamModule – jefflunt

+0

什麼是獨角獸日誌? – VBart

回答

0

我通過在T增加keepalive_timeout解決的問題他對Nginx的配置。也許這不是一個合適的解決方案,或者你可以根據位置和請求類型來增加它。對於那個Firefox部分,Firefox等待比Chrome更長的響應時間。

1

直接發送一個巨大的文件到你的rails應用程序並不是一個上傳大文件的好方法。你可以使用nginx upload module來處理使用獨角獸+ nginx的大文件上傳。這樣,nginx實際上處理文件上傳,而不是將整個文件作爲多部分形式的數據傳遞給rails,而是將由nginx上傳的本地文件路徑發送到unicorn rails服務器,所有rails都將文件從OS的tmp路徑到您定義的路徑。 Here我配置了nginx和rails來用nginx上傳模塊做上傳作業。現在有更好的解決方案來處理上傳,如TUS協議。