Nginx reverse proxy not working with Puma - ruby-on-rails

I need some help setting up Nginx with Puma and Rails on Ubuntu server at Digital Ocean (already spend a lot time on this).
I have the follow settings:
/etc/nginx/nginx.conf (I remove some commented lines)
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
/etc/nginx/sites-available/test_server
upstream app {
server unix:/home/deploy/www/test_server/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name testserver.com.br www.testserver.com.br; # fake url
root /home/deploy/www/test_server/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;
}
/var/log/nginx/access.log
I can see a lot of requests (from my local machine, from the server at D.O and lot of others IP's that I don't recognize).
/var/log/nginx/error.log
When I try to access www.testserver.com/index from my local desk
2018/06/02 12:21:25 [error] 27541#27541: *6 open() "/usr/share/nginx/html/index" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: localhost, request: "GET /index HTTP/1.1", host: "www.testserver.com.br"
Firewall
Firewall is open to Nginx HTTP/HTTP (v6)
Symbolic link in /etc/nginx/sites-enabled/test_server
lrwxrwxrwx 1 root root 39 Jun 1 16:25 /etc/nginx/sites-enabled/test_server -> /etc/nginx/sites-available/test_server
What I have until now
When I make a request to the site I got this message:
"Welcome to nginx! If you see this page, the nginx web server is successfully installed and working..."
I know Puma is working fine because I can make requests direct to the socket and it returns the correct page (Rails starts in production environment ~ all working fine).
Last considerations from my attempts
I believe that Nginx is not able to connect properly with Puma and find the right files to serve. From the error.log I can see Nginx trying to access the folder /usr/share/nginx/html/ when it should try to connect to the socket, right? Maybe permission is the problem? (I saw a lot of posts on SO with permission problems).
I don't know what else I can try.
Any additional information just ask.
Thanks in advance!
Solution
As I deleted the nginx.conf file (not on purpose) and copy from the git rep., the new file was missing the include /etc/nginx/conf.d/*.conf; and include /etc/nginx/sites-enabled/*; lines. After adding these lines inside the section Http, Nginx started serving correct.

Related

Rails Nginx config can not reach root url

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.

Rails, Nginx, Puma access right error (puma.sock highlighted in pink when perform ls -l)

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;
}

Nginx, Unicorn and Rails = 502 Bad Gateway

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.

rails 4 nginx and unicorn serves only public files (html)

Running Ubuntu 12.10 x64, Nginx, Unicorn, Rails 4
bundle exec rake routes shows all the routes correctly, but when I access any routes (controller/action) it times out (nginx 504). When I open any static HTML files (public directory), it works fine.
Tried restarting server, nginx, unicorn, installed all the gems. What else am I missing?
This is my unicorn log
http://pastebin.com/5BHzqCA9
You have to add socket file to unicorn.rb config file and set it for listener. Something like this:
sock_file = "path/to/unicorn.sock"
......
listen sock_file
In your nginx conf you need:
upstream rails_server {
server unix:/path/to/sock;
}
server {
listen 80;
server_name example.com;
root /path/to/public;
access_log /path/to/log/nginx_access.log;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /path/to/public;
}
try_files $uri, #rails;
location #rails {
proxy_pass http://rails_server;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}

nginx + passenger + rails: do I need to start the rails server or just start nginx?

I'm trying to have my rails server listen on 2 different ports. One solution proposed to me was to use nginx. I installed nginx with sudo passenger-install-nginx-module and added the following to /etc/nginx/conf.d:
server {
listen 80;
listen 10000;
server_name www.myapp.com
passenger_enabled on;
root /root/myapp/public;}
When I went to www.myapp.com I got a 403 Forbidden error. I figured it was because there were no static html files in /public. I dropped a simple "hello world" html page in there and it loaded correctly. I then proceeded to start my rails app using passenger start -e production, which caused it to run in standalone phusion passenger mode on port 3000. I go to myapp.com:3000 and I get the app. However, myapp:80 and myapp:10000 still don't work. I'm confused on how to get my nginx to point to the rails server I'm running. Am I doing this completely wrong? Thanks!
Set nginx to forward to my rails server using this https://gist.github.com/jeffrafter/1229497
worker_processes 1;
error_log /usr/local/var/log/nginx.error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream dev {
server 127.0.0.1:3000;
}
server {
listen 80;
# You could put a server_name directive here (or multiple) if
# you have not setup wildcard DNS for *.dev domains
# See http://jessedearing.com/nodes/9-setting-up-wildcard-subdomains-on-os-x-10-6
# If we choose a root, then we can't switch things around easily
# Using /dev/null means that static assets are served through
# Rails instead, which for development is okay
root /dev/null;
index index.html index.htm;
try_files $uri/index.html $uri.html $uri #dev;
location #dev {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://dev;
}
error_page 500 502 503 504 /50x.html;
}
}

Resources