Configure multiple Rails Application

Configure multiple Rails Application In Nginx, The objective of this article is to configure two rails applications with Nginx so that we can access our rails app through the name server. Like (app1.xyz.com and app2.xyz.com)

In a previous blog, we already learned how to configure Nginx and Puma together.

In this blog, we take two rails applications make sure both apps are running over the different ports like 3000,4000

let’s have a look at puma.rb

application_path="<Application path>"
 directory application_path
 port 3000 # Or 4000 
 environment "production"
 daemonize true
 pidfile "#{application_path}/config/puma.pid"
 state_path 'tmp/pids/puma.state'
 stdout_redirect "#{application_path}/log/puma.log", "#{application_path}/log/puma_err.log"
# # quiet
 threads 1,16
 bind "unix://#{application_path}/puma.sock"
 workers 2
 preload_app!
  on_worker_boot do
    puts 'On worker boot...'
  End

Let’s Configure Nginx config file.

Navigate your terminal to 

$cd /etc/nginx/site-avilable
$nano default

let’s have a look at Nginx Config file

upstream app {
    server unix:///var/www/<location or app Name>/puma_test_prod.sock fail_timeout=0;
}

server {
    listen 80;
    #server_name localhost;
    server_name xyz.com;
    root /var/www/<location or app Name>/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;
}

Just change the file text with the below text.

upstream app1 {
    server unix:///var/www/<location or app Name>/puma_test_prod.sock fail_timeout=0;
}
upstream app2 {
    server unix:///var/www/<location or app Name>/puma_test_prod.sock fail_timeout=0;
}

server {
    listen 80;    
    server_name app1.xyz.com;
    root /var/www/bommgt/public;

    try_files $uri/index.html $uri @app1;

    location @app1 {
        proxy_pass http://app1;
        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;
}

server {
    listen 80;
    server_name app2.xyz.in;
    root /var/www/<location or app Name>/public;

    try_files $uri/index.html $uri @app2;

    location @app2 {
        proxy_pass http://app2;
        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;
}

Restart your Nginx Server
$systemctl restart nginx

Make sure both rails applications are up and running .

Conclusion

I hope this article helps you to Configure multiple Rails Application.
Now you can access both apps through 
http://app1.xyz.com
http://app2.xyz.com

Thanks for reading …

Leave a Reply

Your email address will not be published. Required fields are marked *