NGINX (pronounced “engine-x”) is open source software that powers web servers and enables reverse proxying, caching, load balancing, and media streaming. It was originally designed as a web server with high performance and reliability. Besides functioning as an HTTP server, NGINX acts as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.
Key features of NGINX include:
- Web Server:
- Nginx can serve static content, such as HTML, CSS, and images, efficiently. It is often used as a front-end server to deliver web pages to clients.
- Reverse Proxy:
- Nginx can act as a reverse proxy server, forwarding client requests to other servers (e.g., application servers) and then returning the responses to clients. This is useful for load balancing, distributing incoming traffic, and enhancing security.
- Load Balancer:
- Nginx can distribute incoming network traffic across multiple servers to balance the load. This helps improve the performance, reliability, and availability of web applications.
- SSL/TLS Termination:
- Nginx can handle SSL/TLS termination, allowing it to decrypt incoming encrypted traffic and forward it to backend servers in unencrypted form. This offloads the SSL/TLS processing from the application servers.
- HTTP Cache:
- Nginx includes a caching mechanism that can cache static and dynamic content, reducing the load on backend servers and improving response times.
- Security Features:
- Nginx includes features to enhance security, such as access controls, rate limiting, and the ability to block or allow specific IP addresses.
- Scalability:
- Nginx is designed to be lightweight and scalable. It can handle a large number of simultaneous connections efficiently, making it suitable for high-traffic websites and applications.
- Flexibility:
- Nginx supports a wide range of configurations, and its configuration syntax is flexible. It is often used in various scenarios, including serving static sites, proxying requests to application servers, and load balancing.
NGINX server architecture: How does NGINX work?
NGINX uses a predictable process model that is sensitive to available hardware resources:
- The master process performs privileged tasks such as reading configuration and binding ports, and spawns a small number of child processes.
- The cache loader process runs at startup to load the disk-based cache into memory, and then exits. Because it is scheduled sparingly, it has low resource requirements.
- The cache manager process runs periodically to remove entries from the disk cache, keeping it within the configured size.
- Worker processes do the day-to-day work of the NGINX web server. They handle network connections, read and write disk content, and communicate with upstream servers.
The basics of NGINX configuration
NGINX configuration is typically done using a configuration file, which is usually named nginx.conf
and is located in the NGINX installation directory. The configuration file is written in a specific format, with a set of directives that control how NGINX behaves and what it does when it receives requests.
Configuration concepts
Here are some basic concepts to understand when working with NGINX configuration:
- Directives: Instructions that tell NGINX what to do. They consist of a name and one or more parameters. Directives can be placed at different levels in the configuration file, and the level at which a directive is placed determines its scope and how it is interpreted.
- Blocks: Collections of directives that are grouped together. They are surrounded by curly braces {and} and can contain other blocks. Blocks can be nested to create a hierarchy of configuration settings.
- Context: The context in which a directive is placed determines its meaning and how it is applied. NGINX has several different contexts, including the main context, which applies to the entire NGINX server, and the server context, which applies to a specific server block.
- Server blocks: A server block is a block of directives that define the configuration for a specific virtual server. Virtual servers allow you to host multiple websites on a single NGINX instance by specifying different configurations for each server block.