A reverse proxy acts as an intermediary that forwards client requests to other servers. It is often used for load balancing, security, caching, or to forward HTTP requests to backend servers (for example, an application running on Node.js, Python, PHP, or another server).
Apache allows you to configure this using its mod_proxy and mod_proxy_http modules. Here's a guide on how to do this.
We're going to configure Apache as a reverse proxy for a backend service, such as a server running on localhost on port 8080.
1.Enable the necessary modules
First, you need to enable the proxy modules in Apache:
sudo a2enmod proxy sudo a2enmod proxy_http
Restart Apache for the modules to take effect:
sudo systemctl restart apache2
2.Configure Virtual Host with Reverse Proxy
Now edit the configuration file for your virtual host that we created earlier to add the proxy directives.
Open the configuration file:
sudo your_editor /etc/apache2/sites-available/php.conf
add the proxy configuration lines inside the
ServerAdmin webmaster@localhost ServerName php.info DocumentRoot /var/www/html/php # Reverse Proxy Directives ProxyPreserveHost On ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ AllowOverride All Require all granted # Logs for debugging ErrorLog ${APACHE_LOG_DIR}/php_error.log CustomLog ${APACHE_LOG_DIR}/php_access.log combined
These directives do the following:
3.Restart Apache
After making the changes, restart Apache again:
sudo systemctl restart apache2
4.Test the Reverse Proxy
Now, when you access http://php.info, Apache will forward requests to the backend listening on http://localhost:8080.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3