Unicorn: Start, Stop and Restart Centos 6 - ruby-on-rails

I've just setup nginx and unicorn. I start unicorn like this:
unicorn_rails -c /var/www/Web/config/unicorn.rb -D
I've tried the various commands for stopping the unicorn but none of them work. I usually just restart the server and start unicorn again but this is very annoying.
EDIT
unicorn.rb file (/var/www/Web/config/):
# Set the working application directory
# working_directory "/path/to/your/app"
working_directory "/var/www/Web"
# Unicorn PID file location
# pid "/path/to/pids/unicorn.pid"
pid "/var/www/Web/pids/unicorn.pid"
# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "/var/www/Web/log/unicorn.log"
stdout_path "/var/www/Web/log/unicorn.log"
# Unicorn socket
listen "/tmp/unicorn.Web.sock"
listen "/tmp/unicorn.Web.sock"
# Number of processes
# worker_processes 4
worker_processes 2
# Time-out
timeout 30
default.conf (/etc/nginx/conf.d/):
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:/tmp/unicorn.Web.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost;
# Application root, as defined previously
root /root/Web/public;
try_files $uri/index.html $uri #app;
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}

This is what I do:
$ for i in `ps awx | grep unico | grep -v grep | awk '{print $1;}'`; do kill -9 $i; done && unicorn_rails -c /var/www/Web/config/unicorn.rb -D
If you don't want to have all this line, script it, like this:
/var/www/Web/unicorn_restart.sh:
#!/bin/bash
for i in `ps awx | grep unicorn | grep -v grep | awk '{print $1;}'`; do
kill $i
done
unicorn_rails -c /var/www/Web/config/unicorn.rb -D
and then:
$ chmod +x /var/www/Web/unicorn_restart.sh
summon it each time calling:
$ /var/www/Web/unicorn_restart.sh

Related

Rails + Nginx + Unicorn - unicorn.sock file is not created when deploying

