Set Up Nginx Reverse Proxy, What is Nginx as reverse proxy, How to set up reverse proxy using Nginx, What is Nginx used for, Why use a reverse proxy,
4.8/5 - (6 votes)

Last Updated on January 29, 2023 by Vikash Ekka

A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers. Nginx is a popular web server and reverse proxy that is known for its stability, performance, and flexibility.

Setting up an Nginx reverse proxy is a great way to improve the security and performance of your web servers. A reverse proxy can hide the details of your internal network and control access to your servers, while also improving performance by caching and compressing content.

In this tutorial, we will show you how to set up an Nginx reverse proxy on Ubuntu 22.04.

Prerequisites for Set Up Nginx Reverse Proxy

Before you begin, make sure that you have a server running Ubuntu 22.04 and that you have a user account with sudo privileges.

Once you have met these prerequisites, you are ready to set up an Nginx reverse proxy on your server.

Also Read:

Install phpMyAdmin on Ubuntu 22.04 with Nginx

Install Laravel on Ubuntu 22.04 with Nginx

Hide Apache, Nginx, or PHP version on Ubuntu

Set Up Nginx Reverse Proxy

Step 1: Install Nginx

The first step is to install Nginx on your server. To install Nginx, open a terminal and type the following command:

sudo apt-get install nginx

This will install Nginx and all the necessary dependencies.

Step 2: Create a new configuration file

The next step is to create a new configuration file for the reverse proxy. In the terminal, type the following command to create a new configuration file in the Nginx configuration directory:

sudo nano /etc/nginx/conf.d/reverse-proxy.conf

This will open the nano text editor with a new, empty file.

Step 3: Configure the reverse proxy

In the configuration file, add the following code to configure the reverse proxy:

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://your_upstream_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

In this example, “example.com” is the domain name of your website, and “http://your_upstream_server” is the URL of the server that you want to proxy requests to.

The listen directive specifies the IP address and port that Nginx should listen on. The server_name directive specifies the domain name that Nginx should match when processing requests. The location directive specifies the location that Nginx should proxy requests to.

Also Read:

Install phpMyAdmin on Ubuntu 22.04 with Nginx

Install Laravel on Ubuntu 22.04 with Nginx

Hide Apache, Nginx, or PHP version on Ubuntu

The proxy_pass directive tells Nginx to proxy requests to the specified upstream server. The proxy_set_header directives set various headers that are passed to the upstream server. These headers are used to ensure that the upstream server can correctly identify the original client and provide the appropriate response.

Step 4: Test the configuration

Before you restart Nginx, it’s a good idea to test the configuration to make sure that there are no syntax errors. To do this, type the following command in the terminal:

sudo nginx -t

This will check the syntax of the Nginx configuration and report any errors. If the syntax is correct, you should see the following output:

nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 5: Restart Nginx

If the syntax is correct, type the following command to restart Nginx:

sudo systemctl restart nginx

This will apply the new configuration and start the Nginx service.

Step 6: Verify the reverse proxy is working

To verify that the reverse proxy is working, open a web browser and navigate to “http://example.com“. You should see the content of the upstream server that you specified in the proxy_pass directive. If you see the content, it means that the reverse proxy is working correctly.

Step 7: Configure Firewall

It is important to configure the firewall to only allow connections to the necessary ports. To do this, we will use the UFW (Uncomplicated Firewall) tool. Use the following command to check the status of the firewall:

sudo ufw status

If the firewall is inactive, you will see the following output:

Status: inactive

If the firewall is active, you will see the following output:

Status: active

In case the firewall is inactive, use the following command to enable it:

sudo ufw enable

Now we will allow connections on port 80 and 443 (http and https) using the following command:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

You can check the status of firewall rules by running the following command:

sudo ufw status verbose

Step 8: Enable SSL (Optional)

If you want to serve your website over HTTPS, you can use Nginx to enable SSL. To do this, you will need to have a valid SSL certificate. You can get a free SSL certificate from Let’s Encrypt using the certbot tool.

First, you will need to install certbot by running the following command:

sudo apt-get install certbot python3-certbot-nginx

Then, run the following command to obtain and install the SSL certificate:

sudo certbot --nginx

This will automatically install the SSL certificate and configure Nginx to use it.

With these steps, you have successfully set up an Nginx reverse proxy on Ubuntu 22.04. You can now use this reverse proxy to improve the security and performance of your web servers. Remember that you should regularly check for updates and security patches for both Nginx and Ubuntu to keep your server secure.

Also Read:

Install phpMyAdmin on Ubuntu 22.04 with Nginx

Install Laravel on Ubuntu 22.04 with Nginx

Hide Apache, Nginx, or PHP version on Ubuntu

Conclusion

In conclusion, setting up an Nginx reverse proxy is a great way to improve the security and performance of your web servers. It allows you to hide the IP address of your upstream servers and handle incoming traffic, such as load balancing and SSL termination.

By following the step-by-step instructions in this tutorial, you should be able to set up an Nginx reverse proxy on Ubuntu 22.04.

Remember to regularly check for updates and security patches for both Nginx and Ubuntu to keep your server secure. And always make sure to have the prerequisites met before proceeding with the setup.

By Vikash Ekka

Hi All, My name is Vikash Ekka from India. I’m the founder and tech editor of https://www.vetechno.in. I have completed my Graduation in BCA. I love to write technical articles like Windows, Linux & MAC Tutorials, Tips, Tricks, How To fix, Tutorials About Ethical Hacking & Cyber Security Guide, and Software Review. Currently, I have been working as an IT professional since 2018.

One thought on “How To Set Up Nginx Reverse Proxy – vetechno”

Leave a Reply

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