Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»How To Set Up Nginx Reverse Proxy: A Step-By-Step Tutorial

    How To Set Up Nginx Reverse Proxy: A Step-By-Step Tutorial

    By RahulJanuary 22, 20233 Mins Read

    A reverse proxy is a type of server that sits between a client and a server, acting as an intermediary between the two. It accepts the requests from clients, forwards those requests to the appropriate server, and then returns the server’s response to the client.

    Advertisement

    Reverse proxies are often used to improve the performance and security of web servers, and to allow multiple servers to appear as a single server to clients. For example, a reverse proxy can be used to distribute incoming requests to multiple servers, to cache static content to improve performance or to encrypt and decrypt traffic for security purposes.

    One popular software for configuring a reverse proxy is Nginx (pronounced “engine x”). In this article, we will discuss how to configure a reverse proxy with Nginx using an example.

    Step 1: Install Nginx

    The first step is to install Nginx on your server. This can typically be done using your operating system’s package manager (e.g. apt-get on Debian-based systems, dnf on Red Hat-based systems).

    sudo apt update && sudo apt install nginx 
    
    sudo dnf install nginx 
    

    Step 2: Configure the Backend Application

    A backend application should be listening on some other port. For example, I have created a sample node.js application that serves incoming requests using the Node express module. This application is listening on localhost and port 3000.

    node server.js
    
    Output
    debugger listening on port 5858 Server running at http://127.0.0.1:3000/

    Step 3: Configure the Nginx Server Block

    Nginx uses server blocks to configure individual websites. We need to create a new server block configuration file for our reverse proxy.

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

    Add the following configuration to the server block configuration file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    server {
        listen 80;
        server_name example.com;
     
        location / {
            proxy_pass http://127.0.0.1:3000;
            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 the above configuration file:

    • listen 80; defines the port on which Nginx will listen for incoming connections.
    • server_name example.com; is the domain name that will be used to access the reverse proxy.
    • proxy_pass http://backend-server; tells Nginx to forward the incoming requests to the specified backend server.
    • proxy_set_header Host $host; and proxy_set_header X-Real-IP $remote_addr; and proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; are used to set headers that the backend server can use to identify the original client.

    Step 4: Restart Nginx

    Before restarting the Nginx service, test the configuration files using the following command:

    sudo nginx -t 
    

    If the configuration test is successful, restart Nginx to apply the changes:

    sudo systemctl restart nginx 
    

    Note: This is a basic example and in reality, you may need to configure other settings such as SSL, caching, and security. You may also need to configure Nginx to handle different types of requests, such as handling WebSockets or serving static files. Additionally, it’s also important to consider the performance and scalability of your Nginx reverse proxy, especially if you expect a high volume of traffic.

    To secure your reverse proxy you can use SSL certificates. You can use Let’s Encrypt to get a free SSL certificate. Once you have the certificate you can configure Nginx to use it.

    For caching you can use the proxy_cache_path and proxy_cache_bypass directives to configure caching for your reverse proxy.

    Conclusion

    In conclusion, Nginx reverse proxy can be a powerful tool for managing multiple web servers and improving the performance and security of your web applications. I hope this tutorial has provided you with a good starting point for setting up your own Nginx reverse proxy.

    nginx proxy reverse proxy webserver
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to List Manually Installed Packages in Ubuntu & Debian

    10 Bash Tricks Every Developer Should Know

    How to Validate Email Address in JavaScript

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.