Help:Proxying with Rewrite Rules

From OpenLiteSpeed
Jump to: navigation, search

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 > Configuration > Virtual Hosts > Add

Basic
=====
 Base
  Virtual Host Name: proxy-vhost1
  Virtual Host Root: $SERVER_ROOT/proxy/
  Config File: $VH_ROOT/conf/vhconf.xml
 Connection
  Max Keep-Alive Requests: 1000
 Security
  Follow Symbolic Link: No
  Enable Scripts/ExtApps: No
  Restrained: Yes
General
=======
 General
  Document Root: $VH_ROOT/html/

Save and graceful restart to apply changes.

Add a rewrite rule in the vhost to send traffic to the proxy external application

WebAdmin console > Configuration > 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 > Configuration > 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