Prev Next

Web / NGINX Interview questions

1. What is NGINX? 2. What is the purpose of NGINX as a reverse proxy? 3. What are the main features of NGINX? 4. What are the types of context blocks in NGINX configuration? 5. Define upstream in NGINX? 6. What is the master-worker process model in NGINX? 7. List common NGINX configuration directives? 8. How do you install NGINX on Ubuntu? 9. How do you start, stop, and reload NGINX? 10. What is the nginx.conf file? 11. What are server blocks in NGINX? 12. What is a location block in NGINX? 13. How do you serve static files with NGINX? 14. What is load balancing in NGINX? 15. What are the types of load balancing methods in NGINX? 16. Define a virtual host in NGINX? 17. What is SSL termination in NGINX? 18. How do you enable Gzip compression in NGINX? 19. What is caching in NGINX? 20. What is the purpose of the events block in NGINX? 21. What is the difference between NGINX and Apache HTTP Server? 22. What is the difference between proxy_pass and rewrite in NGINX? 23. What is the difference between a forward proxy and a reverse proxy? 24. How does NGINX handle concurrent client connections? 25. How does NGINX's event-driven architecture work? 26. Why is NGINX considered more performant than process-per-connection servers? 27. Why do we use upstream blocks with multiple servers? 28. When should you use NGINX purely as a reverse proxy versus also as a load balancer? 29. When would you choose round robin over least_conn load balancing? 30. What happens when an upstream server fails a health check? 31. How is SSL/TLS termination configured in NGINX? 32. How can you optimize NGINX for high-traffic websites? 33. How do you troubleshoot a 502 Bad Gateway error in NGINX? 34. How do you troubleshoot high memory usage in NGINX worker processes? 35. Explain the lifecycle of an HTTP request in NGINX? 36. Explain the execution flow of NGINX's request processing phases? 37. Explain the internal working of NGINX worker processes and the event loop? 38. Why doesn't NGINX use a thread-per-request model like Apache's prefork MPM? 39. Why should location block matching order matter between regex and prefix matches? 40. What is the difference between try_files and rewrite directives? 41. How does NGINX handle SSL session caching for performance? 42. How can you optimize NGINX buffer settings for large file uploads? 43. What is the difference between active and passive health checks in NGINX? 44. How do you troubleshoot NGINX configuration errors before reloading? 45. Why is the worker_connections directive important for concurrency limits? 46. When should you use NGINX caching instead of a dedicated CDN? 47. What is the difference between limit_req and limit_conn for rate limiting? 48. How does NGINX handle WebSocket proxying? 49. Explain the internal working of NGINX's keepalive connection pooling to upstream servers? 50. Why do we use try_files with a fallback to PHP-FPM in WordPress-style setups?

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 of resources. Unlike traditional servers that spawn a new pr...

Read full answer

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 NGINX, never directly to the applic...

Read full answer

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. Static content serving - delivering HTML, CSS, JS, and media f...

Read full answer

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 . http - wraps all ...

Read full answer

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. upstream backend_app { server 10.0.0.11:8080 ; server 10.0.0.12:8080 ; } server { location / { proxy_pass ...

Read full answer

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. Workers run as an unprivileged user and never touch configurati...

Read full answer

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. root - the filesystem path used to serve static files. index - the default file served for directory requests,...

Read full answer

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 status nginx or by visiting the server's ...

Read full answer

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 process to spawn new workers ...

Read full answer

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 the events and http blocks. Rather than keeping ...

Read full answer

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 / { try_files $uri $uri/ =404 ; } } A single NGINX instan...

Read full answer

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 exact string ( = ), a prefix, or a r...

Read full answer

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. server { listen 80 ; server_name static.example.com ; root /var/www/static ; location / { try_files $uri $uri/ =404 ; } } With this se...

Read full answer

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. upstream backend_app { server 10.0.0.11:8080; server 10.0.0.12:8080; server 10.0....

Read full answer

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. ip_hash Rou...

Read full answer

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. server { listen 80 ; server_name shop.example.com ; root /var/www/shop ; } server { listen 80 ; server_name b...

Read full answer

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. server { listen 443 ssl ;...

Read full answer

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....

Read full answer

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 : 2 keys_zone = my_cache : 10m max_size = 1g ; server { location / { proxy_...

Read full answer

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 ; } worker_connections sets the maximum simultaneous co...

Read full answer

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. Traditionally process- or thread-per-connection (prefork/worker MPMs),...

Read full answer

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 passing the request to an upstream application, which retu...

Read full answer

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. Sits in front of servers, acting on their behalf. Server doesn't know the real client; sees the proxy instead. Client doesn't know the rea...

Read full answer

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 BSD/macOS. Instead of blocking ...

Read full answer

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. It registers all open sockets with the OS's efficient event notification interface, the...

Read full answer

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 the classic C10k...

Read full answer

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 service down for every user. With several servers in the pool, NGIN...

Read full answer

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 a single backend instance can'...

Read full answer

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. least_conn is the better choice when request processin...

Read full answer

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...

Read full answer

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 :! MD5; location / { proxy_pass http: // backend_app; } } ssl_protocols and ssl_ciphe...

Read full answer

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. Keepalive to upstreams - configure a keepalive connection ...

Read full answer

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 the backend application is actually running, via its process status or listening...

Read full answer

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 per-connection memory use. Exce...

Read full answer

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. It then reads and parses the HTTP request li...

Read full answer

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 to most configurations, in order, ar...

Read full answer

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 event notification interf...

Read full answer

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 grows painfully as connection counts ris...

Read full answer

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 order is: exact match ( location = /path ) first, the...

Read full answer

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. Rewrites the request URI using a regex pattern, without checking the filesystem. Falls back to a named lo...

Read full answer

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. ssl_session_cache shared:SSL:10m; ssl_se...

Read full answer

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...

Read full answer

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. Requires NGINX Plus's health_check directive. Detects failure only from real clie...

Read full answer

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 if everything checks out. It catches missing semicolons, unmatched braces, ...

Read full answer

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...

Read full answer

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 origin server, when cached content changes frequently e...

Read full answer

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. 10 requests per second. Limits the number of simultaneous open connections per key. Uses a leaky-bucket algorithm, can allow controlled bursts...

Read full answer

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 http://backend_app; ...

Read full answer

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 and CPU cost. The keepalive directive inside an upstream b...

Read full answer

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; } location ~ \.php$ { fastcgi_pass unix:/run/php/php-...

Read full answer

«
»

Comments & Discussions