Differences

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

Link to this comparison view

Both sides previous revision Previous revision
litespeed_wiki:lslb:advanced-proxy [2020/01/20 17:40]
Joshua Reynolds final proofreading revisions
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 the [[litespeed_wiki:​lslb:​basic_config|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: +
- +
- +
-<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 backends that match each domain to its real IP, because ADC will redirect/proxy the request to a 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** ---> **Rewrite** 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 the backend domain, otherwise you may get 404 errors if the hostname doesn'​t match in the 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|}} +
- +
-==== 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: +
- +
-<​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 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:​ +
- +
-{{:​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 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''​ +
- +
-{{:​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 rules, you can also enable rewrite logging to help you to see the breakdown on how the rewrite rules were processed & their results +
- +
-{{:​litespeed_wiki:​lslb:​adc-adv-proxy-9.jpg?​600|}} +
- +
-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: +
- +
-<​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 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 [[https://​regexr.com/|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 [[https://httpd.apache.org/docs/​current/​en/​mod/​mod_rewrite.html|Apache docs]]. +
- +
  • Admin
  • Last modified: 2020/11/18 15:45
  • by Lisa Clarke