Setting up OpenLiteSpeed as a Reverse Proxy¶
OpenLiteSpeed can be set up as a transparent reverse proxy to any backend web server or application server that supports HTTP. OpenLiteSpeed proxies to other servers by setting them as external applications.
Once you have set up your web server external application, you will have to set which traffic OpenLiteSpeed should send to that external application. This can be done in a number of ways: via rewrite rules, contexts, or script handlers. The last step is to map the proxy virtual host to your listeners.
Create a Web Server External Application¶
You can do this at either the server level or the virtual host level.
To set up a server-level external application, navigate to Server Configuration > External App > Add > Type in the WebAdmin Console.
When updating the settings, the most important thing is Address. Set your backend server up to listen on this address and port. For simplicity, we've set our copy of Apache to listen on port 8080.
- Name:
apache
- Address:
localhost:8080
- Max Connections:
100
- Initial Request Timeout:
60
- Retry Timeout:
0
- Response Buffering:
No
Now you have OpenLiteSpeed ready to proxy to your backend server. The next step is to set up rewrite rules, contexts, or script handlers so that OpenLiteSpeed knows which traffic to send to this external application.
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.
Configure Proxy¶
For this guide we will use the default Example
virtual host and proxy files to an Apache backend. You can choose one of the following methods to follow:
Rewrite Rules¶
One of the most versatile ways to designate which traffic to send is through rewrite rules. 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.
Navigate to Virtual Host Configuration > Example > Rewrite > Rewrite Rules > Add and add a Rewrite Rule.
If you're using IP-based virtual hosting on your backend (each domain has its own IP), then you can use rewrite rules as follows:
REWRITERULE ^(.*)$ HTTP://apache:8080/$1 [P]
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. This example assumes you have set up both port 80(apachehttp
) and 443(apachehttps
) External Applications:
RewriteCond %{HTTPS} !=on
REWRITERULE ^(.*)$ HTTP://apachehttp/$1 [P,L,E=PROXY-HOST:WWW.EXAMPLE1.COM]
RewriteRule ^(.*)$ HTTPS://apachehttps/$1 [P,L,E=PROXY-HOST:WWW.EXAMPLE1.COM]
Notes
apache
,apachehttp
, andapachehttps
are the names of proxy (web server) external applications you have presumably created. If your proxies have other names, use them instead.- For 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.
- Add
https://
in front if the external web server uses HTTPS. Port is optional if the external web server uses the standard ports.
Contexts¶
Proxying with contexts allows you to add other context functionality on top of your proxying. Using a proxy context to designate which traffic to proxy has the advantage of allowing you to easily set aside a location on your site to be proxied.
Navigate to Virtual Host Configuration > Example > Add > Type > Proxy, and add a Proxy context.
With the following settings, we will be proxying all the locations on this site to the web server internal app.
- URI:
/
- Web Server:
[Server Level]: apache
Script Handlers¶
Setting up a script handler mapped to your web server external application allows you to easily proxy certain types of requests (determined by the suffix used in the request). This can be a simple way to set up OpenLiteSpeed to handle most content, but send requests for certain special content (Java scripts or scripts that require Apache, to name two examples) to a different backend.
Navigate to Virtual Host Configuration > Example > Script Handler > Add and add a Script Handler
- Suffixes :
html
- Handler Type:
Web Server
- Handler Name:
apache
Map the Proxy vhost to Your Listener(s)¶
Navigate to Listeners > Example Listener > Virtual Host Mappings > Add. (Replace Example Listener
with the name of your own listener.)
- Virtual:
Host: proxy-vhost
- Domain:
proxy-vhost.domain.com