Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision Last revision Both sides next revision | ||
litespeed_wiki:lslb:advanced-proxy [2020/01/09 21:05] qtwrk created |
litespeed_wiki:lslb:advanced-proxy [2020/01/20 17:40] Joshua Reynolds final proofreading revisions |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== LiteSpeed ADC Advanced Reverse Proxy Usage ====== | ====== LiteSpeed ADC Advanced Reverse Proxy Usage ====== | ||
- | 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 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. |
- | 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. | + | 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. |
- | ===== Prepation ===== | + | ===== Preparation ===== |
- | We will use 3 test servers as example , each of them has domain | + | We will use 3 test servers as examples with the following domains: |
''adc.litespeed.dev'' | ''adc.litespeed.dev'' | ||
Line 16: | Line 16: | ||
''adc3.litespeed.dev'' | ''adc3.litespeed.dev'' | ||
- | respectively , and ''adc.litespeed.dev'' will be the main server and other two are served for different backend | + | 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: | + | With each server, we create a PHP file with code: |
<code><?php | <code><?php | ||
- | echo "SCRIPT_FILENAME: ".$_SERVER["SCRIPT_FILENAME"]."<br>" ; | + | echo "SCRIPT_FILENAME: ".$_SERVER["SCRIPT_FILENAME"]."<br>"; |
echo "SERVER ADDR: ".$_SERVER["SERVER_ADDR"]."<br>"; | echo "SERVER ADDR: ".$_SERVER["SERVER_ADDR"]."<br>"; | ||
echo "HTTP_HOST: ".$_SERVER['HTTP_HOST']."<br>";</code> | echo "HTTP_HOST: ".$_SERVER['HTTP_HOST']."<br>";</code> | ||
- | This allows us to determine which backend and what file we are requesting with. | + | This allows us to determine which backend and what file we are requesting with: |
{{:litespeed_wiki:lslb:adc-adv-proxy-1.jpg|}} | {{: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. | + | 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|}} | {{:litespeed_wiki:lslb:adc-adv-proxy-2.jpg|}} | ||
- | We have two methods to do this, via ''RewriteRule'' or ''Context'' | + | We have two methods to do this, via ''RewriteRule'' or ''Context'': |
===== RewriteRule ===== | ===== RewriteRule ===== | ||
- | In ADC **Virtual host** ---> **RewriteRule** tab , enable rewrite rule and add rule: | + | In ADC **Virtual Host** ---> **Rewrite** tab, enable rewrite rule and add rule: |
<code> | <code> | ||
Line 45: | Line 45: | ||
</code> | </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. | + | 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|}} | {{: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'' | + | 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|}} | {{:litespeed_wiki:lslb:adc-adv-proxy-4.jpg|}} | ||
Line 55: | Line 55: | ||
Now we can use RewriteCond and RewriteRule to further customize it | 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'' | + | 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: | Ruleset: | ||
Line 71: | Line 71: | ||
{{:litespeed_wiki:lslb:adc-adv-proxy-5.jpg|}} | {{: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'' | |
- | 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: | Ruleset: | ||
Line 90: | Line 90: | ||
===== Context ===== | ===== Context ===== | ||
- | This is pretty much the same as rewrite rule , we take same above 3 examples. | + | 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'' | Proxy ''adc.litespeed.dev/index.php'' to ''adc2.litespeed.dev/index.php'' | ||
- | We will configure the context like screenshot | + | We will configure the context like this screenshot: |
{{:litespeed_wiki:lslb:adc-adv-proxy-7.jpg?600|}} | {{:litespeed_wiki:lslb:adc-adv-proxy-7.jpg?600|}} | ||
Line 109: | Line 109: | ||
</code> | </code> | ||
- | We will need to RewriteRule to alter the hostname, otherwise you will have 404 error. | + | 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. | ||
- | 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. | + | ==== Context Path ==== |
- | This is another example , proxy `adc.litespeed.dev/adc3` to `adc3.litespeed.dev/new-uri.php` | + | 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|}} | {{:litespeed_wiki:lslb:adc-adv-proxy-8.jpg?600|}} | ||
Line 132: | Line 134: | ||
Basically all of these are based on ''RewriteRule''and ''RewriteCond''. | 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 | + | 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|}} | {{:litespeed_wiki:lslb:adc-adv-proxy-9.jpg?600|}} | ||
- | In ADC **Virtual host** ---> **RewriteRule** tab , set **Log Level** to ''9'' | + | 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 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 | + | You will see a log like this: |
<code> | <code> | ||
Line 148: | Line 150: | ||
</code> | </code> | ||
- | The first line , end with result: 1 , any positive value means the condition matches , and negative value means the condition does not match. | + | 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 rule | + | Another useful tool is [[https://regexr.com/|regular expression test tool]], this will also help you to tweak and check your rewrite rules. |
- | 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]] | + | 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]]. |