Top 40 Nginx Interview Questions and Answers You Must Know
This comprehensive guide covers everything from basic Nginx concepts, architecture, and configuration files to advanced topics such as load balancing algorithms, rate limiting, health checks, compression, and the differences between Nginx and Apache, providing clear explanations and code examples for each interview question.
What is Nginx?
Nginx is a lightweight, high‑performance reverse‑proxy web server that supports HTTP, HTTPS, SMTP, POP3 and IMAP, capable of handling tens of thousands of concurrent connections.
Key Advantages of Nginx
Cross‑platform and easy to configure.
Non‑blocking, high‑concurrency handling (2‑5 × 10⁴ connections).
Low memory usage (10 workers use ~150 MB).
Open‑source and cost‑effective.
High stability with rare crashes.
Built‑in health‑check to avoid routing to failed back‑ends.
Typical Use Cases
Standalone HTTP server for static content.
Virtual hosting multiple sites on one machine.
Reverse proxy and load balancing for high‑traffic applications.
API gateway and security management.
How Nginx Processes a Request
server {
# first server block – independent virtual host
listen 80; # listening port
server_name localhost; # host name
location / {
root html; # site root directory
index index.html index.html;
}
}On startup Nginx parses the configuration, creates a listening socket, forks worker processes, and each worker accepts connections, registers read/write events, processes the request, and finally closes the connection.
High‑Concurrency Mechanism
Nginx uses an asynchronous, non‑blocking event‑driven model (epoll) so a small number of worker processes can handle a large number of simultaneous connections without allocating a thread per request.
Forward and Reverse Proxy
A forward proxy sits between a client and the origin server, forwarding client requests. A reverse proxy receives external requests and forwards them to internal servers, hiding the origin servers and providing load balancing and security.
Directory Structure
# tree /usr/local/nginx
/usr/local/nginx
├── client_body_temp
├── conf
│ ├── fastcgi.conf
│ ├── mime.types
│ ├── nginx.conf
│ └── ...
├── html
│ ├── 50x.html
│ └── index.html
├── logs
│ ├── access.log
│ └── error.log
├── sbin
│ └── nginx
└── ...nginx.conf Main Modules
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
...
}Cookie vs Session
Both store key‑value data. Cookies reside on the client browser and are visible to the user; sessions are stored on the server (file, DB, Redis) and can hold sensitive information.
Why Nginx Does Not Use Multithreading
Unlike Apache, which creates a thread or process per request, Nginx handles many requests within a single worker using asynchronous I/O, reducing CPU and memory consumption.
Nginx vs Apache
Lightweight, lower memory footprint.
Asynchronous, better under high concurrency.
Modular design.
Apache uses a synchronous multi‑process model.
Static‑Dynamic Resource Separation
Static files (CSS, JS, images) are served directly by Nginx, while dynamic requests are proxied to application servers such as Tomcat, improving response speed and reducing backend load. CDN can further cache static assets.
Load‑Balancing Algorithms
Round‑robin (default).
Weight‑based.
IP‑hash.
Fair (third‑party module).
URL‑hash (third‑party module).
# round‑robin
upstream backserver {
server 192.168.0.12;
server 192.168.0.13;
}Cross‑Domain Solution
Configure Nginx to proxy cross‑origin API requests through the same origin, eliminating CORS issues.
Virtual Host Configuration
# domain‑based virtual host
server {
listen 80;
server_name www.example.com;
location / {
root /data/www;
index index.html;
}
}
# port‑based virtual host
server {
listen 8080;
server_name 8080.example.com;
location / {
root /data/www;
index index.html;
}
}Location Directive
The location block matches request URIs and applies specific handling. Examples include exact match, prefix match, case‑sensitive and case‑insensitive regex, and fallback.
# exact match
location = / { return 400; }
# prefix match
location ^~ /av { root /data/av/; }
# regex match
location ~ /media { alias /data/static/; }
# file extension match
location ~* \.(jpg|gif|png|js|css)$ { root /data/av/; }
# default
location / { return 403; }Rate Limiting
Nginx implements rate limiting using the leaky‑bucket algorithm via ngx_http_limit_req_module and connection limiting via ngx_http_limit_conn_module.
# limit one request per minute per IP
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m;
server {
location /seckill.html {
limit_req zone=one;
proxy_pass http://backend;
}
}Health Check
Backend health can be monitored with the built‑in proxy/upstream modules or the third‑party ngx_http_upstream_check_module.
Compression
Enable gzip to reduce bandwidth for text resources.
http {
gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain application/javascript text/css;
gzip_vary on;
}Other Topics
ngx_http_upstream_module defines server groups for proxy, fastcgi, uwsgi, etc.
C10K problem: handling 10 000 simultaneous sockets.
Gunzip module can decompress upstream responses.
Obtain current time with SSI $date_gmt.
Adding modules requires recompilation.
Set worker_processes to the number of CPU cores for optimal performance.
Common status codes: 499 (client closed), 502 (upstream error), etc.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
