Skip to content

Commit 1edf986

Browse files
ivictborCopilot
andcommitted
docs: update websocket and deployment documentation for Nginx configuration
Co-authored-by: Copilot <copilot@github.com>
1 parent 8d8f7ce commit 1edf986

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

adminforth/documentation/docs/tutorial/03-Customization/16-websocket.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
AdminForth provide own build-in websocket interface which allows to stream some data to frontend from backend.
55

6+
If in production you run through a reverse proxy such as Nginx, ensure websocket upgrade is enabled on the `/afws` path under your AdminForth base URL, otherwise websocket will not work. See [Deploy in Docker > Nginx version](/docs/tutorial/deploy#nginx-version).
7+
68
In two words, to subscribe to a topic from any frontend component you need to do next
79

810
```javascript

adminforth/documentation/docs/tutorial/04-deploy.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,18 @@ docker compose -p stack-my-app -f compose.yml up -d --build --remove-orphans --w
115115
If you want to deploy your AdminForth application to a sub-folder like `https://mydomain.com/admin` you
116116
should do the following:
117117

118-
1) Open `index.ts` file and change `ADMIN_BASE_URL` constant to your subpath:
118+
1) Open `index.ts` file and set the same subpath in `ADMIN_BASE_URL` and `baseUrl`:
119119

120120
```ts title='./index.ts'
121121
//diff-remove
122122
const ADMIN_BASE_URL = '';
123123
//diff-add
124124
const ADMIN_BASE_URL = '/admin/';
125+
126+
export const admin = new AdminForth({
127+
baseUrl: ADMIN_BASE_URL,
128+
...
129+
});
125130
```
126131

127132
2) Open `compose.yml` file and change `traefik.http.routers.adminforth.rule` to your subpath:
@@ -141,11 +146,18 @@ Now you can access your AdminForth application by going to `https://mydomain.com
141146

142147
If you want to automate the deployment process with CI follow [our docker - traefik guide](https://devforth.io/blog/onlogs-open-source-simplified-web-logs-viewer-for-dockers/)
143148

144-
# Nginx version
149+
## Nginx version
145150

146-
If you are using Nginx instead of traefik, here is siple proxy pass config:
151+
If you are using Nginx instead of traefik, proxy both regular HTTP traffic and AdminForth websocket endpoint.
147152

148-
```
153+
AdminForth websocket always uses `<baseUrl>/afws`, where `<baseUrl>` is the same value you pass to `baseUrl: ADMIN_BASE_URL` in `index.ts`.
154+
155+
- If `ADMIN_BASE_URL = ''`, use `/afws`.
156+
- If `ADMIN_BASE_URL = '/admin/'`, use `/admin/afws`.
157+
158+
For root deployment, the config can look like this:
159+
160+
```nginx
149161
server {
150162
listen 80;
151163
server_name demo.adminforth.dev;
@@ -163,6 +175,17 @@ server {
163175
gzip_min_length 2000;
164176
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
165177
178+
location /afws {
179+
proxy_http_version 1.1;
180+
proxy_read_timeout 220s;
181+
proxy_set_header Upgrade $http_upgrade;
182+
proxy_set_header Connection "upgrade";
183+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
184+
proxy_set_header Host $http_host;
185+
proxy_redirect off;
186+
proxy_pass http://127.0.0.1:3500;
187+
}
188+
166189
location / {
167190
proxy_read_timeout 220s;
168191
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@@ -173,6 +196,35 @@ server {
173196
}
174197
```
175198

199+
If you deploy AdminForth under a subpath, use the same prefix in Nginx locations. For example, when `ADMIN_BASE_URL = '/admin/'`, the websocket path becomes `/admin/afws`:
200+
201+
```nginx
202+
server {
203+
...
204+
205+
location /admin/afws {
206+
proxy_http_version 1.1;
207+
proxy_read_timeout 220s;
208+
proxy_set_header Upgrade $http_upgrade;
209+
proxy_set_header Connection "upgrade";
210+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
211+
proxy_set_header Host $http_host;
212+
proxy_redirect off;
213+
proxy_pass http://127.0.0.1:3500;
214+
}
215+
216+
location /admin/ {
217+
proxy_read_timeout 220s;
218+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
219+
proxy_set_header Host $http_host;
220+
proxy_redirect off;
221+
proxy_pass http://127.0.0.1:3500;
222+
}
223+
}
224+
```
225+
226+
If websocket upgrade is not configured on the correct `<baseUrl>/afws` path, realtime AdminForth features will not work behind Nginx.
227+
176228
# Environment variables best practices
177229

178230
Use `.env` file for sensitive variables like `OPENAI_API_KEY` locally.

0 commit comments

Comments
 (0)