How to Setup Nginx as a Reverse Proxy for Apache on Debian 11

Nginx and Apache both are free, open-source, and most popular web servers around the world. Apache is known for its power while Nginx is known for its speed. Both have some pros and cons. Nginx is useful for static content while Apache is for dynamic content. If we combine both servers then we will get a better result of each other.

In this tutorial, I will configure Apache as a backend server and use Nginx as a reverse proxy for Apache on Debian 11.

Prerequisites

  • A server running Debian 11.
  • A root password is configured on the server.

Getting Started

Before starting, it is recommended to update your system's package cache to the latest version. You can update it using the following command:

apt-get update -y

After updating the package cache, install other required dependencies using the following command:

apt-get install gnupg2 curl -y

Once all the required dependencies are installed, you can proceed to the next step.

Install and Configure Apache

In this section, we will install the Apache webserver and configured it to run on port 8000.

First, install the Apache webserver using the following command:

apt-get install apache2 -y

Once the Apache is installed, edit the Apache port configuration file:

nano /etc/apache2/ports.conf

Change the Apache default port from 80 to 8000:

Listen 8000

Save and close the file then edit the Apache default configuration file:

nano /etc/apache2/sites-enabled/000-default.conf

Change the default port from 80 to 8000 as shown below:

<VirtualHost *:8000>

Save and close the file then reload the Apache service to apply the changes:

systemctl restart apache2

Now, open your web browser and access the Apache test page using the URL http://your-server-ip:8000. You should see the Apache default test page on the following screen:

Apache default page

Install and Configure Nginx

Now, we will install and configure Nginx as a reverse proxy to pass the incoming requests to the Apache server.

First, install the Nginx with the following command:

apt-get install nginx -y

Once the Nginx is installed, edit the Nginx default virtual host configuration file with the following command:

nano /etc/nginx/sites-enabled/default

Remove all lines and add the following lines:

server {

listen 80;
index index.php index.html index.htm;

server_name your-server-ip;
                
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Save and close the file then verify the Nginx for any syntax error with the following command:

nginx -t

Next, reload the Nginx service to apply the changes:

systemctl restart nginx

Verify Nginx Web Server

At this point, Nginx is installed and configured to pass all requests to the Apache backend server. You can now open your web browser and type the URL http://your-server-ip. You should see the Apache webserver default page on the following screen:

Nginx reverse proxy

Conclusion

Congratulations! you have successfully installed and configured Nginx as a reverse proxy for Apache. You can now use this setup in the production environment to speed up your website performance.

Share this page:

0 Comment(s)