This is an old revision of the document!
LiteSpeed ADC Advanced Reverse Proxy Usage
In the basic configuration article it explains how to set up a simple reverse proxy, but in that case the request URI would need to be the same in the frontend as the backend, otherwise a 404 Not Found Error would be returned.
Now in this guide we will give examples for more advanced reverse proxy usage for two scenarios: proxying a frontend URI to a different backend URI and masking the URI path.
Preparation
We will use 3 test servers as examples with the following domains:
adc.litespeed.dev
adc2.litespeed.dev
adc3.litespeed.dev
Where adc.litespeed.dev
will be the main server and other two are served for different backends.
With each server, we create a PHP file with code:
<?php echo "SCRIPT_FILENAME: ".$_SERVER["SCRIPT_FILENAME"]."<br>"; echo "SERVER ADDR: ".$_SERVER["SERVER_ADDR"]."<br>"; echo "HTTP_HOST: ".$_SERVER['HTTP_HOST']."<br>";
This allows us to determine which backend and what file we are requesting with:
We have also created 3 cluster backends that match each domain to its real IP, because ADC will redirect/proxy the request to a cluster worker to do the proxy:
We have two methods to do this, via RewriteRule
or Context
:
RewriteRule
In ADC Virtual Host ---> Rewrite tab, enable rewrite rule and add rule:
RewriteRule ^(.*)$ http://adc2/$1 [P,E=Proxy-Host:adc2.litespeed.dev]
where adc2
is the cluster name, and adc2.litespeed.dev
is the backend domain, otherwise you may get 404 errors if the hostname doesn't match in the backend.
Now save and restart ADC, check the page again, you will see it is proxied to adc2.litespeed.dev
Now we can use RewriteCond and RewriteRule to further customize it
For example, we want to proxy request URI that contains string adc2
to adc2.litespeed.dev
and URI contains adc3
to adc3.litespeed.dev
Ruleset:
RewriteCond %{QUERY_STRING} adc2 RewriteRule ^(.*)$ http://adc2/$1 [P,E=Proxy-Host:adc2.litespeed.dev,L] RewriteCond %{QUERY_STRING} adc3 RewriteRule ^(.*)$ http://adc3/$1 [P,E=Proxy-Host:adc3.litespeed.dev,L]
Result:
RewriteRule Path
We can also rewrite the URI path where we will create a file new-uri.php
with same PHP code, this time we will rewrite adc.litespeed.dev/index.php?rewrite
to adc3.litespeed.dev/new-uri.php
Ruleset:
RewriteCond %{QUERY_STRING} rewrite RewriteRule ^(.*)$ http://adc3/new-uri.php$1 [P,E=Proxy-Host:adc3.litespeed.dev]
Result:
Context
This is pretty much the same as with the previous rewrite rules, where we will use the same 3 examples as before.
Proxy adc.litespeed.dev/index.php
to adc2.litespeed.dev/index.php
We will configure the context like this screenshot:
URI: /index.php
Cluster: adc2
Enable Rewrite: Yes
Rewrite Rules:
RewriteRule ^(.*) - [E=Proxy-Host:adc2.litespeed.dev]
We will need to use RewriteRule to alter the hostname, otherwise you will have 404 errors.
Please note that Context has a dedicated cluster and does NOT support query string, so you may not be able to proxy to different backends by query string like the earlier rewrite rule examples.
Context Path
This is another example, proxy adc.litespeed.dev/adc3
to adc3.litespeed.dev/new-uri.php
URI: /adc3
Cluster: adc3
Enable Rewrite: Yes
Rewrite Rules:
RewriteRule ^(.*)$ http://adc3/new-uri.php$1 [P,E=Proxy-Host:adc3.litespeed.dev]
Summary
Basically all of these are based on RewriteRule
and RewriteCond
.
If you are having trouble with rewrite rules, you can also enable rewrite logging to help you to see the breakdown on how the rewrite rules were processed & their results
In ADC Virtual Host ---> Rewrite tab, set Log Level to 9
You can check the rewrite log in server log, by default it’s /usr/local/lslb/logs/error.log
You will see a log like this:
[REWRITE] Rule: Match '/index.php' with pattern '^/index.php', result: 1 [REWRITE] set ENV: 'Proxy-Host:adc3.litespeed.dev' [REWRITE] Source URI: '/index.php' => Result URI: '/new-uri.php'
The first line that ends with result: 1 can be helpful since any positive value means the condition matches and a negative value means the condition does not match.
Another useful tool is regular expression test tool, this will also help you to tweak and check your rewrite rules.
Additionally, as LiteSpeed ADC is compatible with Apache mod_rewrite syntax, you can also check Apache docs.