a year ago
Hi
I have a monorepo setup with turborepo.
My monorepo contains two apps:
backend (hono nodeJS server)
admin dashboard (Vite React frontend).
I have two services in my Railway project
FE
--- has a custom build command: pnpm build:frontend
--- has a custom start command: pnpm start:frontendBE
--- has a custom build command: pnpm build:backend
--- has a custom start command: pnpm start:backend
I have a question related to running my vite frontend.
I saw this template to deploy using Caddy.
I copied the Caddyfile + nixpacks.toml in my FE app, but I have the impression it's not doing anything or detecting it.
However, I do have a custom build and start command set. Maybe that might be the culprit.
I did deploy the React - caddy template and it worked fine.
So I am wondering if it's related to my monorepo setup or maybe something else.
Thanks for the help in advance.
0 Replies
a year ago
is your frontend part of the shared repo? as in its importing stuff from the backend?
@Brody backend and frontend app do not depend on each other. I made sure of that.
a year ago
isolated monorepo?
a year ago
gotcha
a year ago
I assume you already have two services in your railway project, one for the backend and one for the frontend?
a year ago
perfect
a year ago
90% there
a year ago
set the location to the nixpacks.toml file with the NIXPACKS_CONFIG_FILE service variable, and then change your start command to start Caddy, you will likely need to adjust the file path for the Caddyfile in both the start command and fmt command within the nixpacks.toml
Ok, before I do that I have a follow up question:
Is it the best and recommended way to run a vite react app with Caddy ? I saw in previous threads that some serve it with docker/node. Others were running with vite, but that was not recommended due to (very) high costs?
a year ago
the command vite runs a development server and that's going to be unstable and yes cost far more than a production grade web server such as Caddy
a year ago
a lot of people also use NGINX but I personally think Caddy is far more user friendly
a year ago
let me know if you run into any problems
a year ago
probably need to change the full path to the Caddyfile, given you have it in a subdirectory, you can edit that in the nixpacks.toml
a year ago
and then you'll want to remove the start command in the UI that you set so that the start command in the nixpacks.toml file will be used
a year ago
no you shouldn't, does it result in a 502?
a year ago
nope, please do not change that
a year ago
your target port is likely set incorrectly
a year ago
well, don't change it unless you have some custom config that puts the built static files anywhere other than the default location
a year ago
yep!
a year ago
all working now?
a year ago
you still have your build command set right?
a year ago
maybe try updating the root to use the full path? I can't remember needing to do that before though
a year ago
the root directory in the Caddyfile
a year ago
no problem!
I was testing with caddy locally.
I discovered the following:
When I run caddy locally from the root of the project/monorepo and run the command caddy run --config apps/admin-dashboard/Caddyfile, then it's not accessible on PORT 3000 . When i go to 8080 I receive a 404 error.
When I navigate into the FE folder (admin-dashboard), and run the same command caddy run --config Caddyfile, it's the same issue (404).
But when i just run caddy run in the FE folder, than it works locally (without the --config)
Which leads me to believe there is something still misconfigured in the Caddyfile ?
Ok, changing the root back to apps/admin-dashboard/Caddyfile fixed it. It's now up
Now I am figuring out how to do a reverse proxy so that api calls to /api/products go to the backend service.
I was able to figure out how to have the caddy reverse proxy working locally:
# global options
{
admin off # theres no need for the admin api in railway's environment
persist_config off # storage isn't persistent anyway
auto_https off # railway handles https for us, this would cause issues if left enabled
# runtime logs
log {
format json # set runtime log format to json mode
}
# server options
servers {
trusted_proxies static private_ranges 100.0.0.0/8 # trust railway's proxy
}
}
# site block, listens on the $PORT environment variable, automatically assigned by railway
:{$PORT:3000} {
# access logs
log {
format json # set access log format to json mode
}
# health check for railway
rewrite /health /*
handle /api/* {
reverse_proxy localhost:3001
}
# Serve static files and handle client-side routing
handle {
# Serve files from the 'dist' folder
root * apps/admin-dashboard/dist
file_server
# Rewrite all other requests to 'index.html' for client-side routing
try_files {path} /index.html
}
# enable gzipping responses
encode gzip
}I had to do those two handle blocks, because try files was interupting my reverse proxy.
So it was always either my reverse proxy working and not my client side routing or the other way around.
By working with the two blocks, that got resolved.
I tried deploying it to Railway. Client side routing works, but the call to the backend via the reverse proxy takes a long time when i change this line
Before
handle /api/* {
reverse_proxy localhost:3001
}After
handle /api/* {
reverse_proxy {$BACKEND_BASE_URL}
}I have set the BACKEND_BASE_URL variable in my FE, but it doesn't work …

a year ago
may I ask why the reverse proxy is needed? simply have your frontend call the public domain of the backend, the reverse proxy is very likely needless complications
Would've liked to hide my backend domain. But it's more of an optimization actually.
a year ago
I personally don't see that as an optimization, but as long as it's working!
a year ago
!s
Status changed to Solved brody • 12 months ago












