Skip to content

Commit e331877

Browse files
committed
Update examples
1 parent 1fd6fd5 commit e331877

File tree

1 file changed

+86
-2
lines changed

1 file changed

+86
-2
lines changed

doc/examples.md

+86-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
3. [Serve PHP files with PHP-FPM and sync local permissions](#-serve-php-files-with-php-fpm-and-sync-local-permissions)
1515
4. [Serve PHP files with PHP-FPM over HTTPS](#-serve-php-files-with-php-fpm-over-https)
1616
5. [Act as a Reverse Proxy for NodeJS](#-act-as-a-reverse-proxy-for-nodejs)
17-
6. [Fully functional LEMP stack with Mass vhosts](#-fully-functional-lemp-stack-with-mass-vhosts)
18-
7. [Docker Compose](#-docker-compose)
17+
6. [Act as a Reverse Proxy for Websocket](#-act-as-a-reverse-proxy-for-websocket)
18+
7. [Fully functional LEMP stack with Mass vhosts](#-fully-functional-lemp-stack-with-mass-vhosts)
19+
8. [Docker Compose](#-docker-compose)
1920

2021

2122

@@ -237,6 +238,89 @@ The following example proxies all HTTP requests to a NodeJS remote backend. You
237238
238239
239240
241+
## 💡 Act as a Reverse Proxy for Websocket
242+
243+
The following example proxies all HTTP requests to a Websocket remote backend. You could also enable SSL on the webserver in order to access the websocket backend via HTTPS.
244+
245+
* **Vhost:** main (default)
246+
* **Backend:** Reverse Proxy (with websocket support)
247+
248+
> 🛈 No files need to be mounted into the webserver, as content is coming from the websocket server.
249+
250+
1. Create a websocket server application
251+
```bash
252+
# Create source directory
253+
mkdir -p src
254+
255+
# websocket server application
256+
cat << EOF > src/index.js
257+
const WebSocket = require("ws");
258+
const wss = new WebSocket.Server({ port: 3000 });
259+
wss.on("connection", (ws) => {
260+
ws.send("hello client, you are connected to me");
261+
ws.on("message", (message) => {
262+
console.log("New message from client: %s", message);
263+
});
264+
});
265+
console.log("WebSocket server ready at localhost:3000");
266+
EOF
267+
268+
# package.json
269+
cat << EOF > src/package.json
270+
{
271+
"name": "node-websocket-example",
272+
"version": "1.0.0",
273+
"main": "index.js",
274+
"devDependencies": {},
275+
"scripts": {
276+
"test": "echo \"Error: no test specified\" && exit 1"
277+
},
278+
"keywords": [],
279+
"author": "",
280+
"license": "ISC",
281+
"description": "",
282+
"dependencies": {
283+
"ws": "^7.5.1"
284+
}
285+
}
286+
EOF
287+
288+
# Startup script
289+
cat << EOF > src/start.sh
290+
#!/bin/sh
291+
npm install
292+
node index.js
293+
EOF
294+
```
295+
2. Start the Websocket server container
296+
```bash
297+
docker run -d -it \
298+
--name websocket \
299+
-v $(pwd)/src:/app \
300+
-w /app \
301+
node:19-alpine sh start.sh
302+
```
303+
3. Start Reverse Proxy
304+
```bash
305+
docker run -d -it \
306+
-p 80:80 \
307+
-e MAIN_VHOST_BACKEND='conf:rproxy:ws:websocket:3000' \
308+
--link websocket \
309+
devilbox/nginx-mainline
310+
```
311+
4. Verify
312+
```bash
313+
# On your host system
314+
npm install -g wscat
315+
wscat --connect localhost
316+
317+
Connected (press CTRL+C to quit)
318+
< hello client, you are connected to me
319+
>
320+
```
321+
322+
323+
240324
## 💡 Fully functional LEMP stack with Mass vhosts
241325
242326
The following example creates a dynamic setup. Each time you create a new project directory below `www/`, a new virtual host is being created.

0 commit comments

Comments
 (0)