Intro
According to the website, NGINX is a web-server, reverse-proxy, and load-balancer. To me that sounds like a Swiss knife of web infrastructure.
It can handle millions of requests in parallel using an Event-driven architecture that queues jobs in a way that reduces latency and wait times.
What It Does
It is a gateway between an application such as a website and the internet. Consider the following scenario, you have a website hosted on a server, but that sever also does some other stuff, NGINX can accept the HTTP requests and determine what to serve back.
Web Server
It can fulfill requests to a web server, by serving the requested assets, such as a web pages.
Load-Balancer / Proxy
Say you have several servers for your website or service, and you need to determine which of these machines will fulfill a specific request. NGINX can sit as a proxy, between the clients and the servers, and determines which of the machines will handle the request.
The load-balancing logic can be whatever algorithm it’s been configured to use. Some common examples include:
- Least Connections: whichever server has the least active connections, i.e. is least busy.
- RoundRobin which means it distributes the load cyclically between all the machines, one at a time.
Caching
When NGINX is used as a proxy, it can be configured to handle a cache for frequently requested resources. For example, if most users request the same chunk of data or a certain file over and over again, then it can be cached to decrease latency and reduce needless compute. Check out Distributed System Design Basics for more about Caching.
Single Entry Point
Services and websites that have multiple servers working can be vulnerable if each server is accepting client requests publicly, as the attack surface is greater. To prevent this, a single gateway server is used, and it’s the only one exposed to the public. By doing this, security efforts can be focused on this one machine, minimizing exposure, centralizing control.
Where’s it Found?
It’s often found on web-server machines running reverse-proxy or server, where nginx.conf
determining how to manage traffic, access, load balancing, and what to serve.
How it Functions
…
Configuration
NGINX has a config file, /etc/nginx/nginx.conf
, that defines how it’ll run, manage resources, handle requests, etc.
This file defines whether it functions as a web server or a proxy.
Directives and Blocks
A directive is the name of a particular aspect of the config, its value is the block.
- The
location
directive is used to define how a server processes specific types of requests.
Contexts
When a block directive has more directives in it, it’s then considered a Context.
Example
user ...
error_log ...
http{
server{
listen 300,
access_log ...
location ... {
root ...
}
}
}