How to Setup Apache Reverse Proxy
You set up Apache as a reverse proxy by configuring modules like mod_proxy and mod_proxy_http to direct client requests to your backend servers.
A reverse proxy acts as a gateway, intercepting incoming client requests and forwarding them to appropriate backend services. This boosts security, enhances performance via caching, and distributes traffic load effectively.
By enabling core Apache modules, specifically mod_proxy and mod_proxy_http, you transform Apache into a robust reverse proxy server. This is commonly done on Apache version 2.4 and above to manage applications like Node.js or Python.
This configuration proves invaluable when your backend applications don’t include built-in web server capabilities, allowing you to centralize and streamline request handling.
Enable Apache modules like `mod_proxy` and `mod_proxy_http` using `a2enmod`. Then, configure a VirtualHost block with `ProxyPass` and `ProxyPassReverse` directives to specify the backend server address. This directs client requests to your specified application server.
How to use Apache as a reverse proxy server
Apache needs a few special tools, called modules, turned on before it can work as a reverse proxy server.
Here’s how 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>This configuration tells Apache to send all requests for your example.com domain’s root directory (/) to the server at http://127.0.0.1:8080.
For example, if a client requests http://example.com/, Apache will forward the request to the backend server defined on the ProxyPass and ProxyPassReverse lines: http://127.0.0.1:8080.
While this setup covers the basics, Apache offers many advanced configurations for proxy servers. This simple example should give you a clear picture of how they function.
Apache 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
Setting up your Apache reverse proxy involves choosing options that tell it how to handle incoming requests and send them to your other servers.
<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.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!