Help:How To Set Up Reverse Proxy With Template

From OpenLiteSpeed
Jump to: navigation, search

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 and setting up the virtual host. The final part gives examples of how to add different kinds of functionality to the reverse proxy.

Note: Any setting not specifically noted in this walkthrough can be left as the default.

Import the reverse proxy template

1. Download the reverse proxy template file.

2. Save this file to /usr/local/lsws/conf/templates/rproxy.xml

3. Make sure user lsadm has write permission.

chown lsadm /usr/local/lsws/conf/templates/rproxy.xml

4. Add the template on the WebAdmin console.

WebAdmin console > Configuration > Virtual Host Templates > Add
Template Name: reverseproxy (name it anything as long as it does not conflict with existing templates)
Template File: $SERVER_ROOT/conf/templates/rproxy.xml
Mapped Listeners: Default
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 above 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.)

5. Graceful restart to apply changes. (Actions > Graceful Restart)

Set up virtual hosts using this template

1. Create a member virtual host from the template.

WebAdmin console > Configuration > Virtual Host Templates > reverseproxy > Member Virtual Hosts > Add
Virtual Host Name: proxy1
Domain: proxy.test.com

2. 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

3. 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.
WebAdmin console > Configuration > Virtual Host Templates > reverseproxy > proxy1 > Instantiate

4. Point the proxy to your backend server.

(You also might as well rename the proxy external application while you're here.)
WebAdmin console > Configuration > Virtual Hosts > proxy1 > External App > Proxy_$VH_NAME > Edit
Name: Proxy_proxy1 (or anything else)
Address: xxx.xxx.xxx.xx:xx (the IP address and port of your backend server)

5. Graceful restart to apply changes. (Actions > Graceful Restart)

Your new proxy vhost is now live and sending traffic to the backend.

Added functionality

Serve static content with OpenLiteSpeed, proxy dynamic content to a backend server

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.

WebAdmin console > Configuration > Virtual Hosts > Rewrite
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 with a different domain name

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.

WebAdmin console > Configuration > Virtual Hosts > Rewrite
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:

WebAdmin console > Configuration > Virtual Hosts > Rewrite
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.