roger
roger

Reputation: 9933

How can I deploy ruby-on-rails application in nginx?

I am deploy a ruby on rails application in a nginx, my application is as follow:

app/
    ...(omit other files)
    views/
        ...(omit other files)
        static_pages/
            ....(omit other files)
            home.html.erb
public/
    404.html
    422.html
    500.html
config/
    routes.rb
....(omit other files)

Here is the routes.rb:

 Rails.application.routes.draw do
     root 'static_pages#home'
 end

Here is the Gemfile:

source 'https://rubygems.org'
gem 'rails', '4.2.0'
gem 'bootstrap-sass', '~> 3.3.1'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bcrypt', '~> 3.1.7'
gem 'faker'
gem 'carrierwave'
gem 'mini_magick'
gem 'fog'
gem 'nokogiri'
gem 'will_paginate', '~> 3.0.6'
gem 'will_paginate-bootstrap'

group :development, :test do
  gem 'byebug'
  gem 'web-console', '~> 2.0'
  gem 'spring'
end

group :test do
  gem 'minitest-reporters'
  gem 'mini_backtrace'
  gem 'guard-minitest'
end

group :production do
  gem 'pg', '0.17.1'
  gem 'rails_12factor'
  gem 'unicorn'
end

as you can see, my home page is app/views/static_pages/home.html.erb and only some error pages are in public/.

So how can I config nginx to point to my home page?(my application is in /home/roger/blog/)

server {
    listen 80 default;
    server_name example.com;
    location {
        root ?????????;
        index ?????????;
    }
}

Upvotes: 0

Views: 512

Answers (1)

ihaztehcodez
ihaztehcodez

Reputation: 2133

You don't configure nginx to point to your home page, you configure nginx to proxy traffic to your rails app and the rails app does everything else (with few exceptions).

Here is a simple example that assumes the rails app is listening to socket /tmp/unicorn.app.sock and the application root is /vagrant.

upstream unicorn {
  server unix:/tmp/unicorn.app.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  root /vagrant/public;
  try_files $uri/index.html $uri @unicorn;

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header HOST $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 5;
}

You can change the root (in your case /home/roger/blog/public) and upstream server to whatever is appropriate for your setup (maybe you want to change unix:/tmp/unicorn.app.sock to http://127.0.0.1:3000).

Here is an example that should work for you with no changes (assuming your app server is listening on port 3000):

upstream blog {
  server 127.0.0.1:3000;
}

server {
  listen 80 default deferred;
  root /home/blog/roger/public;
  try_files $uri/index.html $uri @blog;

  location @blog {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header HOST $http_host;
    proxy_redirect off;
    proxy_pass http://blog;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 5;
}

Upvotes: 1

Related Questions