Apache Virtual Host: Adding reverse proxy

What is a reverse proxy?

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 applicat…


This content originally appeared on DEV Community and was authored by Antonio Silva

What is a reverse proxy?

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.

Scenario

We're going to configure Apache as a reverse proxy for a backend service, such as a server running on localhost on port 8080.

Steps to configure the reverse proxy:

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 <VirtualHost> block:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName php.info
    DocumentRoot /var/www/html/php

    # Reverse Proxy Directives
    ProxyPreserveHost On
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/

    <Directory /var/www/html/php/>
        AllowOverride All
        Require all granted
    </Directory>

    # Logs for debugging
    ErrorLog ${APACHE_LOG_DIR}/php_error.log
    CustomLog ${APACHE_LOG_DIR}/php_access.log combined
</VirtualHost>

These directives do the following:

  • ProxyPreserveHost On: Preserves the original Host header from the client when Apache makes the request to the backend server.
  • ProxyPass / http://localhost:8080/: Redirects all requests that come into Apache to the server listening on port 8080.
  • ProxyPassReverse / http://localhost:8080/: Maps the request back to Apache, meaning that the responses from the backend are modified to look like they came directly from Apache.

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.


This content originally appeared on DEV Community and was authored by Antonio Silva


Print Share Comment Cite Upload Translate Updates
APA

Antonio Silva | Sciencx (2024-10-08T17:00:00+00:00) Apache Virtual Host: Adding reverse proxy. Retrieved from https://www.scien.cx/2024/10/08/apache-virtual-host-adding-reverse-proxy/

MLA
" » Apache Virtual Host: Adding reverse proxy." Antonio Silva | Sciencx - Tuesday October 8, 2024, https://www.scien.cx/2024/10/08/apache-virtual-host-adding-reverse-proxy/
HARVARD
Antonio Silva | Sciencx Tuesday October 8, 2024 » Apache Virtual Host: Adding reverse proxy., viewed ,<https://www.scien.cx/2024/10/08/apache-virtual-host-adding-reverse-proxy/>
VANCOUVER
Antonio Silva | Sciencx - » Apache Virtual Host: Adding reverse proxy. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/08/apache-virtual-host-adding-reverse-proxy/
CHICAGO
" » Apache Virtual Host: Adding reverse proxy." Antonio Silva | Sciencx - Accessed . https://www.scien.cx/2024/10/08/apache-virtual-host-adding-reverse-proxy/
IEEE
" » Apache Virtual Host: Adding reverse proxy." Antonio Silva | Sciencx [Online]. Available: https://www.scien.cx/2024/10/08/apache-virtual-host-adding-reverse-proxy/. [Accessed: ]
rf:citation
» Apache Virtual Host: Adding reverse proxy | Antonio Silva | Sciencx | https://www.scien.cx/2024/10/08/apache-virtual-host-adding-reverse-proxy/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.