Ugacomp

How do I set up a reverse proxy with Nginx?

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

A reverse proxy is a server that sits between client devices and backend servers, forwarding client requests to the appropriate server and returning the server’s response to the clients.

When a client makes a request to a web server, Nginx, configured as a reverse proxy, intercepts the request. Instead of serving the content directly, Nginx forwards the request to one or more backend servers. The backend server processes the request and sends the response back to Nginx, which, in turn, delivers the response to the client.

Before configuring Nginx as a reverse proxy, ensure that Nginx is installed on your server. You can install it using package managers like apt or yum:

  • For Debian/Ubuntu
sudo apt-get update
sudo apt-get install nginx
  • For CentOS/RHEL
sudo yum install epel-release
sudo yum install nginx

Basic Reverse Proxy Configuration

Once Nginx is installed, you can start configuring it as a reverse proxy. Open the Nginx configuration file, typically located at /etc/nginx/nginx.conf or in a separate directory like /etc/nginx/conf.d/default.conf, and add the following:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

In this example, replace example.com with your domain and backend_server with the address of your backend server. The proxy_pass directive forwards requests to the specified backend server.

Configuring Multiple Backend Servers

To configure multiple backend servers for load balancing, extend the configuration:

upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Here, the upstream block defines a group of backend servers, and the proxy_pass directive forwards requests to the servers defined in the upstream block, providing load balancing.

SSL Termination

To handle HTTPS traffic and terminate SSL at the reverse proxy, add SSL-related directives to the server block:

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /path/to/your/certificate.pem;
    ssl_certificate_key /path/to/your/private_key.pem;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Replace /path/to/your/certificate.pem and /path/to/your/private_key.pem with the paths to your SSL certificate and private key files.

After making changes to the Nginx configuration, restart the Nginx service to apply the new settings:

sudo service nginx restart

Conclusion

Configuring Nginx as a reverse proxy is a fundamental step in optimizing web application architectures. Directing and load-balancing client requests to backend servers enables Nginx to enhance performance, security, and scalability. The provided examples above cover basic and common scenarios, and further customization can be done based on specific application requirements.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.