How to Setup Apache Reverse Proxy
An Apache reverse proxy is a helpful middleman server that takes web traffic from visitors and sends it to the right place on your backend servers. Running version 2.4 or newer gives you access to built-in modules that handle this traffic routing smoothly.
This setup lets Apache protect your web apps and share visitor traffic across multiple servers so your site stays fast and reliable. It works well for programs like Node.js or Python that do not have their own built-in web servers.
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 relies on specialized components called modules to function as a reverse proxy. Administrators must enable the mod_proxy and mod_proxy_http modules. These components help Apache manage website traffic by sending visitor requests to another server and retrieving responses.
Here is how to enable these Apache modules:
The ProxyPass directive in the Apache web server configuration identifies the backend server that will receive incoming website requests. This configuration line makes the Apache proxy server act as a middleman, forwarding traffic to the correct server. Apache version 2.4.54 commonly uses this directive.
<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 the 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 forwards that request to the backend server defined on the ProxyPass and ProxyPassReverse lines: http://127.0.0.1:8080.
Apache offers advanced settings for proxy servers beyond this basic configuration. This example illustrates how Apache proxy servers function.
Apache also handles 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.
Apache often proxies requests to non-HTTP hosts using PHP-FPM. This method represents the standard way 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>Enabling a few modules allows apache2 to work with PHP-FPM:
Configuring an Apache reverse proxy involves setting up options that dictate how the server handles incoming requests and forwards them. This setup ensures Apache knows precisely where to send traffic and preserves critical details, such as the original website address.
<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>Consult the site for more options to use in your environment.
That completes the setup process.
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!