Search code examples
ruby-on-railsnginxdeploymentproxyunicorn

How to config nginx to proxy to rails app? so that i dont have to say domain.com:port


Update: Currently i visit my app at domain.com:3000, but i would like to visit domain.com to see my app
I have setup nginx at 80 to proxy my rails app at 3000. below is the configuration

upstream railsapp {
  server 127.0.0.1:3000;
}

server {
  listen 80;
  server_name APP;

  # Tell Nginx and Passenger where your app's 'public' directory is
  root /var/www/APP/current/public;
  index index.html index.htm;

  # Static assets are served from the mentioned root directory
location / {
    root /var/www/APP/current;
    index index.html index.htm;

    proxy_pass http://railsapp/;
    proxy_redirect off;
    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_set_header X-Real-Port $server_port;
    # proxy_set_header X-Real-Scheme $scheme;
    proxy_set_header X-NginX-Proxy true;
}

  # Turn on Passenger
  passenger_enabled on;
  passenger_ruby /usr/local/rvm/gems/ruby-2.1.3/wrappers/ruby;
}

i referred to : https://stackoverflow.com/a/5015178/1124639

this is located at /etc/nginx/sites-enabled/APP.conf and is included in /etc/nginx/nginx.conf as below within http{...}

include /etc/nginx/sites-enabled/*;

but my APP.com still shows 'Welcome to nginx on Ubuntu!' and APP.com:3000 shows my app. What am i doing wrong?

What i am using:
Ubuntu 14.04 ec2 instance
nginx 1.8.0
unicorn server at 3000


Solution

  • I was trying to run unicorn so i can fork my app to multiple instances. I guess the issue here was, i set passenger_enabled on and was actually running unicorn on 3000.

    so instead i ran passenger

    passenger start -a 127.0.0.1 -p 3000 -d -e production
    

    and my nginx conf like this,

    server {
    listen 80;
    server_name www.APPNAME.com;
    
    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/APPNAME/current/public;
    index index.html index.htm;
    
    # Static assets are served from the mentioned root directory
    location / {
    #  root /var/www/APPNAME/current;
    #  index index.html index.htm;
    proxy_pass http://127.0.0.1:3000;
    
    proxy_redirect off;
    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_set_header X-Real-Port $server_port;
    # proxy_set_header X-Real-Scheme $scheme;
    proxy_set_header X-NginX-Proxy true;
    }
    
    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /usr/local/rvm/gems/ruby-2.1.3/wrappers/ruby;
    }
    

    and everything works now!