Soft fork for a caddy reverse template for railway
1# [Caddy](https://caddyserver.com/) Frontend & Backend Reverse Proxy 2 3>soft fork to change `handle_path` to `handle`. Original is found at https://github.com/railwayapp-templates/caddy-reverse-proxy 4 5**Combine your separate frontend and backend services into one domain!** 6 7### [View the example public project here](https://railway.app/project/35d8d571-4313-4049-9699-4e7db7f02a2f) - Utilizes sleeping frontend and backend services with wake via the private network 8 9Access the frontend from `/*` and access the backend from `/api/*` on the same domain 10 11**Frontend - Vue 3:** https://mysite.up.railway.app/ 12 13**Backend - Go Mux:** https://mysite.up.railway.app/api/ 14 15The proxy configurations are done in the [`Caddyfile`](https://github.com/brody192/reverse-proxy/blob/main/Caddyfile) everything is commented for your ease of use! 16 17When deploying your Reverse Proxy service it will require you to set four service variables: **FRONTEND_DOMAIN** / **FRONTEND_PORT** and **BACKEND_DOMAIN** / **BACKEND_PORT** 18 19**Note:** You will first need to have set a fixed `PORT` variable in both the frontend and backend services before deploying this template. 20 21These are the four template variables that you will be required to fill out during the first deployment of this service, it is highly recommended to use [reference variables](https://docs.railway.app/guides/variables#referencing-another-services-variable). 22 23Example: 24 25``` 26FRONTEND_DOMAIN = ${{Frontend.RAILWAY_PRIVATE_DOMAIN}} 27FRONTEND_PORT = ${{Frontend.PORT}} 28 29BACKEND_DOMAIN = ${{Backend.RAILWAY_PRIVATE_DOMAIN}} 30BACKEND_PORT = ${{Backend.PORT}} 31``` 32 33**Relevant Caddy documentation:** 34 35- [The Caddyfile](https://caddyserver.com/docs/caddyfile) 36- [Caddyfile Directives](https://caddyserver.com/docs/caddyfile/directives) 37- [reverse_proxy](https://caddyserver.com/docs/caddyfile/directives/reverse_proxy) 38 39**Some prerequisites to help with common issues that could arise:** 40 41- Both the frontend and backend need to listen on fixed ports, in my example project I have configured my frontend and backend to both listen on port `3000` 42 - 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` 43 44- Since Railway's internal network is IPv6 only the frontend and backend apps will need to listen on `::` (all interfaces, both IPv4 and IPv6) 45 46 **Start commands for some popular frameworks:** 47 48 - **Gunicorn:** `gunicorn main:app -b [::]:${PORT:-3000}` 49 50 - **Uvicorn:** `uvicorn main:app --host :: --port ${PORT:-3000}` 51 52 - 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 53 54 - **Hypercorn:** `hypercorn main:app --bind [::]:${PORT:-3000}` 55 56 - **Next:** `next start -H :: --port ${PORT:-3000}` 57 58 - **Express/Nest:** `app.listen(process.env.PORT || 3000, "::");`