How to handle both frontend and backend hosting on Railway?
I currently have my backend deployed on Railway and my frontend deployed on Firebase. I'd like my URL structure to be as follows:
mywebsite.com -> user facing website
mywesbtei.com/api/* -> all the API endpoints my website will need
These are two different services(React, and Python). Given that they're using URL parameters they will both need to be running inside Railway I assume? I can't have them split across service providers(Google and Railway)
Is there any option besides setting up nginx(or similar) to act as a reverse proxy? Clearly, I could serve my static site at the root route of my API server but I'd prefer to not do that.
11 Replies
Project ID:
N/A
N/A
may I ask what's wrong with
mywebsite.com
for the frontend
api.mywebsite.com
for the backendNothing in general and I thought about it because I could easily do it with my configuration that I already have.
Partially, I have existing code that depends on the other structure. In addition, it makes the API on a different domain, which can get a little messy with auth etc.
It also seems that the "general" best practice is to keep them on the same domain.
railway itself has the frontend and backend on different domains, so not bad practice at all. but I can understand not wanting to change code and I do know with can get tricky, just was curious is all.
have a look at this, it does exactly what you want https://railway.app/template/7uDSyj
make sure to use the private network!
and just for the sake of thoroughness, how are you getting railway to serve your react frontend?
Fair point, I might bight the bullet and switch -- as with all infrastructure/architecture decisions it seems there is not "right" answer.
I haven't thought about how to have railway deploy the React.js site(it's pure react not Next). If using Caddy, could just have any attached volume that stores the "compiled" static assets vs another container with a small web server.
Unless you have a better idea 😛
Thanks for your advice btw
create react app?
At the moment yes, but will be moving to Vite
for vite, take a look at this template https://github.com/brody192/vite-react-template
it uses nixpacks and a Caddyfile to serve the built site, no volume needed.
for how your project should look like all setup, take a look a the demo reverse proxy project https://railway.app/project/35d8d571-4313-4049-9699-4e7db7f02a2f
notice how neither the frontend or backend services are publicly exposed!
Sweet, I'll check that one out. Thank you!
happy to help, let me know if you have any questions at all!