Step-by-Step Guide! Apache Reverse Proxy on Debian System
Here I show you how to set up a reverse proxy server with Apache2
A reverse proxy server acts as an intermediary between users and the servers where a website or service is hosted. It receives user requests and forwards them to the appropriate server without the user directly connecting to it. This setup offers several advantages:
Benefits of a Reverse Proxy Server
A reverse proxy provides numerous benefits for performance, security, and management of web applications. By intelligently distributing requests and adding security features, a reverse proxy significantly enhances user experience and increases security.
Key Advantages of a Reverse Proxy Server
- Load Balancing: The reverse proxy can distribute requests across multiple servers, balancing the load to prevent overloads and improve speed.
- Security: It conceals the identity and structure of backend servers, protecting them from direct attacks.
- SSL Termination: The proxy can manage SSL certificates and decrypt HTTPS requests, reducing the load on backend servers.
- Caching: A reverse proxy can cache content, reducing load times for frequently accessed resources.
A reverse proxy is, therefore, a valuable tool for enhancing the performance and security of web applications while also providing better control over data traffic.
Let´s start!
Here in this example I use http, but with https it works the same only via port 443.
Apache2 must be installed, I always install PHP as well.
In this experiment, one DNS name should be accessible “normally” via port 80 and another DNS name via port 81.
In this experiment, one DNS name should be accessible “normally” via port 80 and another DNS name via port 81.
-
website1.loc This website is accessible via port 80 and is located in the standard Apache directory.
/var/www/html
-
website2.loc This website should be accessible via port 81 and this website is located in this newly created directory.
/var/www/site2
My router can resolve the DNS names, I have stored them in the router and so I can resolve the DNS names in my private network.
Now we have to activate the ports Apache2 listens to, You need root privileges!
Simply add a line for each port you want to activate.
nano /etc/apache2/ports.conf
Port 80 is activated by default and there is already a default config 000-default.conf
and it directs all requests to this directory./var/www/html
We also use port 81 and if a request is received on this port, this request must be forwarded to the directory that we have previously created./var/www/site2
When we have everything ready, the request is then automatically forwarded to port 81 via port 80 with the help of the reverse proxy.
Now we create a new config file which redirects port 81 to the directory.
nano /etc/apache2/sites-available/website1_port81.conf
ServerAdmin webmaster@localhost
DocumentRoot /var/www/site2
</VirtualHost>
Now we have to activate the newly created config file. You can choose the name for the config file yourself.
a2ensite website1_port81.conf
Then restart Apache.
systemctl reload apache2
Now you can test whether the website is accessible via port 81. Simply enter your server IP and the port in the URL of the browser and the page should load.
Now we finalize the reverse proxy, we just want to enter a DNS name in the URL and then we should be redirected in the background without the user noticing anything or having to add a port to the URL. In this example I type website2.loc in the URL and I am redirected to port 81. Again, you can of course choose the port yourself.
Now we activate the required packages for the reverse proxy.
a2enmod proxy
a2enmod proxy_http
Now we create a config file again, you can choose the name yourself, in this config file we configure the reverse proxy.
nano /etc/apache2/sites-available/website2_proxy.conf
ServerName website2.loc #If the request comes with this DNS name, # this config is used
ProxyPass "/" "http://localhost:81/" #Here you specify which website should, #be loaded. #An external link can also be entered here #Adjust both lines equally ProxyPassReverse "/" "http://localhost:81/"
# Additional configurations here, if needed
</VirtualHost>
Activate the config and restart apache.
a2ensite website2_proxy.conf
systemctl reload apache2
Now enter the DNS name in the browser and your desired website will be loaded with the reverse proxy server.