Sub topic 1
What Nginx Is and How It Works
Prerequisites
This chapter assumes basic Linux command-line use, familiarity with sudo and text editors, and a basic understanding of HTTP requests, DNS names, and TCP ports. You should know that a client sends an HTTP request to a server and receives an HTTP response.
The role of Nginx
Nginx is a web server and reverse proxy. A web server returns files such as HTML, CSS, JavaScript, and images. A reverse proxy accepts requests on behalf of another application and forwards them to that application.
Nginx commonly listens on TCP port 80 for HTTP and port 443 for HTTPS. It examines each request, chooses a configuration rule, and then performs an action:
- Return a file from the local filesystem.
- Forward the request to an application server.
- Redirect the client to another URL.
- Return an error such as
404 Not Foundor502 Bad Gateway. - Pass the request to one of several backend servers.
Nginx differs from an application server. Nginx does not normally contain your business logic or database code. It handles connections and HTTP traffic, while an application such as Node.js, Python, PHP, or Java performs application work.
How Nginx handles connections
Nginx uses an event-driven architecture. A small master process reads configuration, opens listening sockets, and manages worker processes. Worker processes handle many connections by waiting for network events instead of creating one dedicated process for every connection.
This design lets Nginx handle many simultaneous connections with relatively low memory use. It is especially effective for static files, connection management, TLS termination, and proxying. The application behind Nginx may still become the performance bottleneck.
The master process does not normally serve requests itself. Workers inherit listening sockets and process traffic. If a worker fails, the master can start a replacement. A configuration reload starts workers with the new configuration while existing workers finish their current requests.
Configuration determines behavior
Nginx configuration consists of directives. A directive is a setting or instruction that ends with a semicolon. Directives appear inside contexts, which are nested blocks that define where a setting applies.
The http context contains HTTP-wide settings. A server block describes one virtual host, such as example.com. A location block selects behavior for a request URI.
server {
listen 80;
server_name example.com;
location / {
root /var/www/example.com/html;
}
location /api/ {
proxy_pass http://127.0.0.1:3000;
}
}In this example, requests for / can receive files from the local directory. Requests beginning with /api/ are sent to an application listening on port 3000.
Common errors and trade-offs
A frequent mistake is treating Nginx as the application itself. Nginx can serve files and proxy requests, but it cannot replace the application server’s logic.
Another mistake is changing configuration and reloading without testing it. A syntax error can prevent a reload or leave the service unable to start after a restart. Use nginx -t before applying changes.
Also verify the complete request path. DNS must point to the correct host, the firewall must allow the listening port, and the backend application must be reachable from Nginx. A 502 Bad Gateway usually means Nginx could not obtain a valid response from the upstream application.
Exercise
A client requests /api/orders, and Nginx has one location / block and one location /api/ block. Which block should handle the request, and why?
Show answer
The /api/ block should handle it because its prefix matches the request more specifically than the general / block.