Search code examples
ruby-on-railsregexnginxamazon-ec2production-environment

how to run two apps on EC2 with nginx


I am a newbie in Ubuntu and generally server side and I have created a Rails app and have deployed it on Ubuntu Ec2. I am using Nginx and Thin server on it.The app is running perfectly on it.

Now I want to deploy another app on the same server. I have already put the app on the server and when i try to start the rails app it does not start. I guess it is because of nginx.conf file.

Can someone please let me know how to run two apps on the same server


Solution

  • When you try to browse to a machine on Amazon's EC2, and you don't get any response, the best suspect is the AWS Security Group. Make sure that the port the application runs on is open in your machine's security group:

    Security group screen
    (source: amazon.com)

    For nginx to run both you apps, you need to configure them both on its nginx.conf

    upstream app1 {
      server 127.0.0.1:3000;
    }
    
    upstream app2 {
      server 127.0.0.1:3020;
    }
    server {
      listen   80;
      server_name .example.com;
    
      access_log /var/www/myapp.example.com/log/access.log;
      error_log  /var/www/myapp.example.com/log/error.log;
      root     /var/www/myapp.example.com;
      index    index.html;
    
      location /app1 {
        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://app1;
      }
    
      location /app2 {
        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://app2;
      }
    }
    

    This configuration will listen for app1 on local port 3000, and app2 on local port 3020, and redirect data starting with http://my.example.com/app1 to the first app, and data starting with http://my.example.com/app2 to the second app.