I'm getting a "We're sorry, but something went wrong" screen upon attempting to deploy. It was previously working fine, however when I tried to update it, I ran into this issue. This is my first time deploying anything, and I'm still not exactly sure what I'm doing so I would really appreciate some input as to what I'm doing incorrectly.
I get the following error from unicorn.log
I, [2018-10-01T19:54:53.470419 #4905] INFO -- : unlinking existing
socket=/home/deploy/production/appName/tmp/sockets
/bcrypt_unicorn.todo.sock
I, [2018-10-01T19:54:53.470635 #4905] INFO -- : listening on addr=/home/deploy/production/appName/tmp/sockets/bcrypt_unicorn.todo.sock fd=10
I, [2018-10-01T19:54:53.470737 #4905] INFO -- : worker=0 spawning...
I, [2018-10-01T19:54:53.471045 #4905] INFO -- : worker=1 spawning...
I, [2018-10-01T19:54:53.471397 #4905] INFO -- : master process ready
I, [2018-10-01T19:54:53.476589 #4908] INFO -- : worker=0 spawned pid=4908
I, [2018-10-01T19:54:53.476714 #4908] INFO -- : Refreshing Gem list
I, [2018-10-01T19:54:53.477787 #4910] INFO -- : worker=1 spawned pid=4910
I, [2018-10-01T19:54:53.477910 #4910] INFO -- : Refreshing Gem list
I, [2018-10-01T19:54:59.740522 #4908] INFO -- : worker=0 ready
I, [2018-10-01T19:54:59.744825 #4910] INFO -- : worker=1 ready
and the following error from /var/log/nginx/error.log:
2018/10/01 20:00:41 [crit] 5067#5067: *2 connect() to unix:/home/deploy
/production/appName/tmp/sockets/bcrypto_unicorn.todo.sock failed
(2: No such file or directory) while connecting to upstream, client:
77.75.77.32, server: , request: "GET /genres/gaming HTTP/1.1", upstream:
"http://unix:/home/deploy/production/appName/tmp/sockets
/bcrypto_unicorn.todo.sock:/genres/gaming", host: "appName.com"
I restarted Nginx with
sudo service nginx restart
Reload the updated configuration
sudo nginx -s reload
Then stopped the running Unicorn process
ps aux | grep "unicorn master"
kill -9 PID
Then pulled updated code to deploy
git status
git stash save -u quick-fix
git pull origin master
git stash apply
Then migrated the db
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake assets:precompile
Then finally restarted Unicorn
bundle exec unicorn -E production -c config/unicorn.rb -D
My unicorn.rb file is as follows:
app_path = "/home/deploy/production/appName"
working_directory app_path
pid app_path + "/tmp/pids/unicorn.pid"
stderr_path app_path + "/log/unicorn.log"
stdout_path app_path + "/log/production.log"
listen app_path + '/tmp/sockets/bcrypt_unicorn.todo.sock'
worker_processes 2
timeout 65
appName/config/nginx.conf
upstream unicorn {
server unix:/tmp/sockets/bcrypto_unicorn.todo.sock fail_timeout=0;
}
server{
listen 80 default deferred;
root /home/deploy/production/appName/public;
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;
}
error_page 403 404 /404.html;
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
and /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
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;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# 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/javascript text/xml applicati$
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites-enabled/bcrypto
upstream bcrypto_unicorn {
server unix:/home/deploy/production/appName/tmp/sockets/bcrypto_unicorn.todo.sock fa$
}
server {
listen 80 default deferred;
root /home/deploy/production/appName/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #bcrypto_unicorn;
location #bcrypto_unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://bcrypto_unicorn;
}
error_page 422 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 60;
}
Can someone please point me in the direction of what I'm doing incorrectly here?
Firstly, check your tmp/sockets folder to see whether its exists or not. If not, create the folder with following command
mkdir /home/deploy/production/appName/tmp/sockets
Then, change your socket to the same path with nginx by modify your unicorn.rb
listen app_path + '/tmp/sockets/bcrypt_unicorn.todo.sock'
Lastly, restart unicorn
bundle exec unicorn -E production -c config/unicorn.rb -D
The problem is that Nginx is looking for the socket in a different place that Unicorn has set it.
Nginx is trying to find the socket at:
/home/deploy/production/appName/tmp/sockets/byc2-master_unicorn.todo.sock while the Unicorn config is setting it to /tmp/appName_unicorn.todo.sock
To solve the problem both paths must be the same. So you need to place the same path in the upstream directive of /etc/nginx/sites-enabled/bcrypto and the one defined by the listen directive in unicorn.rb
Then restart Unicorn and reload Nginx

Docker nginx error: openssl: command not found

I am using nginx as a proxy to forward requests to other components (servers).
Each component, including nginx is implemented as docker container, i.e. I have a docker container for 'nginx-proxy', 'dashboard-server', 'backend-server' (REST API), and 'landing-server' (Landing Page). The latter 3 components are all NodeJS Express servers and working properly when I use the command docker-compose build there are no errors but when I start the containers with docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d the NodeJS containers work fine, but the nginx container gives me this error using docker-compose logs nginx-proxy:
Attaching to docker_nginx-proxy_1
nginx-proxy_1 | /start.sh: line 5: openssl: command not found
nginx-proxy_1 | Creating dhparams…\c
nginx-proxy_1 | ok
nginx-proxy_1 | Starting nginx…
nginx-proxy_1 | 2017/08/23 23:27:20 [emerg] 6#6:
BIO_new_file(“/etc/letsencrypt/live/admin.domain.com/fullchain.pem”)
failed (SSL: error:02001002:system library:fopen:No such file or directory:
fopen(‘/etc/letsencrypt/live/admin.domain.com/fullchain.pem’,‘r’) error:2006D080:BIO routines:BIO_new_file:no such file)
nginx-proxy_1 | nginx: [emerg]
BIO_new_file(“/etc/letsencrypt/live/admin.domain.com/fullchain.pem”) failed (SSL: error:02001002:system library:fopen:
No such file or directory:fopen(‘/etc/letsencrypt/live/admin.domain.com/fullchain.pem’,‘r’)error:2006D080:BIO routines:BIO_new_file:no such file)
I am using Lets Encrypt for the SSL certificates, however the command certbot certonly --webroot -w /var/www/letsencrypt -d admin.domain.com -d api.domain.com -d www.domain.com -d domain.com results in the error Connection Refused because the nginx server does not start to handle the requests.
My nginx Dockerfile (nginx-proxy/Dockerfile):
FROM nginx:1.12
COPY start.sh /start.sh
RUN chmod u+x /start.sh
COPY conf.d /etc/nginx/conf.d
COPY sites-enabled /etc/nginx/sites-enabled
ENTRYPOINT ["/start.sh"]
My start.sh file (nginx-proxy/start.sh):
#!/bin/bash
if [ ! -f /etc/nginx/ssl/dhparam.pem ]; then
echo "Creating dhparams…\c"
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
echo "ok"
fi
echo "Starting nginx…"
nginx -g 'daemon off;
My default.conf file (nginx-proxy/conf.d/default.conf):
include /etc/nginx/sites-enabled/*.conf;
My api.conf file (the others are similar) (nginx-proxy/sites-enabled/api.conf):
server {
listen 80;
server_name api.domain.com;
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/letsencrypt;
}
location = /.well-known/acme-challenge/ {
return 404;
}
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name api.domain.com;
ssl on;
ssl_certificate /etc/letsencrypt/live/api.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.domain.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:1m;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
client_max_body_size 0;
chunked_transfer_encoding on;
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/letsencrypt;
}
location = /.well-known/acme-challenge/ {
return 404;
}
location / {
proxy_read_timeout 900;
proxy_pass_header Server;
proxy_cookie_path ~*^/.* /;
proxy_pass http://backend-server:3000;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
}
}
Any ideas?
I found the solution.
In my nginx Dockerfile, I had to use
FROM nginx:1.12-alpine
RUN apk update \
&& apk add openssl
...
Then the openssl command worked properly.
At first, you can try edit your start.sh file at line openssl to /usr/bin/openssl. Did /usr/bin/openssl exists?
Second, your nginx server will not start until /etc/letsencrypt/live/api.domain.com/fullchain.pem and /etc/letsencrypt/live/api.domain.com/privkey.pem file exists.
So delete or comment all the server block that handling 443 port, keep server block that handling 80 port. Your api.conf will become this:
server {
listen 80;
server_name api.domain.com;
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/letsencrypt;
}
location = /.well-known/acme-challenge/ {
return 404;
}
return 301 https://$host$request_uri;
}
Then start your nginx server and retry install Let's Encrypt certificate.

Ruby on Rails on Digital Ocean Nginx Unicorn Page not found

UPDATE: I give up installing by myself and use the 1-click rails app and it's works fine.
I follow the instructions How To Deploy Rails Apps Using Unicorn And Nginx on CentOS 6.5 and build the exact app (my_app). The only change I made is include unicorn in the Gemfile.Both nginx and unicorn are running fine.
When only the ip address http://107.170.197.55/ the nginx page shows up. However when I run ip with tasks (http://107.170.197.55/tasks) it shows up "The page you are looking for is not found". Here's the code. Let me know if you need more information.
Is it because I am using CentOS 7?
/etc/nginx/conf.d/default.conf
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost;
# Application root, as defined previously
root /root/my_app/public;
try_files $uri/index.html $uri #app;
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
/var/www/my_app/config
# Set the working application directory
# working_directory "/path/to/your/app"
working_directory "/var/www/my_app"
# Unicorn PID file location
# pid "/path/to/pids/unicorn.pid"
pid "/var/www/my_app/pids/unicorn.pid"
# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "/var/www/my_app/log/unicorn.log"
stdout_path "/var/www/my_app/log/unicorn.log"
# Unicorn socket
listen "/tmp/unicorn.myapp.sock"
# Number of processes
# worker_processes 4
worker_processes 2
# Time-out
timeout 30

rails nginx unicorn listen multiple port

First, sorry for my English (T^T)
I want to run three apps.
(actually, one app. only Difference is environment (development, staging, production))
so I modify some conf codes.
but only working on 80 port!
If I change port of server on working (listening 80) to 81, it not works on port 81 T^T.
I dont know why it is only working on 80 port
Here is my my_app_nginx.conf, deploy.rb, unicorn.rb, unicorn_init.sh, 'netstat -lnp'
/etc/nginx/sites-enabled/my_app_nginx (it is included nginx.conf)
Three upstrem.
and three server on listening port 80(production), 3000(development), 3001(staging)
log_format compression '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
upstream unicorn_development {
server unix:/tmp/unicorn.chimiseng_development.sock fail_timeout=0;
}
upstream unicorn_staging {
server unix:/tmp/unicorn.chimiseng_staging.sock fail_timeout=0;
}
upstream unicorn_production {
server unix:/tmp/unicorn.chimiseng_production.sock fail_timeout=0;
}
server {
listen 80;
root /bps_data/apps/chimiseng_production/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn_production;
location #unicorn_production {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn_production;
}
error_page 500 502 503 504 /500.html;
error_log /bps_data/apps/chimiseng_production/shared/log/nginx_error.log warn;
access_log /bps_data/apps/chimiseng_production/shared/log/nginx_access.log compression;
client_max_body_size 4G;
keepalive_timeout 10;
}
server {
listen 3000;
root /bps_data/apps/chimiseng_development/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn_development;
location #unicorn_development {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn_development;
}
error_page 500 502 503 504 /500.html;
error_log /bps_data/apps/chimiseng_development/shared/log/nginx_error.log warn;
access_log /bps_data/apps/chimiseng_development/shared/log/nginx_access.log compression;
client_max_body_size 4G;
keepalive_timeout 10;
}
server {
listen 3001;
root /bps_data/apps/chimiseng_staging/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #unicorn_staging;
location #unicorn_staging {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn_staging;
}
error_page 500 502 503 504 /500.html;
error_log /bps_data/apps/chimiseng_staging/shared/log/nginx_error.log warn;
access_log /bps_data/apps/chimiseng_staging/shared/log/nginx_access.log compression;
client_max_body_size 4G;
keepalive_timeout 10;
}
For reference.. http://my_domain.com:80 is logged at nginx_access_log
but http://my_domain.com:3000 or :3001 is never logged at nginx_access_log. just leave 0 bytes.
unicorn.rb
environment = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'production'
root = "/bps_data/apps/chimiseng_#{environment}/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
listen "/tmp/unicorn.chimiseng_#{environment}.sock"
worker_processes 2
timeout 30
deploy.rb
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command} #{rails_app}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/chimiseng"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
end
...
...
unicorn_init.sh
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/bps_data/apps/chimiseng_$2/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E $2"
... start|stop|force-stop|restart... codes...
netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
...
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:3001 0.0.0.0:* LISTEN -
...
Active UNIX domain sockets (only servers)
...
unix 2 [ ACC ] STREAM LISTENING 9525914 8564/unicorn.rb -E /tmp/unicorn.chimiseng_development.sock
unix 2 [ ACC ] STREAM LISTENING 9509620 13448/unicorn.rb -E /tmp/unicorn.chimiseng_staging.sock
unix 2 [ ACC ] STREAM LISTENING 9519355 3020/unicorn.rb -E /tmp/unicorn.chimiseng_production.sock
...
I tried to "nginx restart" and sh -c "/etc/init.d/unicorn_chimiseng_ENVIRONMENT restart ENVIRONMENT" not working.
But you can see netstat -lnp.. listen ports well and the socks is active.
And there is no default deferred options at nginx 'listen'
Why cannot use ports except for 80 ??
please help me T^T
Sorry T^T
I resolved this problem.
I'm using a cloud server.
All of a sudden, thinking about Fort forwarding in cloud server web site strike me.
Allow for 3000, 3001 port in the web site.. it works well...!
Every body click down unclear question.. sorry for reading this question. X(
Have a nice day!

Nginx + Unicorn do not allow upload file > 400mb

I have app in RoR, I test it on apache2 to upload file > 1 GB, it's working. But I must use nginx. I have this error after 100% upload on nginx server:
2012/11/09 17:17:01 [error] 1436#0: *12 upstream prematurely closed
connection while reading response header from upstream, client:
134.19.136.32, server: my_domain, request: "POST /attachments HTTP/1.1", upstream:
"http://unix:/tmp-sock/unicorn.my_domain.sock:/attachments", host:
"my_domain", referrer: "http://my_domain/"
I think problem is with unicorn settings, but I don't know where exactly.
BTW: Everthing work only in Firefox.
My config files:
#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
Sending a huge file straight to your rails application is not a good solution for uploading big files. You can use nginx upload module to handle big file uploads using unicorn+nginx. This way, nginx actually handles the file upload and instead of passing the whole file to rails as multipart-form data, you send the local file path, uploaded by nginx, to the unicorn rails server and all rails does is to move the file from the tmp path of the os to the path you define. Here I configured nginx and rails to do upload job with nginx upload module. Now there are better solution for handling uploads such as TUS protocol.
I solved the problem by increasing the keepalive_timeout in the Nginx configuration. Maybe this is not a proper solution, or maybe you can increase that based on location and request type. And for that Firefox part, Firefox waits for a response longer than Chrome.

Resources