Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
litespeed_wiki:proxy:lsws-as-a-proxy-rewrite [2018/03/05 15:07]
Johathan Kagan [User rewrite rules or mod_proxy directive in .htaccess]
litespeed_wiki:proxy:lsws-as-a-proxy-rewrite [2021/08/09 14:00] (current)
Jackson Zhang [How to set up the server as a proxy via rewrite rules through .htaccess]
Line 1: Line 1:
 ====== How to set up the server as a proxy via rewrite rules through .htaccess ====== ====== How to set up the server as a proxy via rewrite rules through .htaccess ======
 +LiteSpeed 6.0 and above do support ''​ProxyPass''​ directive as well as rewrite rule-based proxy with ''​[P]''​ without setting up an external app.  ''​ProxyAddHeaders on|off''​ is also supported starting from 6.0.7.
  
-LiteSpeed ​works with Apache ​mod_proxy directives, but there is an extra step to add an external app of the proxy in LSWS configuration to make it work fully.  ​+LiteSpeed ​5.4.x and earlier version do not support ''​ProxyPass'',​ ''​ProxyPassReverse''​ and other mod_proxy directives(starting from LSWS 5.3.6 ''​ProxyPass''​ and-''​ProxyPassMatch''​ support for AJP backend have been added but not supported for non-AJP backend). However, you can use rewrite rules with ''​[P]''​ and set up an external app of the proxy in LSWS configuration to make it work fully.  ​
  
 ===== Add the remote proxy as an external app ===== ===== Add the remote proxy as an external app =====
Line 16: Line 17:
 {{ :​litespeed_wiki:​proxy:​add-proxy-webserver-handler2.png?​800 |}} {{ :​litespeed_wiki:​proxy:​add-proxy-webserver-handler2.png?​800 |}}
     ​     ​
-    * Name - Any unique external app name. It should not start with <​code>​http://</​code>​. As the example, ''​Local_proxy''​. +    * Name - Any unique external app name. It should not start with "http://". As the example, ''​Local_proxy''​. 
-    * Address - The IP:PORT or UDS socket of the backend server. It cannot be a domain name. As the example,​front end is on port ''​443'',​ and backend on ''​ip:​80''​. Hence enter backend ''​ip:​80''​ here.+    * Address - The IP:PORT or UDS socket of the backend server. It cannot be a domain name. As the example,​front end is on port ''​443'',​ and backend on ''​ip:​80''​. Hence enter backend ''​ip:​80''​ here. please remember do not add http:// or https://.
     * Max Connections - The maximum number of concurrent connections to this Web Server at any time.     * Max Connections - The maximum number of concurrent connections to this Web Server at any time.
     * Initial Request Timeout (secs) - The maximum time in seconds the server will wait for a response.     * Initial Request Timeout (secs) - The maximum time in seconds the server will wait for a response.
Line 24: Line 25:
 5. After saving, restart LSWS 5. After saving, restart LSWS
  
-===== Use rewrite rules or mod_proxy directive ​in .htaccess=====+===== Use rewrite rules in .htaccess =====
 ==== Example 1: Proxy to a site on the backend with the same domain name.==== ​ ==== Example 1: Proxy to a site on the backend with the same domain name.==== ​
     * In Shell or an Editor, edit the .htaccess file normally located in virtualhost document root and add the following:     * In Shell or an Editor, edit the .htaccess file normally located in virtualhost document root and add the following:
Line 40: Line 41:
 For the above example in .htaccess: For the above example in .htaccess:
 <​code>​RewriteRule ^(.*)$ http://​Local_proxy/​$1 [P,​E=Proxy-Host:​www.example.com]</​code>​ <​code>​RewriteRule ^(.*)$ http://​Local_proxy/​$1 [P,​E=Proxy-Host:​www.example.com]</​code>​
 +
 +==== Example 3: Proxy to a site on the same server ====
 +If acting as a proxy to a site on the same server, you can use ''​127.0.0.1:<​port>''​ without manually creating an external app. The rewrite rule proxy target will be created automatically if the target is using ''​127.0.0.1:<​port>''​. When using a domain, even if the domain is hosted on the same server, the external application won't be created automatically. This will lead to a 500 status code as the server cannot find the external application. You will need to create an external app manually, in this case.  ​
 +
 +For example, ''​www.example1.com''​ is hosted on the same server. If you want to run the following proxy with domain, you will need to create an external app:
 +<​code>​
 + ​RewriteEngine On
 + ​RewriteRule ^(.*)$ https://​www.example1.com:​5000/​$1 [P,L]
 +</​code>​
 +
 +Alternatively,​ simply using the following code, an external app will be created by the server automatically:​
 +<​code>​
 + ​RewriteEngine On
 + ​RewriteRule ^(.*)$ https://​127.0.0.1:​5000/​$1 [P,L]
 +</​code>​
 +
 +==== Example 4: cPanel webmail-like proxy ====
 +cPanel'​s ''​www.domain.com/​webmail''​ proxy loads one installation at ''<​nowiki>​http://​127.0.0.1/​rainloop/</​nowiki>''​.
 +
 +An Apache user without a control panel who wants to copy this behavior may achieve it with ''​ProxyPass'',​ like so:
 +  ProxyPass "/​webmail/"​ "​http://​127.0.0.1/​rainloop/"​
 +
 +But LiteSpeed doesn'​t support ''​ProxyPass''​ in this case. Instead, a LiteSpeed user may simply use the following rewrite rule in the virtual host's Apache configuration. No need to create an external app:
 +  RewriteRule /​webmail/​(.*) http://​127.0.0.1/​rainloop/​$1 [P]
  
 ===== Testing ===== ===== Testing =====
 From the browser, ​ From the browser, ​
  
-1. Firstly Check backend: ​ http://​www.example.com to or by curl command to make sure backend ​runnning ​ok.+1. Firstly Check backend: ​ http://​www.example.com to or by curl command to make sure backend ​running ​ok.
  
 2. Secondly, Check front end: example front end is on port 443, visit https://​www.example.com and it should proxy/​return backend correctly. ​ 2. Secondly, Check front end: example front end is on port 443, visit https://​www.example.com and it should proxy/​return backend correctly. ​
 +
 +===== Troubleshooting =====
 +
 +If you see the similar error log as the following:
 +  [REWRITE] Proxy target is not defined on external application list, please add a 'web server'​ with name '​https://​Local_proxy'​
 +
 +It means the proxy "​Local_proxy"​ not defined on external application. You need to create a web server proxy on the external application named "​Local_proxy"​ (without http:// or https:// ).
  • Admin
  • Last modified: 2018/03/05 15:07
  • by Johathan Kagan