Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
litespeed_wiki:lslb:advanced-proxy [2020/01/09 21:05]
qtwrk created
litespeed_wiki:lslb:advanced-proxy [2020/11/18 15:45] (current)
Lisa Clarke Redirect to new Documentation Site
Line 1: Line 1:
-====== LiteSpeed ADC Advanced Reverse Proxy Usage ====== +~~REDIRECT>https://docs.litespeedtech.com/products/lsadc/settings/~~
- +
-In previous [[litespeed_wiki:​lslb:​basic_config|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: +
- +
- +
-<code><?php +
-echo "​SCRIPT_FILENAME"​.$_SERVER["​SCRIPT_FILENAME"​]."<​br>"​ ; +
-echo "​SERVER ADDR: "​.$_SERVER["​SERVER_ADDR"​]."<​br>";​ +
-echo "​HTTP_HOST:​ "​.$_SERVER['​HTTP_HOST'​]."<​br>";<​/code> +
- +
-This allows us to determine which backend and what file we are requesting with. +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-1.jpg|}} +
- +
-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. +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-2.jpg|}} +
- +
- +
-We have two methods to do this, via ''​RewriteRule''​ or ''​Context''​ +
- +
-===== RewriteRule ===== +
- +
-In ADC **Virtual host** ---> **RewriteRule** tab , enable rewrite rule and add rule: +
- +
-<​code>​ +
-RewriteRule ^(.*)$ http://​adc2/​$1 [P,​E=Proxy-Host:​adc2.litespeed.dev] +
-</​code>​ +
- +
-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. +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-3.jpg|}} +
- +
-Now save and restart ADC , check the page again , you will see it is proxied to ''​adc2.litespeed.dev''​ +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-4.jpg|}} +
- +
-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: +
- +
-<​code>​ +
-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] +
-</​code>​ +
- +
-Result: +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-5.jpg|}} +
- +
- +
- +
-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: +
- +
-<​code>​ +
-RewriteCond %{QUERY_STRING} rewrite +
-RewriteRule ^(.*)$ http://​adc3/​new-uri.php$1 [P,​E=Proxy-Host:​adc3.litespeed.dev] +
-</​code>​ +
- +
-Result: +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-6.jpg|}} +
- +
- +
- +
-===== 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 +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-7.jpg?​600|}} +
- +
-**URI**: ''/​index.php''​ +
- +
-**Cluster**:​ ''​adc2''​ +
- +
-**Enable Rewrite**: ''​Yes''​ +
- +
-**Rewrite Rules**:  +
-<​code>​ +
-RewriteRule ^(.*) - [E=Proxy-Host:​adc2.litespeed.dev] +
-</​code>​ +
- +
-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` +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-8.jpg?​600|}} +
- +
-**URI**: ''/​adc3''​ +
- +
-**Cluster**:​ ''​adc3''​ +
- +
-**Enable Rewrite**: ''​Yes''​ +
- +
-**Rewrite Rules**:  +
-<​code>​ +
-RewriteRule ^(.*)$ http://​adc3/​new-uri.php$1 [P,​E=Proxy-Host:​adc3.litespeed.dev] +
-</​code>​ +
- +
-===== 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 +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-9.jpg?​600|}} +
- +
-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 +
- +
-<​code>​ +
-[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'​ +
-</​code>​ +
- +
-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 [[https://​regexr.com/|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 [[https://httpd.apache.org/docs/​current/​en/​mod/​mod_rewrite.html|Apache doc]] +
- +
  • Admin
  • Last modified: 2020/01/09 21:05
  • by qtwrk