Reverse Proxy Template¶
Templates are an easy way to set up OpenLiteSpeed for various uses. In the following article we will guide you through the steps to use a template to set up a virtual host that will allow OpenLiteSpeed to function as a reverse proxy for another server on the back end.
This tutorial has three parts. The first two parts explain how to set up a basic reverse proxy:
- importing the template to your server
- setting up the virtual host.
The final part gives examples of how to add different kinds of functionality to the reverse proxy.
Tip
Any setting not specifically noted in this walkthrough can be left as the default.
Import the Reverse Proxy Template¶
Download the reverse proxy template file and save it to /usr/local/lsws/conf/templates/rproxy.xml
Make sure user lsadm
has write permission:
chown lsadm /usr/local/lsws/conf/templates/rproxy.xml
Navigate to WebAdmin console > VHost Templates and click Add to add the template on the WebAdmin console.
Set the following values:
Template Name: reverseproxy
(name it anything you like as long as it does not conflict with existing templates) Template File: $SERVER_ROOT/conf/templates/rproxy.xml
Mapped Listeners: Default
Graceful restart (via Actions > Graceful Restart) to apply the changes.
Set up Virtual Hosts Using This Template¶
There are five steps required to configure virtual hosts to use the reverse proxy template:
- Create a member virtual host from the template
- Create directories for the new vhost
- Instantiate the proxy virtual host
- Point the proxy to your backend server
- Graceful restart
Create a member virtual host from the template.¶
In the Webadmin Console, navigate to VHost Templates > Reverseproxy > Member Virtual Hosts and add a new one. Set the following values:
- Virtual Host Name:
proxy1
- Domain:
proxy.example.com
Note
In order for the server to direct traffic to the proper vhost, you must have listeners set up to listen on the proper IP and port. When you use a virtual host template, you do not have to define listener-to-virtual host mappings in the listener settings. Instead you set the mapped listeners when adding a template.
In the template settings, we have used the listener Default
, which is set up to listen on port 8088 of ANY
IP by default. You must adjust your listener settings to make sure there are listeners on the IP and ports you need.
The easiest way to do this is for most sites to just change the port setting to 80
for the Default
listener in WebAdmin console > Configuration > Listeners > Default > General. This gives you a listener listening on port 80 of all IPs.
Create directories for this vhost¶
As the root user, run the following commands:
mkdir /usr/local/lsws/proxy1
mkdir /usr/local/lsws/proxy1/{html,logs,conf}
chown lsadm:lsadm /usr/local/lsws/proxy1/conf
Instantiate the proxy virtual host.¶
A template's member vhosts share external applications. If you are proxying for more than one virtual host, you will not want the proxies sharing external apps as that could cause traffic on one site to unduly affect another site. If you instantiate the member vhost (which separates it from the template and makes it its own standalone virtual host) the vhost will have its own proxy external application.
Navigate to WebAdmin console > VHost Templates > Reverseproxy > proxy1 and click Instantiate
Point the proxy to your backend server.¶
You also might as well rename the proxy external application while you're here.
Navigate to WebAdmin console > Virtual Hosts > proxy1 > External App > Proxy_$VH_NAME and click Edit. Set the following values:
- Name:
Proxy_proxy1
(or whatever you like) - Address: the IP address and port of your backend server
Graceful restart¶
Navigate to Actions > Graceful Restart to apply the changes.
Your new proxy vhost is now live and sending traffic to the backend.
Added Functionality¶
Serve Static Content with OLS¶
One of the most common reasons to use a reverse proxy is because you have some dynamic content that can only be served by Apache (or that you are more comfortable using Apache for), but you would like to have a faster server software (like OpenLiteSpeed) serving the rest of your content. You can do this by adding a simple rewrite rule.
Navigate to WebAdmin console > Virtual Hosts > your virtual host > Rewrite and set the following values:
- Enable Rewrite:
Yes
- Log Level:
0
- Rewrite Rules:
RewriteRule /(.*.php)$ http://Proxy_proxy1/$1 [P]
Note
Proxy_proxy1
is the name of your external application. The above rewrite rule will send all requests for files with the suffix .php
to the proxy external application, which will then send them to the backend server you've specified. You can of course modulate this rule to look for other suffixes or only in certain places. Please see the Apache mod_rewrite documentation for further guidance in writing rewrite rules.
Proxy to a Site on the Backend¶
You can use your reverse proxy to send traffic to a site other than the domain name originally requested by the client. To do so, you need send the request to the backend with a different header so the backend server knows which domain you want to send the traffic to.
Navigate to WebAdmin console > Virtual Hosts > your virtual host > Rewrite and set the following values:
- Enable Rewrite: Yes
- Log Level: 0
- Rewrite Rules:
RewriteRule /(.*)$ http://Proxy_proxy1/$1 [P,E=Proxy-Host:www.example.com]
Note
Proxy_proxy1
is the name of your external application. www.example.com
is the domain name on the backend server you intend to send the request to.
Combine Functionalities¶
As the above functionalities are enabled through rewrite rules, they are a cinch to combine. All you have to do is combine the rewrite rules:
Navigate to WebAdmin console > Virtual Hosts > your virtual host > Rewrite and set the following values:
- Enable Rewrite: Yes
- Log Level: 0
- Rewrite Rules:
RewriteRule /(.*.php)$ http://Proxy_proxy1/$1 [P,E=Proxy-Host:www.example.com]
Note
Proxy_proxy1
is the name of your external application. www.example.com
is the domain name on the backend server you intend to send the request to.