This post shows students and new users the steps to configure a reverse proxy with the Apache HTTP server. Apache is primarily used as a web server and not as a reverse proxy. However, Apache comes with access to a wide range of powerful extensions to enable reverse proxy. A reverse proxy is a service that sits between the client and the backend servers.
The proxy server takes and directs client requests to the appropriate backend servers. A proxy server can also perform additional tasks such as SSL encryption, caching, compression, and load balancing to ease the load on the backend servers.
Usually a reverse proxy server is used against Node.js, Python, Java, and other popular applications that do not have web server functions enabled. In this case, Apache is usually the proxy server used to handle client requests.
In the following, we will show you how to use Apache as a reverse proxy for most backend applications and servers. We will give you some basic settings that can be used in your environments.
Also, for students and new users learning Linux, the easiest place to start learning is Ubuntu Linux. Ubuntu is the modern open source Linux operating system for desktops, servers, and other devices.
To get started using Apache as a reverse proxy, follow the steps below.
How to use Apache as a reverse proxy server
Before using Apache to function as a reverse proxy, you must enable the necessary modules. These modules will allow Apache to serve as a reverse proxy for backend applications and other hosts.
Run the following commands to enable these Apache modules.
sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_ajp sudo a2enmod rewrite sudo a2enmod deflate sudo a2enmod headers sudo a2enmod proxy_balancer sudo a2enmod proxy_connect sudo a2enmod proxy_html
The basic configuration to allow Apache to serve as a proxy server can be done using the line code below. Configure the server block for the domain and specify a location to the backend server for Apache to send the requests it receives.
<VirtualHost *:*> ServerName example.com ProxyPreserveHost On # Servers to proxy the connection, or; ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ </VirtualHost>
The simple configuration above tells Apache to pass all requests to the / Root of example.com domain to proxy server at http://127.0.0.1:8080.
For example, if a customer requests http://example.com/, Apache will forward the requests to the backend server defined in the ProxyPass and ProxyPassReverse lines: http://127.0.0.1:8080.
There are many advanced settings that can be done with a proxy server. However, the simple setup above gives you a good idea of how a proxy server works.
Proxy servers can also handle requests for non-HTTP servers, such as:
- proxy_fcgi – reverse proxy to a FastCGI server.
- proxy_uwsgi – reverse proxy to a uwsgi server.
- proxy_scgi – reverse proxy to a SCGI server.
A common Apache reverse proxy for non-HTTP hosts is done with the use of PHP-FPM. An example is how Apache serves PHP scripts.
<VirtualHost *:*> ServerName example.com ProxyPreserveHost On # Servers to proxy the connection, or; <FilesMatch .php
gt; # 2.4.10+ can proxy to unix socket SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost" </FilesMatch> </VirtualHost>
You will need to enable some modules for apache2 to work with PHP-FPM:
sudo a2enmod actions fcgid alias proxy_fcgi
How to configure Apache reverse proxy settings
Reverse proxy servers have common options that define how requests to back-end servers or applications are handled and served. It is recommended to use these configuration options with the Apache proxy. In most cases, these headers and parameters should work in all environments where Apache is used as a reverse proxy.
<VirtualHost *:*> ServerName example.com ProxyPreserveHost On UseCanonicalName on ProxyPreserveHost on CacheStaleOnError on RemoteIPHeader X-Forwarded-For ProxyRequests Off ProxyPass / http://127.0.0.1:8080/ ProxyPassReverse / http://127.0.0.1:8080/ </VirtualHost>
You can visit the site for more options to use in your environment.
You should do that!
In this tutorial we have seen how to use Apache as a reverse proxy server to handle requests from back-end applications or other HTTP servers. If you find any errors above or have something to add, please use the comment form below.