Set up a reverse proxy using Nginx to route requests to multiple backend apps Before diving into setting up a reverse proxy with Nginx, it's essential to first understand what a reverse proxy is. To effectively use Nginx as a reverse proxy, we need to clear any confusion around the concept of reverse proxies. What is a reverse proxy? A reverse proxy is a server that sits between clients and backend servers, forwarding client requests to the appropriate backend server and returning the server's response to the client. It acts as an intermediary, providing benefits such as load balancing, caching, SSL termination, and enhanced security.For example, when a user accesses a website, the reverse proxy determines which backend app (like a Node.js or Python app) should handle the request and forwards it accordingly. Why use a reverse proxy? Here are some key advantages of using a reverse proxy: [object Object], [object Object], [object Object], [object Object], [object Object] Setting up reverse proxy using Nginx Now that we understand the importance of a reverse proxy, let's explore how to set one up. In this example, we'll use Nginx to route requests to multiple backend applications. To demonstrate, we'll create a simple Node.js server and spin up multiple instances of it, simulating multiple backend applications. Let's begin by creating a simple Node.js server to serve as our backend application: Once the initialization is complete, navigate into your project folder and create a server.js file, where we'll write a simple backend server. To ensure everything is working correctly, add a start script to your package.json file. "start": "node server.js" Now, head over to your browser and type localhost:8080 in the address bar. If you see the response below, everything is working fine so far. Now that our server is ready, we can use Docker to create multiple instances of it. To do this, we first need to Dockerize the application by creating a Dockerfile. After creating the Dockerfile, we'll need a docker-compose.yaml file to spin up multiple instances of this application. Since we've added an environment variable in our docker-compose.yaml file, we can leverage it to differentiate the outputs of the various app instances. To achieve this, we'll make some changes to our server.js file. After saving these changes, open your console and type: docker-compose up --build -d This will spin up three different instances of the application, which you can access through localhost:8081, localhost:8082, and localhost:8083. Now that everything is in place, the only thing left is implementing Nginx to use it as a reverse proxy for balancing incoming requests to the backend servers. But first, let's take a moment to talk a bit about Nginx. What is Nginx? Nginx (pronounced "engine-x") is a high-performance web server and reverse proxy server that is widely used to serve static content, manage load balancing, and act as a reverse proxy for backend services. Developed for handling a high number of concurrent connections, Nginx is known for its speed, scalability, and resource efficiency.Applications of Nginx as a Reverse Proxy: [object Object], [object Object], [object Object], [object Object] While we can install Nginx locally, since we're already using Docker, we can simplify the process by running another container to handle this task. To get it working, we can add the following code to our docker-compose.yaml file. In this configuration, you'll notice that under the volumes section, we are binding a custom Nginx config file to the one inside the container. This allows us to manage the Nginx configuration as needed by modifying the local config file. To do this, create an nginx.config file in your project folder. A brief explanation of above Nginx configuration This Nginx configuration sets up a reverse proxy with load balancing across three backend servers (app1:8080, app2:8080, app3:8080). The upstream backend block defines the servers, while the server block listens on port 80 and forwards all incoming requests to the backend. Custom headers (Host, X-Real-IP, X-Forwarded-For, X-Forwarded-Proto) are set to pass client information to the backend servers. This setup ensures efficient traffic distribution, improves scalability, and provides fault After doing this, stop the currently running container by executing the following command, then restart the container. All done!! 🎉 Now, if you head over to your browser and type localhost, you'll receive the response first from app1 , then from app2 , and finally from app3 . This process will continue in a round-robin fashion, as Nginx uses the round-robin algorithm by default to load balance the servers.Final output will look something like this: Conclusion In this tutorial, we've walked through the process of setting up Nginx as a reverse proxy to load balance traffic across multiple backend servers. By using Docker to manage our application instances and leveraging Nginx's round-robin