502 Nginx and Puma - Ubuntu 14.04

July 3, 2017 817 views
Nginx Ruby on Rails Ubuntu

I can't figure out how to configure properly nginx and puma in my ruby on rails application.
When a access it in the browser I get a 502.

I see many similar files:

  • /etc/nginx/nginx.conf
  • /etc/nginx/sites-enabled/myapp
  • /etc/nginx/sites-enabled/myapp_production
  • /etc/nginx/sites-avaliable/myapp_production
  • ~/apps/me/current/config/ngnix.conf

Do I really need that file config/nginx.conf inside my application?
How do sockets are supposed to be configured and work?
Is there a file among these I mentioned I don`t really need?

If Nginx was working/configured properly it should show this file /var/www/html/index.nginx-debian.html ?

If you need any additional information to be able to help me I'll gladly provide it.
Thanks in advance,
Diego

3 Answers

Hi @DiegoBenetti

/etc/nginx/nginx.conf = this is the main Nginx configuration.
/etc/nginx/sites-avaliable/myappproduction = this is one of your sites.
/etc/nginx/sites-enabled/myapp = this is one of your sites, should be a symlink.
/etc/nginx/sites-enabled/myapp
production = this is most likely a symlink.
~/apps/me/current/config/ngnix.conf = I do not know what this is.

The main Nginx configuration should not have any specific website information.

Your websites are normally located in sites-available and then symlinked from sites-enabled, so the sites that are actually active is located in sites-enabled. If that's confusing, then you can move all files from sites-available to sites-enabled - that means every file will be active.

You should post the ~/apps/me/current/config/ngnix.conf since I don't know what it is.

If Nginx was configured correctly, then it would normally show content from the root directory specified in one of the site-configurations.

And instead of just the visible error message, let's have a look in the error log:

tail -30 /var/log/nginx/error.log

I tried to follow this
digitalOcean tutorial

This tutorial, in the end of 6th step, suggests that nginx.conf file you asked for. The following is mine:

upstream puma {
  server unix:///home/deploy/apps/diegodigital/shared/tmp/sockets/myapp-puma.sock;
}

server {
  listen 80 default_server deferred;
  server_name my.url;

  root /home/deploy/apps/myapp/current/public;
  access_log /home/deploy/apps/myapp/current/log/nginx.access.log;
  error_log /home/deploy/apps/myapp/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'm totally beginner with nginx, so I can't tell what is good and right configuration from what it's not yet.
I don't know what is the nginx default configuration, how it is supposed to work.

I have only one website to launch, if there is more configuration than needed to one, it means I already committed a mistake.

by Sheharyar Naseer
In this tutorial we'll install Ruby and Nginx on a DigitalOcean Ubuntu Droplet and configure Puma and Capistrano in our web app. Nginx will be used to capture client requests and pass them over to the Puma web server running Rails. We'll use Capistrano to automate common deployment tasks, so every time we have to deploy a new version of our Rails app to the server, we can do that with a few simple commands.
  • @DiegoBenetti Can you list all the files in these to directories, so we can see if it's a file or a symlink:

    ls -ls /etc/nginx/sites-available/
    
    ls -ls /etc/nginx/sites-enabled/
    

    But your configuration of the config/nginx.conf doesn't follow the tutorial completely. You haven't replaced all the text with red with whatever your user is called (deploy) and whatever your app is called (myapp).
    Because of that, you're then probably linking to a socket that doesn't exists, which is why you're getting the 50x error.

    • deploy@ubuntu1404-server:~$ ls -ls /etc/nginx/sites-available/
      total 4
      4 -rw-r----- 1 deploy deploy 1394 Jul  4 14:47 diegodigital_production
      
      deploy@ubuntu1404-server:~$ ls -ls /etc/nginx/sites-enabled/
      total 0
      0 lrwxrwxrwx 1 root root 56 Jul  4 14:57 diegodigital -> /home/deploy/apps/diegodigital/current/config/nginx.conf
      

      these commands were very helpful!

      my config/nginx.conf exactly as it is right now:

      • user: deploy
      • application: diegodigital
      upstream puma {
        server unix:///home/deploy/apps/diegodigital/shared/tmp/sockets/diegodigital-puma.sock;
      }
      
      server {
        listen 80 default_server deferred;
        #server_name diego.digital;
      
        root /home/deploy/apps/diegodigital/current/public;
        access_log /home/deploy/apps/diegodigital/current/log/nginx.access.log;
        error_log /home/deploy/apps/diegodigital/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;
      }
      

upstream puma {
server unix:///home/deploy/apps/diegodigital/shared/tmp/sockets/myapp-puma.sock;
}

server {
listen 80 defaultserver deferred;
server
name my.url;

root /home/deploy/apps/myapp/current/public;
accesslog /home/deploy/apps/myapp/current/log/nginx.access.log;
error
log /home/deploy/apps/myapp/current/log/nginx.error.log info;

location ^~ /assets/ {
gzipstatic on;
expires max;
add
header Cache-Control public;
}

tryfiles $uri/index.html $uri @puma;
location @puma {
proxy
setheader X-Forwarded-For $proxyaddxforwardedfor;
proxy
setheader Host $httphost;
proxy_redirect off;

proxy_pass http://puma;

}

errorpage 500 502 503 504 /500.html;
client
maxbodysize 10M;
keepalive_timeout 10;
}

Have another answer? Share your knowledge.