Skip to content

Set up OpenLiteSpeed as a Reverse Proxy for a Node.js App

In this guide, we will set up OpenLiteSpeed (OLS) as a reverse proxy for a Node.js application. In our example, we will use the Docker compose method. If you already have an app started as a container, then you can skip right to Set up OpenLiteSpeed as a Proxy.

Set up Node.js

Download the OLS Docker stack and change to the ols-docker-env directory:

git clone https://github.com/litespeedtech/ols-docker-env.git; cd ols-docker-env

Follow the official Docker instructions to create a simple Node.js application.

The folder name will be node-docker, and the structure will look like this:

.
├── LICENSE
├── README.md
├── acme
├── bin
├── data
│   └── db
├── docker-compose.yml
├── logs
├── lsws
│   ├── admin-conf
│   └── conf
└── node-docker
    ├── Dockerfile
    ├── node_modules
    ├── package.json
    └── server.js

Test the application:

curl --request POST \
  --url http://localhost:8000/test \
  --header 'content-type: application/json' \
  --data '{"msg": "testing" }'

The output should look like this:

{"code":"success","meta":{"total":3,"count":3},"payload":[{"id":"27b1a4fd-7fa0-45ec-90de-37771a98a193","createDate":"2022-04-08T06:54:30.225Z"},{"id":"59dad706-e6c7-4bc8-af6e-b2a97c05abd2","createDate":"2022-04-08T06:54:30.233Z"},{"msg":"testing","id":"ccd2da12-f28f-4e52-b6aa-418aa3a44184","createDate":"2022-04-08T06:54:34.918Z"}]}

Edit docker-compose.yml and include the following content:

version: '3'
services:
  litespeed:
    image: litespeedtech/openlitespeed:${OLS_VERSION}-${PHP_VERSION}
    env_file:
      - .env
    volumes:
        - ./lsws/conf:/usr/local/lsws/conf
        - ./lsws/admin-conf:/usr/local/lsws/admin/conf
        - ./bin/container:/usr/local/bin
        - ./sites:/var/www/vhosts/
        - ./acme:/root/.acme.sh/
        - ./logs:/usr/local/lsws/logs/
    ports:
      - 80:80
      - 443:443
      - 443:443/udp
      - 7080:7080
    restart: always
    environment:
      TZ: ${TimeZone}
    network_mode: "host"

  nodejs:
    image: nodejs
    build: node-docker/.
    environment:
      - NODE_ENV=production
    ports:
      - 8000:8000

Set up OpenLiteSpeed as a Proxy

Using the official OLS as Reverse Proxy guide, we can set up OLS by creating an external app, and adding rewrite rules.

Create a Web Server External Application

Log in to the OLS WebAdmin Console. To set up a server-level external application, navigate to Server Configuration > External App > Add > Type. Give it the following values:

  • Name = nodejs
  • Address = localhost:8000
  • Max Connections = 100
  • Initial Request Timeout = 60
  • Retry Timeout = 0
  • Response Buffering = No

Add Proxying Rewrite Rules

Navigate to Virtual Host Templates > docker > Rewrite > Rewrite Rules > Add and add a set of rewrite rules, like these:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ HTTP://nodejs/$1 [P,L,E=PROXY-HOST:www.example.com]
RewriteRule ^(.*)$ HTTPS://nodejs/$1 [P,L,E=PROXY-HOST:www.example.com]
Replace the URL www.example.com with the address of your OLS server.

Verify

Use curl command to check the server is served by LiteSpeed.

curl -I https://www.example.com/test

The output should look like this:

HTTP/2 200
x-powered-by: Express
access-control-allow-origin: *
content-type: application/json; charset=utf-8
etag: W/"1b3-MxQbaP1S3gmyG6akl7lrj8F0CMY"
server: LiteSpeed
alt-svc: h3=":443"; ma=2592000, h3-29=":443"; ma=2592000, h3-Q050=":443"; ma=2592000, h3-Q046=":443"; ma=2592000, h3-Q043=":443"; ma=2592000, quic=":443"; ma=2592000; v="43,46"

Last update: April 13, 2022