How to config subdirectory with Rails + Nginx + Unicorn?

How to config subdirectory with Rails + Nginx + Unicorn?

This article mainly focuses on how to run the rails app in a subdirectory using NGINX. You will learn the reverse proxy of Nginx to serve your rails app.

It was needed because I came to meet a problem in which I want to run a specific part of a website in Ruby on Rails for a specific subdirectory.  For example, my website is in PHP www.myphpwebsite.com/ and I want to run Ruby on rails site on the same url with different subdirectories like www.myphpwebsite.com/runthisinrubyonrails/. Whenever /runthisinrubyonrails/ detected, It should run on a different servers which is running   ROR.

Table of Contents

Configuration in a rails app.

Rails give us a simple configuration for this purpose.

In config/application.rb file, add the following code:

module YourAPPName
 class Application < Rails::Application
   config.relative_url_root = '/runthisinrubyonrails'
   # some other configuration code
 end
end

In config.ru file:

map Rails::Application.config.relative_url_root || "/" do
 run Rails.Application
end

Test everything works fine

we can check everything works fine with click route with /runthisinrubyonrails/ and simple by /.

for example, first asset was getting by www.myphpwebiste.com/assets/favicon.ico now you can try with www.myphpwebsite.com/runthisinrubyonrails/assets/favicon.ico. Both should work because when we have defined that in our config.ru file

Rails::Application.config.relative_url_root || "/"

if relative_url_root found then try with that if not then try with “/”

Nginx Configuration

So we have the following configuration:

upstream unicorn_sock {
 server your_sock_path;
}

server {
root <path_to_your_rails_app>/public;
location @proxy_rails_app {
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header Host $http_host;
 proxy_redirect off;
 proxy_pass http://unicorn_sock;
}
location /runthisinrubyonrails/ {
 alias <path_to_your_rails_app>/public/;
 try_files $uri @proxy_rails_app;
}
try_files $uri @proxy_rails_app;
# some other configuration code
}

Subscribe to our Newsletter.

Thank you! Your subscription has been added to our newsletter!
Oops! Something went wrong while submitting the form.

Related Blogs.

How to Create a Winning Web Presence with Ruby on Rails
logos
How to Create a Winning Web Presence with Ruby on Rails
Get the most out of your web presence with Ruby on Rails by taking advantage of the advantages it provides and tips from Phaedra Solutions on creating successful web applications.
Ruby on Rails: The Entrepreneur's Secret for Successful Web Development
logos
Ruby on Rails: The Entrepreneur's Secret for Successful Web Development
Get the low-down on Ruby on Rails, the powerful, efficient, and cost-effective web development tool that entrepreneurs are using to succeed in their online businesses.
Why Product Owners, Developers and Investors Love Ruby on Rails
logos
Why Product Owners, Developers and Investors Love Ruby on Rails
Find out why product owners, developers and investors love Ruby on Rails and how it can help get the most out of web development projects in this blog post.

07 — Contact Us

heading glow
glow

Take the road to {digital transformation} with our top-notch software development services.

map
2 West Canal Bank Road, Lahore ,Pakistan
500 Grant Street, Downtown, Suite 2900,Pittsburgh, PA 15219, USA
3 Dalmeny Ave Huddersfield, Huddersfield, HD4 5NN, UK
DSO-IFZA , IFZA Properties, Dubai Silicon oasis, UAE
Don’t like the forms? Drop us a line via email.
hello@phaedrasolutions.com
no-img
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Pakistan
2 West Canal Bank Road, Lahore ,Pakistan
UK
500 Grant Street, Downtown, Suite 2900, Pittsburgh, PA 15219, USA
dubai
3 Dalmeny Ave Huddersfield, Huddersfield, HD4 5NN, UK
america
DSO-IFZA , IFZA Properties, Dubai Silicon oasis, UAE