Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One domain name for multiple Rails apps with Nginx and Unicorn

I have two Rails apps and I want to host them with just one domain name like this:

app1.example.com
app2.example.com

I have a VPS on digital ocean and I have already run one app with Nginx and Unicorn. This is my nginx configuration file:

upstream app1{
  server unix:/tmp/unicorn.app1.sock fail_timeout=0;
}
#upstream app2{
#  server unix:/tmp/unicorn.app2.sock fail_timeout=0;
#}
server{
  listen 80; 
  root /var/www/app1/public;
  try_files $uri/index.html $uri.html $uri @app;
  location @app{
    proxy_pass http://app1;
  }
  error_page 500 502 503 504 /500.html;
}

It seems I need another server block to host another app, but I don't know how to let nginx differentiate the two server blocks since I only have one domain. Any ideas?

like image 830
Joey Hu Avatar asked Sep 14 '14 21:09

Joey Hu


1 Answers

ok since you already defined 2 subdomains, you just need to add the server_name to the nginx blocks

upstream app1{
  server unix:/tmp/unicorn.app1.sock fail_timeout=0;
}
upstream app2{
  server unix:/tmp/unicorn.app2.sock fail_timeout=0;
}
server{
  listen 80;
  server_name app1.domain.com;
  root /var/www/app1/public;
  try_files $uri/index.html $uri.html $uri @app;
  location @app{
    proxy_pass http://app1;
  }
  error_page 500 502 503 504 /500.html;
}
server{
  listen 80;
  server_name app2.domain.com;
  root /var/www/app2/public;
  try_files $uri/index.html $uri.html $uri @app;
  location @app{
    proxy_pass http://app2;
  }
  error_page 500 502 503 504 /500.html;
}
like image 53
Mohammad AbuShady Avatar answered Oct 18 '22 14:10

Mohammad AbuShady