How to Setup Apache Reverse Proxy
Apache acts as a reverse proxy when you set it up using modules like mod_proxy and mod_proxy_http.
A reverse proxy is like a helpful middleman that takes requests from people visiting your website and sends them to the right place on your servers. This makes your website safer, faster by saving copies of pages, and better at sharing the work across multiple servers.
You usually use this setup with Apache version 2.4 or newer. It’s especially handy for programs like Node.js or Python that might not have their own web server built-in, letting Apache handle all the incoming traffic for them.
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. You must enable the `mod_proxy` and `mod_proxy_http` modules. These modules help Apache manage website traffic by sending visitor requests to another server and retrieving responses.
Here’s how to enable these Apache modules:
The `ProxyPass` directive in your 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 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 that request to the backend server defined on the `ProxyPass` and `ProxyPassReverse` lines: http://127.0.0.1:8080.
Apache offers many advanced settings for proxy servers beyond this basic setup. This clear example illustrates how Apache proxy servers 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.
Apache often proxies requests to non-HTTP hosts using PHP-FPM. This is how Apache typically 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:
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>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!