How to Setup Apache Reverse Proxy

This post provides a guide for setting up the Apache HTTP server as a reverse proxy. Apache, typically a web server, can function as a reverse proxy for applications/devices lacking web server features. Using Apache reduces backend server load via SSL encryption, caching, etc. Steps include enabling necessary modules and setting configurations. This is particularly…

This post shows students and new users how to set up a reverse proxy with an Apache HTTP server. Apache is mostly used as a web server and not a reverse proxy. However, Apache has many powerful extensions to enable reverse proxy and other features. A reverse proxy is a service that sits between the client and 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 take the load off the backend servers.

A reverse proxy server is typically used in front of Node.jsPythonJava, and other popular applications that do not have web server features enabled. In this case, Apache is usually the proxy server to handle client requests.

Below we’ll show you how to use Apache as a reverse proxy for most applications and backend servers. We’ll give you some basic settings that may be used in your environments.

Also, for students and new users learning Linux, Ubuntu Linux is the easiest place to start learning. Ubuntu is the modern, open-source Linux operating system for desktops, servers, and other devices.

To use Apache as a reverse proxy, follow the steps below.

How to use Apache as a reverse proxy server

You must enable the necessary modules before using Apache to serve as a reverse proxy. These modules allow Apache to reverse proxy to backend apps and other hosts.

Run the commands below 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 for Apache to serve as a proxy server can be done using the line code below. You configure a VirtualHost for the domain and specify a location to the backend server for Apache to send 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 the example.com domain to the proxied server at http://127.0.0.1:8080.

For example, if a client request http://example.com/, Apache will forward the requests to the backend server defined on the ProxyPass and ProxyPassReverse lines: http://127.0.0.1:8080.

There are lots of advanced setups one can perform with a proxy server. However, the simple configuration 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 an SCGI server.

A common Apache reverse proxy to non-HTTP hosts is done using PHP-FPM. An example is how Apache serves PHP scripts.

<VirtualHost *:*>
    ServerName example.com
    ProxyPreserveHost On

    # Servers to proxy the connection, or;

    <FilesMatch \.php$>
        # 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 a few modules for apache2 to work with PHP-FPM:

sudo a2enmod actions fcgid alias proxy_fcgi

How to configure Apache reverse proxy options

Reverse proxy servers have common options that define how requests are handled and served to the backend servers or apps. These configuration options are recommended to use with 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

    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.

That should do it!

Conclusion:

  • Setting up Apache as a reverse proxy allows you to efficiently manage client requests and backend servers.
  • Apache provides extensive modules that enhance its functionality for reverse proxy operations.
  • Simple configurations can effectively route requests to desired backend applications, such as Node.js, Python, and PHP-FPM.
  • Advanced configurations are available for handling various types of requests beyond HTTP.
  • Understanding the fundamental reverse proxy concepts can help optimize web application performance and reliability.
  • For further customization and options, refer to the official Apache documentation on reverse proxy settings.
Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Exit mobile version