···11+{ # global options
22+ admin off # theres no need for the admin api in railway's environment
33+ persist_config off # storage isn't persistent anyway
44+ auto_https off # railway handles https for us, this would cause issues if left enabled
55+ log { # runtime logs
66+ format console # set runtime log format to console mode
77+ }
88+ servers { # server options
99+ trusted_proxies static private_ranges # trust railway's proxy
1010+ }
1111+}
1212+1313+:{$PORT} { # site block, listens on the $PORT environment variable, automatically assigned by railway
1414+ log { # access logs
1515+ format console # set access log format to console mode
1616+ }
1717+1818+ reverse_proxy {$FRONTEND_HOST} # proxy all requests for /* to the frontend, configure this variable in the service settings
1919+2020+ # the handle_path directive will strip /api/ from the path before proxying
2121+ # this is needed if your backend's api routes don't start with /api/
2222+ # change paths as needed
2323+ handle_path /api/* { # this strips the /api/ prefix from the uri sent to the proxy address
2424+ reverse_proxy {$BACKEND_HOST} # proxy all requests for /api/* to the backend, configure this variable in the service settings
2525+ }
2626+2727+ # if your backend's api routes do start with /api/ then you wouldn't want to strip the path prefix
2828+ # if so, comment out the above handle_path block, and uncomment this reverse_proxy directive
2929+ # change paths as needed
3030+ # reverse_proxy {$BACKEND_HOST} # configure this variable in the service settings
3131+}
···11+# [Caddy](https://caddyserver.com/) Frontend & Backend Reverse Proxy
22+33+**Combine your **separate** frontend and backend services into one domain!**
44+55+### [View the example public project here](https://railway.app/project/35d8d571-4313-4049-9699-4e7db7f02a2f)
66+77+Access the frontend from `/*` and access the backend from `/api/*` on the same domain
88+99+**Frontend - Vue 3:** https://mysite.up.railway.app/
1010+1111+**Backend - Go Mux:** https://mysite.up.railway.app/api/
1212+1313+The proxy configurations are done in the [`Caddyfile`](https://github.com/brody192/reverse-proxy/blob/main/Caddyfile) everything is commented for your ease of use!
1414+1515+When deploying your Reverse Proxy service it will require you to set two service variables: **FRONTEND_HOST** and **BACKEND_HOST**
1616+1717+**Note:** You will first need to have set a fixed `PORT` variable in both the frontend and backend services before deploying this template
1818+1919+These are the two template variables that you will be required to fill out during the first deployment of this service, replace the respective `<frontend service name>` and `<backend service name>` with the service names as they appear in the Railway project view
2020+2121+```
2222+FRONTEND_HOST = ${{<frontend service name>.RAILWAY_PRIVATE_DOMAIN}}:${{<frontend service name>.PORT}}
2323+BACKEND_HOST = ${{<backend service name>.RAILWAY_PRIVATE_DOMAIN}}:${{<backend service name>.PORT}}
2424+```
2525+2626+**Relevant Caddy documentation:**
2727+2828+- [The Caddyfile](https://caddyserver.com/docs/caddyfile)
2929+- [Caddyfile Directives](https://caddyserver.com/docs/caddyfile/directives)
3030+- [reverse_proxy](https://caddyserver.com/docs/caddyfile/directives/reverse_proxy)
3131+3232+**Some prerequisites to help with common issues that could arise:**
3333+3434+- Both the frontend and backend need to listen on fixed ports, in my Caddyfile I have used port `3000` in the proxy address, and configured my frontend and backend to both listen on port `3000`
3535+ - This can be done by [configuring your frontend and backend apps to listen on the `$PORT`](https://docs.railway.app/troubleshoot/fixing-common-errors) environment variable, then setting a `PORT` service variable to `3000`
3636+3737+- Since Railway's internal network is IPv6 only the frontend and backend apps will need to listen on `::` (all interfaces - both IPv4 and IPv6)
3838+3939+ **Start commands for some popular frameworks:**
4040+4141+ - **Gunicorn:** `gunicorn main:app -b [::]:$PORT`
4242+4343+ - **Uvicorn:** `uvicorn main:app --host :: --port $PORT`
4444+4545+ - Uvicorn does not support dual stack binding (IPv6 and IPv4) from the CLI, so while that start command will work to enable access from within the private network, this prevents you from accessing the app from the public domain if needed, I recommend using [Hypercorn](https://pgjones.gitlab.io/hypercorn/) instead
4646+4747+ - **Hypercorn:** `hypercorn main:app --bind [::]:$PORT`
4848+4949+ - **Next:** `next start -H :: --port $PORT`
5050+5151+ - **Express/Nest:** `app.listen(process.env.PORT, "::");`