Learn how to set up and master reverse proxy with Apache and NGINX on Oracle Linux, RHEL 8/9, and Ubuntu Server for improved performance.
As businesses and developers increasingly adopt scalable and secure web infrastructures, reverse proxies have become a cornerstone for efficient application delivery. A reverse proxy acts as an intermediary between client devices and backend servers, forwarding client requests to the appropriate backend server and returning the response to the client. This setup offers a myriad of benefits, including load balancing, enhanced security, caching, and SSL termination.
For Linux administrators — whether newly stepping into the role or seasoned professionals seeking to refine their expertise — mastering reverse proxy configurations is essential. This guide provides an in-depth overview of configuring and troubleshooting reverse proxies using NGINX and Apache on popular Linux distributions such as Oracle Linux, RHEL 8/9, and Ubuntu Server. Practical examples, real-world use cases, and troubleshooting techniques are included to cater to both entry-level and advanced administrators.
Setting Up NGINX Reverse Proxy on Oracle Linux, RHEL 8/9, and Ubuntu
1. Install NGINX
Oracle Linux / RHEL 8/9: Enable the EPEL repository to install NGINX:
sudo dnf install epel-release
sudo dnf install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
Ubuntu Server: Install NGINX from the default repository:
sudo apt update
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
2. Configure NGINX for Reverse Proxy
Create a new configuration file:
sudo nano /etc/nginx/conf.d/reverse-proxy.conf
Basic Reverse Proxy Example:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Test and reload the configuration:
sudo nginx -t
sudo systemctl reload nginx
Advanced Example: NGINX Reverse Proxy with SSL
Install Let’s Encrypt client to set up an SSL certificate:
sudo dnf install certbot python3-certbot-nginx # Oracle Linux/RHEL
sudo apt install certbot python3-certbot-nginx # Ubuntu
Get and apply an SSL certificate:
sudo certbot --nginx -d example.com
sudo systemctl reload nginx
Advanced Use Case: Microservices Architecture with NGINX
When managing a microservices-based application, you can route traffic to different services based on URL paths:
server {
listen 80;
server_name api.example.com;
location /auth/ {
proxy_pass http://127.0.0.1:8081;
}
location /data/ {
proxy_pass http://127.0.0.1:8082;
}
}
Setting Up Apache Reverse Proxy on Oracle Linux, RHEL 8/9, and Ubuntu
1. Install Apache
Oracle Linux / RHEL 8/9:
sudo dnf install httpd
sudo systemctl enable httpd
sudo systemctl start httpd
Ubuntu Server:
sudo apt update
sudo apt install apache2
sudo systemctl enable apache2
sudo systemctl start apache2
2. Enable Necessary Apache Modules
Enable the required modules for reverse proxy functionality:
sudo a2enmod proxy proxy_http # Ubuntu
sudo systemctl restart apache2
# Oracle Linux / RHEL
sudo dnf install mod_proxy
3. Configure Apache for Reverse Proxy
Edit or create a configuration file:
sudo nano /etc/httpd/conf.d/reverse-proxy.conf # Oracle Linux/RHEL
sudo nano /etc/apache2/sites-available/reverse-proxy.conf # Ubuntu
Basic Reverse Proxy Example:
<VirtualHost *:80>
ServerName example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
Enable and reload the configuration:
sudo systemctl reload httpd # Oracle Linux/RHEL
sudo a2ensite reverse-proxy.conf # Ubuntu
sudo systemctl reload apache2
Advanced Example: Apache Reverse Proxy with Load Balancing
Distribute traffic across multiple backend servers for better performance:
<Proxy "balancer://mycluster">
BalancerMember http://127.0.0.1:8081
BalancerMember http://127.0.0.1:8082
</Proxy>
<VirtualHost *:80>
ServerName example.com
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/
</VirtualHost>
Troubleshooting Reverse Proxy Issues
Common Issues and Solutions
- 502 Bad Gateway
- Cause: Backend server is unreachable.
- Investigation:
- Check if the backend server is running:
sudo systemctl status backend_service
- Verify the backend IP and port in the configuration.
- Test connectivity to the backend server:
curl http://127.0.0.1:8080
- Check if the backend server is running:
- Solution:
- Start the backend service.
- Correct the
proxy_pass
orProxyPass
directive.
- 503 Service Unavailable
- Cause: Misconfigured or disabled backend server.
- Investigation:
- Check logs for errors:
sudo tail -f /var/log/nginx/error.log # NGINX sudo tail -f /var/log/httpd/error_log # Apache (RHEL/Oracle Linux) sudo tail -f /var/log/apache2/error.log # Apache (Ubuntu)
- Confirm the backend service is listening on the correct port.
- Check logs for errors:
- Solution: Fix the backend service or update the proxy configuration.
- SSL Certificate Errors
- Cause: Expired or improperly configured SSL certificate.
- Investigation:
- Check certificate validity:
openssl s_client -connect example.com:443
- Check certificate validity:
- Solution: Renew the SSL certificate:
sudo certbot renew sudo systemctl reload nginx
- Slow Proxy Response
- Cause: Backend server performance issues or high load.
- Investigation:
- Monitor backend server load:
- Monitor backend server load:
top “` – Use tools like htop
or iotop
for deeper analysis. – Check logs for processing delays.
- Solution: Optimize the backend application and consider adding caching to the proxy server.
Conclusion
Reverse proxies are indispensable for building scalable, secure, and efficient web applications. Both NGINX and Apache offer robust solutions for implementing reverse proxies, whether you’re hosting multiple websites, managing microservices, or optimizing traffic with load balancing. By mastering the configurations and troubleshooting techniques discussed in this guide, Linux administrators can streamline application delivery, improve system performance, and ensure high availability for their infrastructure.
For beginners, starting with simple setups like basic reverse proxying or hosting multiple domains is a great way to learn the fundamentals. Advanced administrators can leverage SSL termination, load balancing, and microservices routing to handle more complex architectures.
Ultimately, reverse proxies empower Linux administrators to design flexible and secure environments that meet the demands of modern web applications, ensuring both reliability and performance for end-users.