Nginx
- Pronunciation
- EN-jin-EKS
In short
Nginx is a fast, open-source web server that is also widely used as a reverse proxy, load balancer and HTTP cache in front of application servers.
What is Nginx?
Nginx, pronounced "engine x", was created by Igor Sysoev and released in 2004 to solve a specific problem: serving many thousands of simultaneous connections on one machine. Instead of starting a new thread or process for every visitor, it uses a small number of worker processes that handle connections with an event loop, which keeps memory use low and steady under heavy load.
As a web server, Nginx serves static files such as HTML, CSS, images and downloads very efficiently. As a reverse proxy, it receives requests from the internet and forwards them to application servers written in Node.js, Python, PHP or anything else, so the app doesn't have to face the internet directly. On the way, it can terminate HTTPS, compress responses, cache them and enforce rate limits.
It can also act as a load balancer, spreading requests across several copies of an app, and as a gateway that routes paths like /api and /static to different places. All of this is described in a plain-text configuration file, usually nginx.conf, made of blocks such as server and location.
A common misconception is that Nginx runs your application code. It mostly passes requests to other processes that do; for PHP it talks to PHP-FPM, for Node.js it proxies to the Node server. It is one of the most used web servers in the world, alongside Apache httpd, and is also the basis of popular Kubernetes ingress controllers.
Key takeaways
- Nginx is an open-source web server, reverse proxy, load balancer and cache.
- An event-driven design lets it handle many thousands of connections cheaply.
- It serves static files and forwards dynamic requests to app servers.
- It can terminate HTTPS, compress, cache and rate-limit on the way.
- Its behavior is set in a plain-text config file, usually nginx.conf.
Example
server {
listen 80;
server_name example.com;
# Static files straight from disk
location /static/ {
root /var/www;
}
# Everything else goes to the app on port 3000
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Readers ask
How do you pronounce Nginx?
It is pronounced "engine x".
What is the difference between Nginx and Apache?
Both are popular open-source web servers. Nginx uses an event-driven design that handles many connections with little memory and is often used as a reverse proxy, while Apache httpd traditionally uses a process or thread per connection and supports per-directory .htaccess files.
Is Nginx a load balancer?
It can be. Besides serving files and proxying requests, Nginx can spread traffic across several upstream servers using methods such as round robin or least connections.
See also
- Web ServerBackend & APIs, p. 46A web server is software, or the machine running it, that accepts HTTP requests from browsers and other clients and responds with pages, files, or data.
- Reverse ProxyDevOps & Cloud, p. 44A reverse proxy is a server that sits in front of web servers, accepts client requests on their behalf, and forwards each request to the right backend server.
- Load BalancerDevOps & Cloud, p. 34A load balancer is a server or service that spreads incoming traffic across several backend servers so no single one is overloaded and the app stays available.
- HTTP CachingBackend & APIs, p. 22HTTP caching is the reuse of stored HTTP responses by browsers, CDNs and proxies, controlled by headers like Cache-Control and ETag, to avoid repeat downloads.
- TLSSecurity, p. 45TLS is the cryptographic protocol that encrypts data sent over a network and verifies the server's identity, securing HTTPS, email, and many other connections.
- Rate LimitingBackend & APIs, p. 37Rate limiting is a technique that caps how many requests a client can make to a server or API within a time window, protecting it from abuse and overload.
Spotted a mistake or something missing on this page?Suggest an edit