Press "Enter" to skip to content

How to Set Up Nginx as a Reverse Proxy for Apache on Ubuntu | Step-by-Step Guide

Introduction:

Are you looking to optimize your server’s performance and strengthen website security? In this step-by-step guide, we’ll show you how to set up Nginx as a reverse proxy for Apache on Ubuntu. By the end, you’ll have a robust and efficient server configuration that enhances your website’s user experience. Let’s get started!

Step 1: Install NGINX and Apache

  • Update the package lists on your Ubuntu server: sudo apt update
  • Install NGINX: sudo apt install nginx
  • Install Apache: sudo apt install apache2

Step 2: Configure Apache

  • Create a new Apache configuration file:
    sudo nano /etc/apache2/conf-available/reverse-proxy.conf
  • Add the following content to the file:
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
  • Save and close the file.
  • Enable the Apache proxy modules: sudo a2enmod proxy proxy_http
  • Enable the reverse proxy configuration: sudo a2enconf reverse-proxy
  • Restart Apache: sudo systemctl restart apache2

Step 3: Configure NGINX

  • Open the NGINX default configuration file:
    sudo nano /etc/nginx/sites-available/default
  • Replace the existing contents with the following configuration:
server {
    listen 80;
    server_name your_domain.com; # Replace with your domain name

    location / {
        proxy_pass http://localhost: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;
    }
}
  • Save and close the file.
  • Test the NGINX configuration:
    sudo nginx -t
  • If the configuration test is successful, reload NGINX:
    sudo systemctl reload nginx

Step 4: Firewall Configuration

  • If you have a firewall enabled, you’ll need to allow incoming HTTP traffic through the firewall:
    sudo ufw allow ‘Nginx HTTP’

That’s it! NGINX is now set up as a reverse proxy for Apache. Incoming requests to NGINX will be forwarded to Apache, which will handle the actual web server functionality.

Make sure to replace your_domain.com with your actual domain name or server IP address. Additionally, ensure that Apache is running on port 8080, or modify the configurations accordingly if it is running on a different port.

2 Comments

  1. Nikhil Kushwaha Nikhil Kushwaha June 14, 2023

    Detailed description given. Thank

  2. Jane Doe Jane Doe July 19, 2023

    How can we setup this in plesk?

Leave a Reply

Your email address will not be published. Required fields are marked *