This is an old revision of the document!
LiteSpeed ADC Advanced Reverse Proxy Usage
In previous basic configuration wiki it explains how to set up a basic reverse proxy, but in that case the request URI needed to be the same in the frontend as the backend, otherwise a 404 Not Found Error would be returned.
In this guide we will give examples for more advanced reverse proxy usage for two scenarios: proxy a frontend URI to different backend URI, or mask the URI.
Prepation
We will use 3 test servers as example , each of them has domain
adc.litespeed.dev
adc2.litespeed.dev
adc3.litespeed.dev
respectively , and adc.litespeed.dev
will be the main server and other two are served for different backend
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 backend to match each domain to its real IP, because ADC will redirect/proxy the request to cluster worker to do the proxy.
We have two methods to do this, via RewriteRule
or Context
RewriteRule
In ADC Virtual host ---> RewriteRule 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 backend domain , otherwise you may get 404 error if hostname doesn't match in 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:
We can also rewrite the URL , we 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 rewrite rule , we take same above 3 examples.
Proxy adc.litespeed.dev/index.php
to adc2.litespeed.dev/index.php
We will configure the context like screenshot
URI: /index.php
Cluster: adc2
Enable Rewrite: Yes
Rewrite Rules:
RewriteRule ^(.*) - [E=Proxy-Host:adc2.litespeed.dev]
We will need to RewriteRule to alter the hostname, otherwise you will have 404 error.
Please note that Context has a dedicated cluster and does NOT support query string , so you may not be able to proxy to different backend by query string as above rewrite rule example.
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 rule , you can also enable rewrite log to help you to analyze to know what result did it eventually rewrite into
In ADC Virtual host ---> RewriteRule 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 , end with result: 1 , any positive value means the condition matches , and 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 rule
and as LiteSpeed ADC is compatible with Apache mod_rewrite syntax , you can also check Apache doc