Configuring URL Redirection and Reverse Proxy in Apache2 for Ubuntu Users
Hello DevOps Enthusiasts,
As a Senior DevOps Engineer, one of the essential tasks I often handle involves configuring web servers for URL redirection and setting up reverse proxies. Apache2, a robust and highly customizable web server, is a popular choice for this. In this blog, I'll walk you through the process of setting up URL redirection and reverse proxy using Apache2 on Ubuntu.
Why URL Redirection and Reverse Proxy?
URL Redirection: Simplifies URLs, improves user experience, and maintains SEO rankings.
Reverse Proxy: Enhances security, load balancing, and resource utilization by distributing client requests to different backend servers.
Prerequisites
Ubuntu Server: Ensure you have an Ubuntu server instance running (preferably on an EC2 instance).
Apache2 Installed: Apache2 web server installed on your Ubuntu instance.
Basic Knowledge: Familiarity with the Linux command line and Apache configuration.
Step-by-Step Guide
1. Install Apache2
First, ensure Apache2 is installed on your Ubuntu server. If not, you can install it using the following commands:
sudo apt update sudo apt install apache2
2. Enable Required Modules
For URL redirection and reverse proxy, you need to enable the mod_rewrite
and mod_proxy
modules:
sudo a2enmod rewrite sudo a2enmod proxy sudo a2enmod proxy_http
Restart Apache2 to apply the changes:
sudo systemctl restart apache2
3. Configure URL Redirection
To set up URL redirection, you need to modify your Apache configuration file. You can do this in the default site configuration or create a new configuration file.
Edit the default configuration file:
sudo nano /etc/apache2/sites-available/000-default.conf
Add the following block within the <VirtualHost *:80>
directive for a simple HTTP to HTTPS redirection:
<VirtualHost *:80>
ServerAdmin
webmaster@domain.com
DocumentRoot /var/www/html
# Redirect HTTP to HTTPS RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$
https://%{HTTP_HOST}%{REQUEST_URI}
[L,R=301] or
Redirect permanent /
https://
URL-tobe-redirect
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file. Restart Apache2 to apply the changes:
sudo systemctl restart apache2
4. Configure Reverse Proxy
To set up Apache2 as a reverse proxy, modify your configuration file as follows:
Edit the default configuration file:
sudo nano /etc/apache2/sites-available/000-default.conf
Add the following block within the <VirtualHost *:80>
directive:
<VirtualHost *:80> ServerAdmin
webmaster@domain.com
DocumentRoot /var/www/html
# Redirect HTTP to HTTPS RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$
https://%{HTTP_HOST}%{REQUEST_URI}
[L,R=301]
# Reverse Proxy Configuration ProxyPreserveHost On ProxyPass /app1
http://backend-server1:8080/
ProxyPassReverse /app1
http://backend-server1:8080/
ProxyPass /app2
http://backend-server2:8080/
ProxyPassReverse /app2
http://backend-server2:8080/
ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
Replace http://backend-server1:8080/
and http://backend-server2:8080/
with the actual URLs of your backend servers.
Save and close the file. Restart Apache2 to apply the changes:
sudo systemctl restart apache2
5. Testing Your Configuration
To verify your configuration, open a web browser and navigate to your domain:
URL Redirection: Access
http://your-domain.com
and ensure it redirects tohttps://your-domain.com
.Reverse Proxy: Access
http://your-domain.com/app1
andhttp://your-domain.com/app2
to ensure requests are correctly proxied to your backend servers.
Conclusion
Setting up URL redirection and reverse proxy using Apache2 on Ubuntu can significantly improve your web application's performance, security, and user experience. By following these steps, you can efficiently manage URL redirections and distribute client requests to different backend servers.
Feel free to reach out if you have any questions or need further assistance. Happy configuring!
Best, Vishal Magar
Subscribe to my newsletter
Read articles from Vishal Magar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by