How to set up the server as a proxy via rewrite rules through .htaccess

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.

Add the remote proxy as an external app

1. Log in to the LSWS GUI. The default address is https://IP:7080 or http://ip:7080 for ealier verion

2. Go to Configuration → Server → External App tab and hit Add. The external app can also be added to virtual host level. Adding to server level is recommended.

3. Select Web Server from the drop down list.

4. On the following page, fill in the following and hit Save:

  
  * 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.
  * 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.
  * Retry Timeout (secs) - The period of time that the server waits before retrying a request.

5. After saving, restart LSWS

Use rewrite rules or mod_proxy directive in .htaccess

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:
RewriteRule (.*) http://<EXTERNAL_APP_NAME>/$1 [P]
  • Note: be sure to set it as a 1:1 copy of the External App made previously.

In the above example in .htaccess:

RewriteRule (.*) http://Local_proxy/$1 [P]

Example 2: Proxy to a site on the backend with a different domain name.

RewriteRule ^(.*)$ http://<EXTERNAL_APP_NAME>/$1 [P,E=Proxy-Host:www.example.com]

The URL for the vhost should now act as a proxy for the External App Web Server that was just set up.

For the above example in .htaccess:

RewriteRule ^(.*)$ http://Local_proxy/$1 [P,E=Proxy-Host:www.example.com]

Testing

From the browser,

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.

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: ).

 
litespeed_wiki/proxy/lsws-as-a-proxy-rewrite.txt · Last modified: 2018/04/09 20:42 by Jackson Zhang