+20
-15
README.md
+20
-15
README.md
···
1
1
# [Caddy](https://caddyserver.com/) Frontend & Backend Reverse Proxy
2
2
3
-
**Combine your **separate** frontend and backend services into one domain!**
3
+
**Combine your separate frontend and backend services into one domain!**
4
4
5
-
### [View the example public project here](https://railway.app/project/35d8d571-4313-4049-9699-4e7db7f02a2f)
5
+
### [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
6
6
7
7
Access the frontend from `/*` and access the backend from `/api/*` on the same domain
8
8
···
12
12
13
13
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!
14
14
15
-
When deploying your Reverse Proxy service it will require you to set two service variables: **FRONTEND_HOST** and **BACKEND_HOST**
15
+
When deploying your Reverse Proxy service it will require you to set four service variables: **FRONTEND_DOMAIN** / **FRONTEND_PORT** and **BACKEND_DOMAIN** / **BACKEND_PORT**
16
16
17
-
**Note:** You will first need to have set a fixed `PORT` variable in both the frontend and backend services before deploying this template
17
+
**Note:** You will first need to have set a fixed `PORT` variable in both the frontend and backend services before deploying this template.
18
18
19
-
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
19
+
These 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).
20
+
21
+
Example:
20
22
21
23
```
22
-
FRONTEND_HOST = ${{<frontend service name>.RAILWAY_PRIVATE_DOMAIN}}:${{<frontend service name>.PORT}}
23
-
BACKEND_HOST = ${{<backend service name>.RAILWAY_PRIVATE_DOMAIN}}:${{<backend service name>.PORT}}
24
+
FRONTEND_DOMAIN = ${{Frontend.RAILWAY_PRIVATE_DOMAIN}}
25
+
FRONTEND_DOMAIN = ${{Frontend.PORT}}
26
+
27
+
BACKEND_HOST = ${{Backend.RAILWAY_PRIVATE_DOMAIN}}
28
+
BACKEND_HOST = ${{Backend.PORT}}
24
29
```
25
30
26
31
**Relevant Caddy documentation:**
···
31
36
32
37
**Some prerequisites to help with common issues that could arise:**
33
38
34
-
- 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`
39
+
- 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`
35
40
- 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`
36
41
37
-
- Since Railway's internal network is IPv6 only the frontend and backend apps will need to listen on `::` (all interfaces - both IPv4 and IPv6)
42
+
- Since Railway's internal network is IPv6 only the frontend and backend apps will need to listen on `::` (all interfaces, both IPv4 and IPv6)
38
43
39
44
**Start commands for some popular frameworks:**
40
45
41
-
- **Gunicorn:** `gunicorn main:app -b [::]:$PORT`
46
+
- **Gunicorn:** `gunicorn main:app -b [::]:${PORT:-3000}`
42
47
43
-
- **Uvicorn:** `uvicorn main:app --host :: --port $PORT`
48
+
- **Uvicorn:** `uvicorn main:app --host :: --port ${PORT:-3000}`
44
49
45
-
- 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
50
+
- 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
46
51
47
-
- **Hypercorn:** `hypercorn main:app --bind [::]:$PORT`
52
+
- **Hypercorn:** `hypercorn main:app --bind [::]:${PORT:-3000}`
48
53
49
-
- **Next:** `next start -H :: --port $PORT`
54
+
- **Next:** `next start -H :: --port ${PORT:-3000}`
50
55
51
-
- **Express/Nest:** `app.listen(process.env.PORT, "::");`
56
+
- **Express/Nest:** `app.listen(process.env.PORT || 3000, "::");`