Search code examples
ruby-on-railsreactjscontent-management-systemsingle-page-applicationapi-design

Is it possible to run a Rails app and React app on the same domain?


I have a scenario where I want to build a site which would serve as a CMS and social network. All along I would be using Rails only as my API provider. I was originally going to do the client side entirely in React with Redux but as the build grew I found myself writing tons of boilerplate for the most simple tasks (CMS wise).

Instead of going through with the build I thought it would be best to build out the CMS using Rails being that you get your CRUD based tools right of the box. At this point I would have the API and CMS in the same Rails app.

The social network aspect of writing posts, creating collections, etc. would still be done using React. I was wondering if it were at all possible to accomplish a scenario where I could separately make a Rails app and SPA under the same domain. Any resources would be greatly appreciated.

NOTE: I'm aware there's a React gem for rails but would rather use it as a standalone to take advantage of using Redux and other Node packages.


Solution

  • Consider using a reverse proxy like Nginx. Nginx allows you to map different paths to different hosts or same host (with different ports)

    location /cms/ {
       proxy_set_header X-Real-IP  $remote_addr;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_set_header Host $host;
       proxy_pass http://192.168.1.2:3000;
       }
    
    location /social/ {
       proxy_set_header X-Real-IP  $remote_addr;
       proxy_set_header X-Forwarded-For $remote_addr;
       proxy_set_header Host $host;
       proxy_pass http://192.168.1.2:3100;
    }
    

    You can find more information in the following URL

    https://www.nginx.com/resources/admin-guide/reverse-proxy/