Nginx

Sunday, March 9, 2025 • edited Thursday, April 17, 2025

Proxy server

Forward proxy

  1. Primiarily serves clients
  2. used for anonymity, content filtering, bypassing restrictions

Reverse proxy

  1. Primiarily serves servers
  2. used for load balancing, caching, SSL termination, enhancing sercurity

The usage of nginx

The functionalities of Nginx as a proxy server

  1. load balancing

    static dynamic
    round robin least connection method
    weighted round-robin least response time method
    source ip hash
    http {
       upstream [] {
           server ...
           server ...
           server ...
       }
    
        server {
            ...
        }
    }
    
  2. caching (without request to server and database)

  3. security

  4. compression and segmentation

Nginx:

  1. Load balancing across multiple servers
  2. High performance web server (ideal for serving static content)
  3. Reverse proxy forwarding to appropriate back-end server
  4. sercurity features (SSL termination)

Nginx can be used as an ingress controller in kubernetes

Node.js:

  1. Load balancing across multiple CPU cores.
  2. Express.js is a server framework for Node.js
  3. Used to build dynamic web apps and APIs by defining routing logic
  4. Build custom middleware (managing Gzip compression)

brief details for node.js

Node is a cross-platform, open-source javascript runtime environment


source [https://miro.medium.com/v2/resize:fit:1200/1*iTdvBPVxYZdJZQKsP3yILw.jpeg]


Nginx configuration file

mine types : the default file already included most of format used in the web. (css,html,jpg)

location context: alias can be used to direct absolute path.

load balancer: the default algorithm is round robin. Using docker to create 4 servers to test the load balancing status.

  1. Using express.js to build a simple backend server to show some message on browser, and dockerizing the file to build an image.

image

  1. Mapping the server to 4 different ports through docker

image

  1. Modify the nginx.conf with the above code format and write a access_log to check the status of load balancer.
http{
    include       mime.types;

    upstream backendserver{
        server 127.0.0.1:1111;
        server 127.0.0.1:2222;
        server 127.0.0.1:3333;
        server 127.0.0.1:4444;
    }

    log_format upstreamlog '$server_name to: $upstream_addr {$request} '
        'upstream_response_time $upstream_response_time'
        ' request_time $request_time';

    server {
        listen 8080;
        root /mysite;
        # server_name  localhost;

        access_log logs/nginx-access.log upstreamlog;
    }

}

image

DevOps

Hugo usage

Hugo Music Player
⋮⋮ NOW PLAYING
Awesome Track
Great Artist
0:00 3:24
🔊