NGINX explained in simple words in 2 mins with practical examples

NGINX explained in simple words in 2 mins with practical examples

What the heck is NGINX?

NGINX is a free, open-source, high-performance web server that can also function as a reverse proxy server, load balancer, and HTTP cache. NGINX is designed to handle a large number of concurrent connections efficiently and can be used to serve static content, dynamic content, and streaming media.

NGINX works by accepting incoming client requests and processing them according to the rules specified in its configuration files.

Brief overview

  1. NGINX listens for incoming requests on one or more ports. By default, NGINX listens on port 80 for HTTP requests and port 443 for HTTPS requests.

  2. When a request is received, NGINX looks up the configuration file to determine how to handle the request.

  3. NGINX then processes the request according to the rules specified in the configuration file. This could involve serving a static file, proxying the request to another server, or performing some other action.

  4. Once the request has been processed, NGINX sends the response back to the client.

Some examples

Serving static files

NGINX can be used to serve static files like HTML, CSS, and JavaScript files. For example, the following NGINX configuration block serves all files in the "/var/www/html" directory:

server {
    listen 80;
    server_name example.com;

    location / {
        root /var/www/html;
        index index.html;
    }
}

Proxying requests

NGINX can be used to proxy requests to another server. For example, the following NGINX configuration block proxies requests to a backend server running on port 8080:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Load balancing

NGINX can be used as a load balancer to distribute incoming requests across multiple backend servers. For example, the following NGINX configuration block uses the "upstream" directive to define a group of backend servers, and the "proxy_pass" directive to distribute requests across them:

upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

These are just a few examples of how NGINX can be used. NGINX is a powerful and versatile web server that can be configured to handle a wide variety of use cases.

Read more about NGINX

{ Handbook }

Did you find this article valuable?

Support Harsh Mange by becoming a sponsor. Any amount is appreciated!