DevOps

nginx

nginx is a fast web server and reverse proxy that also works as a load balancer and HTTP cache.

What it is

nginx was created by Igor Sysoev and the first public release came in 2004. It is built on an asynchronous event model, so it serves thousands of connections with few processes and little memory. It is now developed by F5.

Typical roles: serving static files, being a reverse proxy in front of an app (Node, Python, PHP-FPM), terminating TLS, balancing load across servers, caching responses and rate limiting.

Configuration is text blocks server and location in nginx.conf. Changes apply without downtime via nginx -s reload.

Use it when

  • You need a reverse proxy and TLS termination in front of an app.
  • You serve static files or cache responses.
  • You balance load across several servers.
  • You need high performance with many connections.

Avoid it when

  • You want automatic certificates out of the box with no setup: Caddy is simpler.
  • A container environment with dynamic service discovery: Traefik or an ingress controller is handier.

Key concepts

server / location
Config blocks: server describes a virtual host, location holds rules for paths.
Reverse proxy
Accepts client requests and forwards them to an app via proxy_pass.
upstream
A group of backend servers among which nginx balances load.
Event loop
An asynchronous model: a few workers serve many connections without a thread per connection.

Example

A reverse proxy with HTTPS and balancing across two app instances.

Interview questions

  1. 1. What is a reverse proxy and why use one?

    A server in front of an app that accepts client requests and forwards them. It can terminate TLS, balance load, cache, compress and shield the backend from direct access.

  2. 2. Why is nginx faster than a thread-per-connection model?

    It uses an asynchronous event model: a few workers handle many connections via epoll, so it does not spend memory and context switches on each client.

  3. 3. How do you update configuration without downtime?

    Check the config with nginx -t and run nginx -s reload. The master process starts new workers with the new config, while old ones finish current requests and exit.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).