Proxying with Rewrite Rules (The most versatile way to proxy — and the simplest for name-based virtual hosting.)
OpenLiteSpeed can be set up as a reverse proxy to a different server on the backend. This is done by designating that backend server as a web server external application in OpenLiteSpeed’s settings. (This is covered in detail in the reverse proxy basics article.) Once that web server external application has been set up, OpenLiteSpeed needs to know which traffic to direct to that external application and thus proxy to the different backend.
One of the most versatile ways to designate which traffic to send is through rewrite rules. In this guide, we will go through how to use a rewrite rule to set up a virtual host to proxy all traffic for a domain. (We are assuming you have already set up a web server external application.) We think rewrite rules are the most convenient way to proxy all traffic for a specific virtual host. Rewrite rules can do far more than just this, though. By editing rewrite rules as specified in Apache’s mod_rewrite documentation, one can direct the server to proxy traffic based on suffix or URI or many other variables.
Create a vhost for the Site to Be Proxied
WebAdmin console > Virtual Hosts > Add
Virtual Host Name: proxy-vhost1 Virtual Host Root: $SERVER_ROOT/proxy/ Config File: $VH_ROOT/conf/vhconf.xml Max Keep-Alive Requests: 1000 Follow Symbolic Link: No Enable Scripts/ExtApps: No Restrained: Yes
Save and graceful restart to apply changes.
Add a Rewrite Rule in the vhost to Send Traffic to the Proxy External Application
WebAdmin console > Virtual Hosts > proxy-vhost1 > Rewrite
For IP-Based Virtual Hosting
If you’re using IP-based virtual hosting on your backend (each domain has its own IP), then you can use the rule as follows:
ENABLE REWRITE: YES REWRITE RULES: REWRITERULE ^/(.*)$ HTTP://VHOST-PROXY1/$1 [P]
Note: “vhost-proxy1” is the name of a proxy (web server) external application you have created.
Note: If you are using IP-based virtual hosting, you will need to set up a different web server external application for each vhost, as each web server external application only reroutes traffic to a single IP address.
For Name-Based Virtual Hosting
If you’re using name-based virtual hosting (many domains using one IP), you need to add another variable to your rewrite rule to signal which backend virtual host you intend the traffic to go to: (This ability is unique to LiteSpeed and our web server as an external application setup.)
ENABLE REWRITE: YES REWRITE RULES: REWRITERULE ^/(.*)$ HTTP://VHOST-PROXY1/$1 [P,E=PROXY-HOST:WWW.EXAMPLE1.COM]
Note: “vhost-proxy1” is the name of a proxy (web server) external application you have created. “www.example1.com” is the domain name of the backend vhost to be proxied.
Map the Proxy vhost to Your Listener(s)
WebAdmin console > Listeners > your listeners > Virtual Host Mappings > Add
Virtual Host: proxy-vhost1 Domain: proxy-vhost1.domain.com
(Optional) Enable Per-Client Throttling for the vhost (to provide HTTP-level anti-DDoS protection)
WebAdmin console > Virtual Hosts > proxy-vhost1 > Basic
Per Client Throttle Static Requests/second: 50 Dynamic Requests/sec: 5 Outbound Bandwidth (bytes/sec): 100K Inbound Bandwidth (bytes/sec): 20K