How to Set Up an Nginx Reverse Proxy

A reverse proxy server acts as an intermediary between client devices (like web browsers) and backend servers. Unlike a traditional forward proxy that handles requests from clients to external servers, a reverse proxy manages requests from clients to internal servers. It serves as a gateway, receiving requests on behalf of the backend servers and then forwarding the responses back to the clients.

You can use a powerful Nginx reverse proxy server that excels at handling simultaneous connections and distributing client requests among multiple servers. In this guide, we will guide you on how you can set up a Nginx reverse proxy server.

Read also Apache vs Nginx

How to Set up an Nginx Reverse Proxy Server

Setting up a Nginx reverse proxy server requires you to follow some steps which are discussed below.

Step 1: Installing Nginx

The first step is to install the Nginx server which can be done by executing the following command:

sudo apt install nginx

apt install nginx

Step 2: Allowing Nginx Through Firewall

The web server is based on HTTP, so we need to allow it through a UFW firewall to allow all the traffic that is accessing it:

sudo ufw allow 'Nginx HTTP'
ufw allow nginx

Step 3: Checking Nginx Status

Now you need to check the operational status of Nginx if it’s been activated or not:

systemctl status nginx

nginx status

Step 4: Creating a New Configuration File

Next, navigate to the sites-available directory:

cd /etc/nginx/sites-available

nginx sites-available

The sites-available directory in Nginx serves as a location where configuration files for individual websites or web applications are stored. Each file in this directory typically represents a specific site configuration, outlining settings such as server blocks, virtual hosts, and other parameters related to how Nginx should handle requests for that site.

Next, you need to create the myserver.conf file using any text editor:

sudo nano myserver.conf
myserver.conf

You can save this configuration file with whatever name you like. Next, you need to write the following lines inside the configuration file:

server {

  listen 80;

  location / {

    proxy_pass http://my_server;

  }

}
proxy pass

This serves as a simple illustration of a Nginx reverse proxy setup. 

In this example, Nginx is configured to monitor all incoming traffic on port 80. The key directive, “proxy_pass,” instructs Nginx to forward all port 80 traffic to a specified location, in this case, http://my_server. Users can customize this destination by replacing “http://my_server” with their preferred location. 

Essentially, this Nginx configuration enables the interception of client requests on port 80, redirecting them to the designated location specified by the user. Upon completing the customization, the file can be saved, and the configuration changes take effect.

Step 5: Activate Configuration File

Now you need to establish a link between an Nginx configuration file located in the sites-available directory and the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myserver.conf /etc/nginx/sites-enabled/myserver.conf
activate nginx conf

The significance of this command lies in the organization of Nginx configuration files. The sites-available directory typically holds various configuration files, while the sites-enabled directory contains symbolic links to the active configurations. 

Creating a symbolic link from sites-enabled to sites-available enables or “enables” a specific configuration, allowing Nginx to recognize and use it when handling web traffic. It provides a convenient way to manage and switch between different configurations without physically moving or duplicating files.

Step 6: Testing Nginx 

Lastly, we need to test the nginx using the command and restart it so that it can run smoothly without any issues:

sudo service nginx configtest
sudo service nginx restart

This command is used to check the syntax and validity of the Nginx configuration files. When you run this command, Nginx performs a test on its configuration files to ensure that they are correctly written and free of errors.

The significance of this command lies in its ability to prevent potential issues that may arise from misconfigured Nginx settings. If there are any errors in the configuration files, running this command will highlight them, allowing administrators to identify and fix problems before applying the configuration changes. It helps ensure the smooth operation of the Nginx web server by catching and addressing configuration issues proactively.

In case you are facing any error you can take a look at error log entries to get more detail by executing the following command:

sudo tail /var/log/nginx/error.log

How Nginx Reverse Proxy Works

Here’s how nginx works as a reverse proxy

Client Request: When a client initiates a request for a particular resource, such as a web page or an application, the request is first directed to the Nginx reverse proxy.

Proxy Server Handling: Nginx, configured as a reverse proxy, receives the client’s request. Instead of processing the request itself, Nginx forwards the request to one or more backend servers that are responsible for hosting the actual content or application.

Backend Server Processing: The backend server processes the request and generates the response, whether it’s a webpage, an application output, or any other resource.

Response to Proxy: Once the backend server has processed the request, it sends the response back to the Nginx reverse proxy.

Proxy Response to Client: Nginx, acting as a reverse proxy, then forwards the response received from the backend server to the original client that initiated the request.

Conclusion

Establishing an Nginx reverse proxy involves a systematic process, from initial installation to configuration and testing. The guide provides clear steps, including firewall setup, creating and activating configuration files, and ensuring their correctness. The core lies in crafting a server block, directing incoming traffic to specified backend servers. 

Proactive error-checking via sudo service nginx configtest ensures smooth server operation. Understanding the proxy’s function is crucial—it efficiently manages client requests, forwarding them to backend servers for processing. This guide equips users to implement a robust Nginx reverse proxy, optimizing traffic management and server resources effectively.

Experience top-notch security and performance with Ultahost’s VPS with DDoS protection. Safeguard your server while maximizing speed and efficiency for your Nginx reverse proxy setup. Choose Ultahost for unparalleled reliability and peace of mind.

FAQ

What is Nginx Reverse Proxy?
Why would I want to set up an Nginx Reverse Proxy?
What are the prerequisites for setting up an Nginx Reverse Proxy?
How do I test if the Nginx Reverse Proxy is working correctly?

Related Post

What is HTTPS Not Secure and how to fix it

Have you ever encountered the "HTTPS Not Secure" messag...

How to Set Up RTMP NGINX on Ubuntu Server

RTMP stands for Real-Time Messaging Protocol and is a w...

How to Limit Login Attempts in WordPress

WordPress is now the most used platform for building we...

What is an “SSL Handshake Failed Error&

An SSL handshake is a process that begins when your bro...

How to Enable Two-Factor Authentication in WP

Two-factor authentication or 2FA adds an extra layer of...

How to Set Password Protected WordPress Page

In many cases, you might want to restrict access to cer...

Leave a Comment