Search code examples
ruby-on-railsnginxproductionpuma

Deploy rails app at sub uri of a domain


We have set up a production server for our rails app with Nginx and puma. We want to deploy our rails app at sub uri and on main domain we want to put wordpress for home page, pricing page etc.

How we configure our rails that it able to run on sub uri which have Devise gem as authentication. Will we need to change our routes for sub uri?

What will be the configuration for nginx and puma?

Thanks in advance!


Solution

  • First put your desired sub uri path in application.rb i.e. main

    ...
    config.relative_url_root = "/main"
    ...
    

    In config.ru, add these lines.

    require ::File.expand_path('../config/environment',  __FILE__)
    map SampleApplication::Application.config.relative_url_root || "/" do
      run Rails.application
    end
    

    In production.rb, add below line.

    # Enable serving of images, stylesheets, and JavaScripts from an asset server  
    config.action_controller.asset_host = "YOUR_DOMAIN_NAME/main"
    
    # ActionMailer Config
    config.action_mailer.default_url_options = {
        :host => "YOUR_DOMAIN_NAME",
        :only_path => false,
        :script_name =>  "/main"
    }
    

    In nginx configuration file, add these lines

    location /main {
        alias /var/deploy/sample_application/current/public;
        try_files $uri @main;
    }
    
    location @main {
        proxy_http_version 1.1;
        chunked_transfer_encoding off;
        proxy_buffering off;
        proxy_cache off;
    
        proxy_redirect     off;
        proxy_set_header   Host             $http_host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-Proto $scheme;
    
        proxy_pass http://puma_sample_application;
    }