Configure Nginx Reverse Proxy on Ubuntu 22.04 LTS

Introduction

Nginx reverse proxy is a server that sits between client devices and a web server, forwarding client requests to the web server and returning the server’s responses to the clients. It operates on the behalf of the client, making requests to one or more backend servers, and then returning the content to the client.

Prerequisites

  • Up and running ubuntu 22.04 LTS machine.
  • Basic knowledge in Linux commands.
  • Internet connectivity.

In this post, We will show you how to setup nginx reverse proxy on ubuntu 22.04 LTS.

Step 1: Run System Update

Run the following command to update the package lists to ensure you have the latest information about available packages:

sudo apt-get update

Step 2: Installing Nginx

If you haven’t installed Nginx, you can do so by running:

sudo apt-get install nginx
 -y

Step 3: Create a Configuration File

Create a new Nginx configuration file or use the default one. In this example, I’ll use the default configuration file located at /etc/nginx/sites-available/default.

sudo nano /etc/nginx/sites-available/default

Step 4: Configure Nginx as a Reverse Proxy

Add the following lines inside the server block to configure Nginx as a reverse proxy. Replace example.com with your domain or server IP, and adjust the proxy_pass directive to point to your backend server.

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;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # Additional configurations if needed
}

Save the changes to the configuration file and exit the text editor.

Step 5: Test Configuration

Before restarting Nginx, test the configuration:

sudo nginx -t

If there are no errors, you should see a message like “nginx: configuration file /etc/nginx/nginx.conf test is successful.”

Step 6: Restart Nginx

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 7: Firewall Configuration (if needed)

If you have a firewall running (e.g., UFW), make sure to allow traffic on the Nginx port (default is 80):

sudo ufw allow 80

Step 8: Verify

Visit your domain or server IP in a web browser. Nginx should now be forwarding requests to your backend server.

  • Ensure that the backend server is configured to accept requests from the Nginx server.
  • Adjust the location block and other settings based on your specific requirements.
  • Always secure your configuration and server. Remove default configurations that you don’t need, and keep your software up-to-date.

Conclusion

We have successfully Installed and configure nginx reverse proxy on ubuntu 22.04 LTS Debian machine, If you still have questions, please post them in the comments section below.

Author

Configure Nginx Reverse Proxy on Ubuntu 22.04 LTS

Leave a Reply

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

Scroll to top