0

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 ?????????;
    }
}
4
  • Can you please provide details on OS and Gemfile and method (process) you use to access your app? Commented Jan 20, 2015 at 8:08
  • @borjagvo, of course, my os is ubuntu, my gem file has a little bit too much code, if you still need, I will add it later Commented Jan 20, 2015 at 8:23
  • @borjagvo, ok, the Gemfile is added. Commented Jan 20, 2015 at 8:30
  • I refer to this document karolgalanciak.com/blog/2013/07/19/…. It has a step by step approach. Commented Jan 20, 2015 at 9:41

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

can you explain a little bit more about why and how to make rails app listening to socket unix:/tmp/unicorn.app.sock, what is this?
In my case I am using unicorn as the app server and I have a file config/unciorn.rb that with a line listen '/tmp/unicorn.app.sock'. This tells unicorn to listen to that socket (which is a glorified file in *nix) instead of listening on a port. It's not necessary but can be nice when you don't want to clutter up ports. If you don't currently have your rails traffic proxied then I would guess your app server is listening on localhost:3000 (same as 127.0.0.1:3000. I would encourage you to do more research, but this example should get you started with the changes I have described.
Also, Ryan Bates did a nice railscast on unicorn and nginx here. In fact my example is pretty much his example verbatim. It's behind a paywall, but the $9 change is non-recurring since he's not doing them anymore. Some of the information there is beginning to age, but it's well worth the money and there is a ton of good information there (just be mindful some things may have changed).
Thank you very much, you have explain very well, I am deploy it with passenger
I am sorry, with your config file, I got 403 error, the error log is2015/01/21 16:04:48 [error] 12432#0: *1 directory index of "/home/roger/ruby_workspace/hello_app/public/" is forbidden, client: 192.168.44.1, server: , request: "GET / HTTP/1.1", host: "192.168.44.131". Do you know why is this?(by the way my application is ok in WEBrick)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.