Web / NGINX Interview questions
1. What is NGINX?
NGINX is an open-source web server that also works as a reverse proxy, load balancer, mail proxy, and HTTP cache. It was created by Igor Sysoev to solve the C10k problem - handling thousands of concurrent connections without running out...
2. What is the purpose of NGINX as a reverse proxy?
As a reverse proxy, NGINX sits between clients and one or more backend servers, accepting incoming requests on the client's behalf and forwarding them to the appropriate upstream application. This hides backend implementation details - clients only ever talk to...
3. What are the main features of NGINX?
NGINX bundles several capabilities that would otherwise require separate tools: Reverse proxying - forwarding client requests to one or more backend applications. Load balancing - distributing traffic across multiple upstream servers....
4. What are the types of context blocks in NGINX configuration?
NGINX configuration is organized into nested context blocks, each controlling directives for a specific scope: main - top-level context, outside any braces; sets global settings like worker processes and user. events - configures connection handling, such as worker_connections ....
5. Define upstream in NGINX?
An upstream block defines a named group of one or more backend servers that NGINX can proxy requests to. It's declared at the http level and referenced by name inside a proxy_pass directive....
6. What is the master-worker process model in NGINX?
NGINX runs as one master process and one or more worker processes. The master process reads and validates the configuration, binds to the configured ports as root, and then spawns worker processes that do the actual request handling....
7. List common NGINX configuration directives?
Some directives appear in almost every NGINX configuration: listen - the port and optional address a server block binds to. server_name - the hostname(s) that route to this server block....
8. How do you install NGINX on Ubuntu?
sudo apt update sudo apt install nginx sudo systemctl enable nginx sudo systemctl start nginx This installs NGINX from Ubuntu's default repositories, sets it to start on boot, and starts the service immediately. You can confirm it's running with systemctl...
9. How do you start, stop, and reload NGINX?
sudo systemctl start nginx sudo systemctl stop nginx sudo systemctl restart nginx sudo systemctl reload nginx restart stops and starts the whole process, briefly dropping connections. reload is the preferred option after a configuration change - it signals the master...
10. What is the nginx.conf file?
nginx.conf is NGINX's main configuration file, typically located at /etc/nginx/nginx.conf . It defines global settings in the main context - things like the number of worker processes, the user NGINX runs as, and error log location - and then opens...
11. What are server blocks in NGINX?
A server block is NGINX's equivalent of Apache's virtual host - it defines how requests for a particular domain, port, or IP should be handled. server { listen 80 ; server_name example.com www.example.com ; root /var/www/example ; location / {...
12. What is a location block in NGINX?
A location block, nested inside a server block, matches part of the request URI and defines how requests matching that pattern should be handled. location /api/ { proxy_pass http://backend_app; } location /static/ { root /var/www/example; } Matching can use an...
13. How do you serve static files with NGINX?
Serving static files is one of NGINX's core strengths. The root directive points to a directory on disk, and NGINX maps the request URI onto a file path under it....
14. What is load balancing in NGINX?
Load balancing is the practice of distributing incoming requests across multiple backend servers instead of sending all traffic to one. In NGINX, this is done through an upstream block listing the available servers....
15. What are the types of load balancing methods in NGINX?
NGINX supports several load-balancing algorithms, selected by a directive inside the upstream block: Method Behavior Round robin (default) Requests are distributed sequentially across servers in order. least_conn Sends the next request to the server with the fewest active connections....
16. Define a virtual host in NGINX?
In NGINX, a virtual host is implemented as a server block that responds to a specific server_name , IP, and port combination. It lets one physical machine host multiple distinct websites....
17. What is SSL termination in NGINX?
SSL termination means NGINX decrypts incoming HTTPS traffic and forwards the request to backend servers as plain HTTP (or re-encrypts it separately). The client-facing connection is encrypted; the internal connection typically isn't, unless the backend also requires TLS....
18. How do you enable Gzip compression in NGINX?
gzip on; gzip_types text/plain text/css application/json application/javascript text/xml; gzip_min_length 256; gzip_comp_level 5; gzip_types restricts compression to text-based formats where it actually helps - compressing already-compressed formats like JPEG or MP4 wastes CPU for little benefit.
19. What is caching in NGINX?
NGINX caching stores responses from an upstream server on disk or in memory so that repeated requests for the same content can be served without hitting the backend again. proxy_cache_path / var / cache / nginx levels = 1 :...
20. What is the purpose of the events block in NGINX?
The events block, placed at the top level of nginx.conf alongside http , controls how NGINX handles network connections at a low level rather than how it processes HTTP requests. events { worker_connections 1024 ; use epoll ; multi_accept on...
21. What is the difference between NGINX and Apache HTTP Server?
Both are mature, widely used web servers, but they differ fundamentally in how they handle connections and configuration. NGINX Apache Event-driven, asynchronous architecture; a fixed number of workers handle many connections.
22. What is the difference between proxy_pass and rewrite in NGINX?
proxy_pass and rewrite solve different problems, even though both can change where a request ends up. proxy_pass forwards a request to a different server entirely - the URL the client sees stays the same, but NGINX acts as an intermediary...
23. What is the difference between a forward proxy and a reverse proxy?
The distinction comes down to which side of the connection the proxy represents. Forward Proxy Reverse Proxy Sits in front of clients, acting on their behalf....
24. How does NGINX handle concurrent client connections?
NGINX handles concurrency with an event-driven, non-blocking model rather than allocating a dedicated OS thread or process per connection. Each worker process runs an event loop built on the operating system's efficient polling mechanism - epoll on Linux, kqueue on...
25. How does NGINX's event-driven architecture work?
NGINX's event-driven architecture is what lets a handful of worker processes serve tens of thousands of connections without the overhead of one thread per client. Each worker runs a single-threaded event loop....
26. Why is NGINX considered more performant than process-per-connection servers?
Process-per-connection (or thread-per-connection) servers pay a fixed cost for every open connection: memory for the process/thread stack, and CPU time for the OS to context-switch between them. As concurrent connections climb into the thousands, that overhead compounds - this is...
27. Why do we use upstream blocks with multiple servers?
Listing multiple servers in an upstream block turns a single point of failure into a pool that NGINX can spread load across and route around problems in. With only one backend server, any restart, deploy, or crash takes the whole...
28. When should you use NGINX purely as a reverse proxy versus also as a load balancer?
A pure reverse proxy setup makes sense when there's only one backend instance - NGINX still adds value by terminating SSL, serving static assets, and hiding the backend, even without distributing traffic across multiple servers. Load balancing becomes necessary once...
29. When would you choose round robin over least_conn load balancing?
Round robin works well when backend requests are roughly uniform in cost - similar processing time and resource use - and all servers in the pool have comparable capacity. It's simple, predictable, and has no per-request bookkeeping overhead....
30. What happens when an upstream server fails a health check?
When passive health checks are configured via max_fails and fail_timeout on a server line, NGINX tracks failed connection attempts or error responses from that upstream server as real traffic flows through. upstream backend_app { server 10.0.0.11:8080 max_fails=3 fail_timeout=30s; server 10.0.0.1
31. How is SSL/TLS termination configured in NGINX?
server { listen 443 ssl ; server_name example.com ; ssl_certificate /etc/nginx/ssl/example.com.crt ; ssl_certificate_key /etc/nginx/ssl/example.com.key ; ssl_protocols TLSv1.2 TLSv1.3 ; ssl_ciphers HIGH: ! aNULL :!
32. How can you optimize NGINX for high-traffic websites?
Several tuning levers matter most once traffic climbs into high-concurrency territory. Worker tuning - set worker_processes auto; to match CPU core count, and raise worker_connections so each worker can hold more simultaneous connections....
33. How do you troubleshoot a 502 Bad Gateway error in NGINX?
A 502 means NGINX successfully received a request but got an invalid or no response from the upstream server it tried to proxy to - the problem is almost always on the backend side, not in NGINX itself. Check whether...
34. How do you troubleshoot high memory usage in NGINX worker processes?
High worker memory usage usually traces back to a handful of common causes rather than a leak in NGINX itself, which is written to be lean by design. Large buffers - oversized proxy_buffers , client_body_buffer_size , or large_client_header_buffers settings inflate...
35. Explain the lifecycle of an HTTP request in NGINX?
Every request NGINX handles moves through a defined sequence of processing stages, regardless of whether it ends up being served locally or proxied elsewhere. First, NGINX accepts the TCP connection and, if TLS is involved, completes the SSL handshake....
36. Explain the execution flow of NGINX's request processing phases?
Internally, NGINX processes every request through a fixed sequence of eleven phases, and most custom logic - whether from core directives or third-party modules - hooks into one of these phases rather than running arbitrary, unordered code. The phases relevant...
37. Explain the internal working of NGINX worker processes and the event loop?
Each NGINX worker process is single-threaded by default but handles many connections concurrently through an event loop rather than through parallel threads. On startup, a worker registers all the listening sockets it inherited from the master with the operating system's...
38. Why doesn't NGINX use a thread-per-request model like Apache's prefork MPM?
A thread- or process-per-request model has to pay fixed overhead for every connection: allocating a stack, and having the OS scheduler context-switch between threads as it decides which gets CPU time next. That overhead is manageable at low concurrency but...
39. Why should location block matching order matter between regex and prefix matches?
NGINX doesn't simply match location blocks in the order they appear in the config file - it applies a specific precedence, and misunderstanding that precedence is a common source of "why isn't my location block being used" bugs. The evaluation...
40. What is the difference between try_files and rewrite directives?
Both directives affect how NGINX resolves a request internally, but they operate very differently. try_files rewrite Checks a list of file paths on disk in order, using the first that exists....
41. How does NGINX handle SSL session caching for performance?
A full TLS handshake is computationally expensive - it involves asymmetric cryptography and multiple round trips before a connection can carry data. SSL session caching avoids repeating that work for clients who reconnect shortly after their first request....
42. How can you optimize NGINX buffer settings for large file uploads?
client_max_body_size 100M; client_body_buffer_size 128k; client_body_timeout 60s; client_max_body_size sets the hard ceiling on upload size; requests larger than this are rejected outright with a 413 before consuming resources unnecessarily. client_body_buffer_size controls how much of the body N
43. What is the difference between active and passive health checks in NGINX?
Both mechanisms exist to keep traffic away from unhealthy backend servers, but they detect failure differently. Passive Health Checks Active Health Checks Available in open-source NGINX via max_fails / fail_timeout....
44. How do you troubleshoot NGINX configuration errors before reloading?
sudo nginx -t This command parses the full configuration tree - including all included files - and reports the exact file and line number of any syntax error, along with a final "syntax is ok / test is successful" message...
45. Why is the worker_connections directive important for concurrency limits?
events { worker_connections 1024 ; } worker_connections sets the maximum number of simultaneous connections a single worker process will accept, and it directly caps NGINX's total concurrency ceiling. Total theoretical concurrent connections is roughly worker_processes × worker_connections , thou
46. When should you use NGINX caching instead of a dedicated CDN?
NGINX caching and a CDN solve overlapping but distinct problems, and the right choice depends on where your traffic and latency concerns actually are. NGINX's proxy_cache is a good fit when traffic is concentrated in one region close to your...
47. What is the difference between limit_req and limit_conn for rate limiting?
Both directives protect backends from being overwhelmed, but they limit different things. limit_req limit_conn Limits the rate of requests over time, e.g....
48. How does NGINX handle WebSocket proxying?
WebSockets start as a normal HTTP request that "upgrades" to a persistent, bidirectional TCP connection - and by default, NGINX's proxying doesn't forward the headers that make that upgrade work, so WebSocket connections need explicit configuration. location /ws/ { proxy_pass...
49. Explain the internal working of NGINX's keepalive connection pooling to upstream servers?
By default, NGINX opens a new TCP connection to an upstream server for every proxied request and closes it afterward - fine at low traffic, but wasteful at scale, since TCP handshakes and (if applicable) TLS negotiation carry real latency...
50. Why do we use try_files with a fallback to PHP-FPM in WordPress-style setups?
WordPress and similar PHP applications rely on a single front controller - typically index.php - to handle routing for URLs that don't correspond to a real file on disk, like /blog/my-post/ . location / { try_files $uri $uri/ /index.php?$args; }...