Explore Tech Stack › How to Config Subdirectory with Rails + Nginx + Unicorn?

How to Config Subdirectory with Rails + Nginx + Unicorn?

How to Config Subdirectory with Rails + Nginx + Unicorn | Phaedra Solutions
Nabeela Anwar, Content Marketer & Strategist at Phaedra Solutions
Nabeela Anwar
Content Marketer & Strategist
Author
Last edited:10/06/2025
Recently Updated on October 6, 2025
Index

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
}

Share this blog
Nabeela Anwar, Content Marketer & Strategist at Phaedra Solutions
Nabeela Anwar
Content Marketer & Strategist
Author
Nabeela is a content writer with 5+ years of experience in digital content and marketing strategy. Her articles have helped brands improve engagement by 2x and rank higher on search engines. Her writing turns complex ideas into engaging content that resonates with the right audience. She focuses on creating articles that inform, inspire action, and support business growth.
Looking For Your Next Big breakthrough? It’s Just a Blog Away.

Great! We’ve received your information.

What are you looking to achieve?*

When are you planning to start?*

What best describes your involvement in this project?*

Select Your Budget*

Select a Service or Technology*