Im trying to setup Nginx, Unicorn and Rails application to work together.
Nginx and Nnicorn are running, I checked that using ps command.
But when trying to access my page Ive got 502 Bad Gateway
Nginx error log has line:
2015/03/18 19:53:26 [error] 14319#0: *1 connect() to
unix:/var/sockets/unicorn.mypage.sock failed (11: Resource temporarily
unavailable) while connecting to upstream
What can be the problem?
my /etc/nginx/conf.d/default.conf
upstream app {
server unix:/var/sockets/unicorn.mypage.sock fail_timeout=0;
}
server {
listen 80;
server_name mypage.com;
# Application root, as defined previously
root /home/rails/mypage/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control 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;
}
/home/rails/mypage/config/unicorn.rb
working_directory "/home/rails/mypage"
pid "/home/rails/mypage/pids/unicorn.pid"
stderr_path "/home/rails/mypage/log/unicorn.log"
stdout_path "/home/rails/mypage/log/unicorn.log"
listen "/var/sockets/unicorn.mypage.sock", backlog: 1024
worker_processes 2
timeout 30
It looks like socket problem, but it is usually (111: Connection refused) when socket is down, so I think this is app problem (high load, slow execution etc).
Try decrease backlog and see logs again for details:
listen "/var/sockets/unicorn.mypage.sock", backlog: 64
:backlog => number of clients
I solved the problem.
It was because of my unicorn server started in development environment, not in production. Unicorn was trying to connect to development database, but credentials for the dev db in database.yml were missing.
After I started unicorn in production env everything connected well.
Related
I need to deploy my rails application,So I have followed all step from here, https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04
But end of the tutorial, I get this error --> "502 Bad Gateway"
EDIT
The error message now --> "We're sorry, but something went wrong."
But Nginx error output is the same, I check puma error messages but they just log when it start and when it stop gracefully.
Rails logs which is under app_directory/log does not produce any output.
puma-manager --> I checked it works correctly
paths ---> I have checked three times
Nginx error.log output message:
2016/05/18 14:22:21 [crit] 1099#0: *7 connect() to unix:/home/deploy /hotel-automata/shared/sockets/puma.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.2.105, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/deploy/hotel-automata/shared/sockets/puma.sock:/500.html", host: "192.168.2.170"
OS -> Vmware Player, Bridged Network Ubuntu Server 14.0.4
Ruby Version: 2.3.1
Rails Version: 4.2.5.2
This is my nginx config contents of /etc/nginx/sites-available/default
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:/home/deploy/hotel-automata/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost;
root /home/deploy/hotel-automata/public;
try_files $uri/index.html $uri #app;
location #app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
EDIT:
Make user that the socket exist. Otherwise it's failing on this point:
in config/puma.rb you need to have line pointing to your socket:
bind "unix://<path or variable for the path where the socket will be>/sockets/puma.sock"
example with variable:
application_path = '/home/deploy/hotel-automata/shared'
bind "unix://#{application_path}/sockets/puma.socket"
Check permissions on the socket
You will need to make sure that Nginx is able to access your socket (have the required rights i.e. RW)
The check the permissions on the whole path try this:
namei -m /home/deploy/hotel-automata/shared/sockets/puma.sock
Alternatively try this:
sudo -u <user> test <-r / -w > <path> && echo True
i.e.
sudo -u nginx test -w /home/deploy/hotel-automata/shared/sockets/puma.sock && echo True
Nginx will require RW access to that socket.
If it doesn't return true then it means that the user has NOT got that permission i.e. -w -> write
Your puma.rb file should look like this.
# /config/puma.rb
app = "manabalss" # App-specific
root = "/home/deployer/apps/#{app}"
workers 5
threads 1, 1 # relying on many workers for thread-unsafe apps
rackup DefaultRackup
port 11592
environment ENV['RACK_ENV'] || 'production'
daemonize true
pidfile "#{root}/puma/puma.pid"
stdout_redirect "#{root}/puma/puma.log", "#{root}/puma/puma_error.log"
bind "unix:/tmp/puma.socket
And your nginx.conf should be like this.
# config/deploy/nginx.conf
upstream puma {
server unix:/tmp/puma.socket fail_timeout=1;
}
# This block redirects http requests to https version
server {
listen 37.139.0.211:80 default deferred;
server_name www.manabalss.lv, manabalss.lv;
return 307 https://manabalss.lv$request_uri;
}
server {
listen 37.139.0.211:443 ssl;
server_name manabalss.lv;
ssl_certificate /etc/ssl/server.crt;
ssl_certificate_key /etc/ssl/server.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers RC4:HIGH:!aNULL:!MD5;
ssl_session_cache shared:SSL:15m;
ssl_session_timeout 15m;
root /home/deployer/apps/manabalss/current/public;
location ^~ /assets/ {
gzip_static on;
gzip_vary on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #puma;
location #puma {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
If this doesn't help, you would like to have a look at this.
Rails + Puma + Nginx Every Bad Gateway 502
I'm following this tutorial to deploy a RoR app with Capistrano but I'm getting an error in my production server
[error] 28314#0: *1 connect() to unix:/tmp/unicorn.myapp.sock failed (111: Connection refused) while connecting to upstream, client: xx.xxx.xx.xx, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.myapp.sock:/", host: "myapp.cloudapp.azure.com"
My /etc/nginx/sites-available/default
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}
server {
listen 3000;
server_name localhost;
root /home/deploy/apps/myapp/current/;
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;
}
I changed the server to many different things, but I get the exact same error, with the same host, server, upstream.
The problem was with Unicorn, somewhere in my Capistrano deploy, it wasn't restarting Unicorn as it should.
Nginx logs indicate that this is an issue with the backend server. After your backend up is up and running. Check:
Does the socket file exist? ls -lthd /tmp/unicorn.myapp.sock
Can you make a direct HTTP connect to the socket, bypassing Nginx?
Once those tests pass, test again through Nginx.
Here is my nginx config
server {
listen 80;
server_name localhost;
root /home/user/app/public;
try_files $uri/index.html $uri #app;
location #app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Let's say my public ip: 111.111.111.111
When I try to enter the site 111.111.111.111/path, it works fine but when I try to enter only the root url 111.111.111.111 it gives me the "Welcome to nginx" message. I looked at the nginx log, but there is nothing about the root url.
I m also using unicorn, but I do not think there was a problem about that, I assume I ve added the necessary part of config here.
I think it might be missing the unicorn .sock file: see example nginx/unicorn/rails config here: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04
You're referencing an upstream in your proxy_pass line but it's not defined. Try adding this to the top:
upstream app {
# Path to Unicorn SOCK file
server unix:/path/to/your/app/shared/sockets/unicorn.sock fail_timeout=0;
}
I just looked at an app i did a while ago with rails, unicorn and nginx, and my entry for this was
server unix:/tmp/.sock fail_timeout=0;
It just depends how your unicorn is configured, as to where the sock file lives, so you'll need to tweak it to point to the right place.
I am trying to deploy my rails application (Nginx + Puma) however it seems that there is some user right issues that associated with it.
The error message from "nginx.error.log" is as follows:
2015/11/12 09:07:23 [error] 1148#0: *10 connect() to
unix:///home/deploy/apps/app_name/shared/tmp/sockets/xxx-puma.sock failed
(111: Connection refused) while connecting to upstream, client: 61.6.11.121,
server: 168.63.241.117, request: "GET / HTTP/1.1",
upstream: "http://unix:///home/deploy/apps/app_name/shared/tmp/sockets/xxx-puma.sock:/",
host: "xxx"
When I enter the directory that contains the puma.sock file and perform a "ls -l" I have noticed the following:
Puma Directory
I have done the exact same settings (capistrano) on my deployment server and replicated it onto my staging server. The only difference between the both is that the puma.sock on my staging server is highlighted in pink color (I suspected it's related to user access right)
Anyone can help me on the issues? Thanks in advance.
Update (nginx.conf):
upstream puma {
server unix:///home/deploy/apps/app_name/shared/tmp/sockets/puma.sock;
}
server {
listen 80;
server_name xxx;
root /home/deploy/apps/app_name/current/public;
access_log /home/deploy/apps/app_name/current/log/nginx.access.log;
error_log /home/deploy/apps/app_name/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #puma;
location #puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
I think you miss Procfile
Create a Procfile and add the following code in
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
You've specified app_name-puma.sock in upstream nginx conf and your actual socket file is named puma.sock
try changing:
upstream puma {
server unix:///home/deploy/apps/app_name/shared/tmp/sockets/app_name-puma.sock;
}
to:
upstream puma {
server unix:///home/deploy/apps/app_name/shared/tmp/sockets/puma.sock;
}
I have stucked with production server. Already trying to resolve it for 2 days but do not understand why it happens like this.
The problem is:
I have tried to deploy Rails application with nginx/unicorn/capistrano. Finally, I got the process working. Nginx and unicorn seem ok on my production server, but when I enter to my server's IP, there is just a blank screen. Before I had there at least a message that nginx is runned. I suppose that the problem may be that nginx and unicorn are searching the app in a wrong place, but I checked the routes and they seem correct.
The application has a root welcome page, so it should be displayed there...
Could you help me with your thougts what might be a problem?
P.S. if I create a test rails app at server and run it using webricks, is showed at MY_SERVER_IP:3000, so the problem is definetly with unicorn/nginx.
My nginx.conf file:
upstream unicorn {
server unix:/tmp/unicorn.foreignernetwork.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name justforeign.com;
root /var/www/foreignernetwork/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control 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 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
My unicorn.rb file:
root = "/var/www/foreignernetwork/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.foreignernetwork.sock"
worker_processes 2
timeout 30
# Force the bundler gemfile environment variable to
# reference the capistrano "current" symlink
before_exec do |_|
ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')
end