Allison Machado Gonçalves

Allison Machado Gonçalves


Nginx Docker Labs

Table of Contents

Introduction πŸ’‘

Nginx, a powerful web server and reverse proxy, is a crucial component for managing and optimizing web traffic. In this blog post, we'll explore various Nginx configurations, each serving a distinct purpose. From serving static files to acting as a load balancer, Nginx is a versatile tool that can help with the performance, security, and scalability of your web applications.

All the source code bellow can be easily downloaded and executed cloning this github repository! 😎

Disclaimer ❗

This post was used with the help of multiple References and the Nginx Documentation. A few sentences are just copied from the sources because my intent is not to be a technical writer, I just want to condense information in a quick to read and absorb way.


1. Static Http Server 🌡

A basic http file server configuration...

In the following Nginx configuration, we establish a basic HTTP server for serving static files:

events {}
http {
  server {
    location / {
        root /data/www;
    }

    location /images/ {
        root /data/;
    }
  }
}

This configuration sets up an HTTP server that listens for incoming requests on the default port 80 and serves files from the specified directories. The first location block serves files from /data/www, while the second location block serves files from /data/images when the URL path begins with /images/.

2. Simple Proxy Server πŸ¦‹

A minimal reverse proxy config for a node.js application with a path rewrite example...

This Nginx configuration demonstrates the use of Nginx as a reverse proxy for a Node.js application with path rewriting:

events {}
http {
  server {
    location / {
        root /data/www;
    }

    location /images/ {
        root /data/;
    }

    location /api {
        proxy_pass http://host.docker.internal:9000;
        rewrite ^/api(/.*)$ $1 break;
    }
  }
}

In this configuration, Nginx proxies requests to a Node.js application running on http://host.docker.internal:9000. This is a way to access a process running in the host from a docker container.

This configuration also includes a path rewriting rule to remove the /api prefix from the incoming requests, ensuring that the application receives correct path without this prefix.

3. Named Proxy Server πŸ“Ÿ

A reverse proxy example where requests are routed by their host name...

This Nginx configuration showcases a reverse proxy setup that routes requests based on their header host name:

events
{
}
http
{
	server
	{
		listen 80;
		server_name app.train.local;
		location /
		{
			proxy_pass http://host.docker.internal:9000;
		}
	}

	server
	{
		listen 80;
		server_name cms.train.local;
		location /
		{
			proxy_pass http://host.docker.internal:7000;
		}
	}
}

In this configuration, Nginx listens on port 80 and routes requests to different backend services based on the server_name specified in the HTTP host header. Requests with the host name app.train.local are directed to a service running on http://host.docker.internal:9000, while those with the host name cms.train.local are routed to a service running on http://host.docker.internal:7000.

4. Load Balancer Server πŸ•ΈοΈ

Use Nginx as an HTTP load balancer to distribute traffic to multiple servers...

This Nginx configuration illustrates how to use Nginx as a load balancer for distributing traffic among multiple servers:

events
{
}
http {
    upstream stateless_app {
        server host.docker.internal:5000;
        server host.docker.internal:7000;
        server host.docker.internal:9000;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://stateless_app/;
        }
    }
}

Here, we define an upstream group called stateless_app that includes multiple backend servers. Nginx load-balances incoming requests across these servers, ensuring efficient distribution of traffic. This configuration uses round-robin by default.

Conclusion πŸ’¬

Nginx is a versatile tool that can be configured to serve various purposes in your web infrastructure. From basic file serving to advanced reverse proxy and load balancing, Nginx plays a significant role in optimizing web application performance and reliability.

Understanding these basic config setups can empower you to leverage Nginx for a wide range of web server and proxying use cases!


References πŸ“š