Configure Apache reverse proxy

Apache's reverse proxy is an act of an Apache webserver providing content from other webserver transparently. This is useful in many instances such as caching and mirroring, but it's mostly used to serve websites that are hosted behind NAT or a firewall. A reverse proxy server routes connection addressed to the internal server, and the client sees the reverse proxy server itself as the origin server.

For example, Apache can be configured to serve URL's such as http://www.example.com/webapp to actually get the content from http://192.168.0.10/myapp, which is hosted from an internal network. This happens transparently and the user initially requesting http://www.example.com/webapp need not to be aware of what happens in the background.

1) Enable proxy modules

Apache require 2 modules enabled for this reverse proxy to work. These modules are proxy and proxy_http. To enable them, run the following command at the terminal.

$ sudo a2enmod proxy_http
Considering dependency proxy for proxy_http:
Enabling module proxy.
Enabling module proxy_http.

That will enable both proxy and proxy_http, as proxy module is actually a dependency to the proxy_http module. This is confirmed by the output of the above command.

Once the module is enabled, you can start configuring the reverse proxy settings.

2) Configure reverse proxy

From the previous example, we would like our public facing webserver at http://www.example.com to host our internal site of http://192.168.0.10/myapp to be available as http://www.example.com/webapp

You'll need to add the following lines to your Apache's configuration file.

For Ubuntu, the file could be /etc/apache2/sites-enabled/000-default

ProxyRequests Off
 
ProxyPass /webapp http://192.168.0.10/myapp
ProxyPassReverse /webapp  http://192.168.0.10/myapp

3) Restart Apache

Apache will have to be restarted for the changes to work.

Sign up for Complete Apache HTTP Server Course and many other great courses on Udemy to learn more.


Written by Mohd Shakir Zakaria. Last updated on 2019-02-12