Nginx
Nginx is a web server and reverse proxy server designed to handle HTTP and HTTPS requests, serve web content, and forward requests to other applications and servers. Nginx can also be used for load balancing, caching, and proxying network traffic.
Nginx is used in the infrastructure of websites, web applications, and APIs. It can serve static files directly or operate in front of an application server or another web server, receiving external requests from users.
The name Nginx is pronounced “engine-x.”
How Nginx Works
When a browser or another client application accesses a website, Nginx receives the HTTP request and determines how to process it according to its configuration.
If a static resource is requested, such as an image, CSS file, or static HTML page, Nginx can read it from the file system and deliver it directly to the client.
For dynamic content, the request can be forwarded to an application running on the same server or another server. The application generates a response and returns it to Nginx, which then sends the result to the user.
Nginx uses an event-driven, asynchronous architecture. Worker processes can handle a large number of connections simultaneously without creating a separate process or thread for each connection. This model is particularly efficient when handling large numbers of concurrent network connections.
Nginx as a Reverse Proxy
One of the most common uses of Nginx is as a reverse proxy. In this architecture, users connect to Nginx, while the actual applications are located behind it and may not be directly accessible from the external network.
For example, a request to example.com first reaches Nginx. Depending on the URL, the server can forward it to a web application, API, or another internal service. The resulting response is then returned to the client.
This architecture makes it possible to centrally manage external connections, TLS certificates, and request routing while hiding the application’s internal structure from users.
Nginx can proxy more than just HTTP requests. Depending on its configuration and the modules being used, it can work with various network protocols.
Load Balancing
Nginx can distribute incoming requests among multiple backend servers. This approach is used to improve performance and scale web applications.
For example, instead of running on a single server, an application can run simultaneously on four servers. Nginx receives user requests and distributes them among these instances.
Different load-balancing methods can be used to select a backend server:
- Round robin — requests are distributed among available servers in turn;
- Least connections — the request goes to the server with the fewest active connections;
- IP hash — requests from a client are directed to the same server based on its IP address;
- weighted distribution and other algorithms.
For example, least connections helps when request processing times vary widely, while IP hash is useful when a session needs to stay tied to a particular server.
However, load balancing alone does not guarantee complete infrastructure fault tolerance. To eliminate a single point of failure, the load-balancing layer itself must also be made redundant.
Caching in Nginx
When operating as a reverse proxy, Nginx can store responses from backend servers in a cache. If another user requests the same resource and the caching conditions are met, Nginx can return the stored copy without contacting the application again.
This reduces the load on backend systems and, in some scenarios, shortens response times.
Caching rules can be configured separately for different resources. For example, static or rarely changing content can be cached for longer periods, while dynamic data may not be cached at all.
Nginx Configuration
Nginx is configured through configuration files. These files define listening addresses and ports, domain names, file locations, routing rules, TLS settings, proxy servers, caching, and other parameters.
Separate server blocks are used to host multiple websites on a single server. Within these blocks, location blocks can define different request-handling rules depending on the URI.
For example, requests to /static/ can be served directly by Nginx, while requests to /api/ can be forwarded to an application server.
This approach allows a single Nginx instance to act as a unified entry point for multiple components of a web system.
Nginx and Apache HTTP Server
Nginx and Apache HTTP Server perform many of the same tasks but differ in their architecture and configuration approaches.
Nginx was originally designed around an event-driven connection-handling model and has become widely used as a high-performance web server, reverse proxy, and load balancer.
Apache has an extensive modular architecture and supports, among other features, directory-level configuration through .htaccess files, which Nginx does not use.
The two servers are not necessarily mutually exclusive. In the same infrastructure, Nginx can operate in front of Apache and provide reverse proxying, TLS termination, static content delivery, or load balancing.
Where Nginx Is Used
Nginx is used to host websites, web applications, and APIs, as well as in microservices and cloud architectures. It can be deployed on a physical server, virtual machine, or in a container environment.
A typical use case is to deploy Nginx as an external entry point that receives HTTPS requests and distributes them among internal applications.
There is an open-source version, NGINX Open Source, as well as commercial products based on NGINX. The specific feature set and support model therefore depend on the edition and solution being used.