Setting Up Apache 2.4 as a Reverse Proxy: A Comprehensive Guide
Written on
Introduction to Reverse Proxy Configuration
Why would one opt for a reverse proxy? There are several benefits, including:
- Security Compliance: Preventing direct access to backend servers enhances security.
- Load Balancing: Distributing incoming HTTP requests efficiently.
- Traffic Monitoring: Improving security through traffic monitoring and logging.
- SSL Offloading: Simplifying certificate management by avoiding installations on backend servers.
- Static Content Serving: Allowing backend servers to focus on dynamic content while serving static files.
- URL Rewriting: Modifying URLs before they reach the backend servers.
- Unified URL Space: Merging different websites into a single domain.
This guide builds upon the previous tutorial on compiling Apache 2.4 on RHEL8 using AWS EC2. To follow along, you will need two Apache servers: one acting as a reverse proxy and the other as a backend web server.
For this demonstration, the reverse proxy will be assigned the IP address 10.0.0.10 on HTTP port 80, while the web server will use the IP address 10.0.0.20 on HTTP port 8080.
Configuring Apache 2.4 as a Reverse Proxy
Setting up Apache 2.4 as a reverse proxy involves a straightforward configuration. Begin by editing the httpd.conf file located in /opt/SP/apache-2.4/conf and uncomment the following lines:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Next, append these lines to the end of the configuration file:
ProxyPreserveHost On
ProxyPass / http://10.0.0.20:8080/
ProxyPassReverse / http://10.0.0.20:8080/
After making these changes, restart Apache for them to take effect:
# service apache restart
To verify that the modules have been loaded correctly, you can run:
# /opt/SP/apache-2.4/bin/apachectl -M | grep proxy
You should see the following output indicating that both modules are loaded:
proxy_module (shared)
proxy_http_module (shared)
Next, ensure that your firewall is configured to allow traffic on HTTP port 8080 for the web server. If using Amazon EC2, check the Security Group settings for your web server and confirm that port 8080 is open to the reverse proxy's IP address, 10.0.0.20, and any Elastic IP you might have.
On the web server, you will also need to edit the httpd.conf file and modify the following line:
Listen 8080
To demonstrate the reverse proxy functionality, you can keep the ports different; the reverse proxy will listen on HTTP port 80, while the web server will operate on HTTP port 8080. After making changes, restart Apache again:
# service apache restart
Confirm that the web server is functioning correctly by running:
# curl http://localhost:8080
This should return a success message.
Next, test the reverse proxy functionality:
# curl http://10.0.0.20:8080
You should see a successful response. Finally, check if the reverse proxy can handle requests directed at HTTP port 80:
# curl http://10.0.0.10
A successful response confirms that the setup is working as intended. To validate that the reverse proxy is indeed routing requests to the web server, you can modify the index.html file on the web server. Change the text "It works!" to "The app works!" and retest the reverse proxy:
# curl http://10.0.0.10
The response should now reflect the change: "The app works!"
If you encounter any issues, the log files for both the reverse proxy and web server can be found at:
/opt/SP/apache-2.4/logs/access_log
/opt/SP/apache-2.4/logs/error_log
You may also find additional resources and guidance in the following videos:
Setup Apache as a Reverse Proxy - YouTube
In this video, you’ll learn how to set up Apache as a reverse proxy, covering essential configurations and best practices.
Configuring Apache HTTP Server as a Reverse Proxy for Jenkins - YouTube
This tutorial provides a step-by-step guide on configuring Apache as a reverse proxy specifically for Jenkins, highlighting key settings.
Conclusion
With this configuration, you can successfully direct requests from an HTTP port 80 endpoint to a backend web server operating on port 8080. Enjoy the benefits of enhanced security and resource management through your Apache reverse proxy setup.