|
14 | 14 | 3. [Serve PHP files with PHP-FPM and sync local permissions](#-serve-php-files-with-php-fpm-and-sync-local-permissions)
|
15 | 15 | 4. [Serve PHP files with PHP-FPM over HTTPS](#-serve-php-files-with-php-fpm-over-https)
|
16 | 16 | 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) |
19 | 20 |
|
20 | 21 |
|
21 | 22 |
|
@@ -237,6 +238,89 @@ The following example proxies all HTTP requests to a NodeJS remote backend. You
|
237 | 238 |
|
238 | 239 |
|
239 | 240 |
|
| 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 | +
|
240 | 324 | ## 💡 Fully functional LEMP stack with Mass vhosts
|
241 | 325 |
|
242 | 326 | 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