Top 40 Nginx Interview Questions and Answers Explained
This comprehensive guide covers the most common Nginx interview questions, including its definition, advantages, architecture, request handling, high‑concurrency mechanisms, proxy types, configuration files, load‑balancing algorithms, rate limiting, health checks, compression, and many practical code examples for real‑world deployment.
Top 40 Nginx Interview Questions
1. What is Nginx?
Nginx is a lightweight, high‑performance reverse‑proxy web server that supports HTTP, HTTPS, SMTP, POP3 and IMAP protocols. It can handle 20‑30 k concurrent connections (officially up to 50 k) and is used by major sites such as Sina, NetEase, and Tencent.
2. What are the advantages of Nginx?
Cross‑platform and simple configuration.
Non‑blocking, high‑concurrency handling (2‑3 k concurrent connections, up to 5 k officially).
Low memory consumption (10 workers use ~150 MB).
Open‑source and low cost.
High stability with very low crash probability.
Built‑in health‑check: failed upstream servers are automatically bypassed.
3. Typical Nginx use cases
HTTP server – can serve static web pages.
Virtual hosting – multiple sites on a single server.
Reverse proxy & load balancing – distribute traffic across a cluster.
API gateway – intercept and manage backend services.
4. How does Nginx process a request?
server {
# first server block – independent virtual host
listen 80;
server_name localhost;
location / {
root html;
index index.html index.html;
}
}When Nginx starts, it parses the configuration, creates a listening socket, forks worker processes, and workers compete to accept new connections. After a connection is accepted, Nginx creates a ngx_connection_t structure, registers read/write event handlers, exchanges data, and finally closes the connection.
5. How does Nginx achieve high concurrency?
Nginx uses an asynchronous, non‑blocking event model (epoll). Workers handle many connections by registering events for operations that may block (e.g., upstream requests) and continue processing other connections while waiting, thus a few processes can serve massive concurrency.
6. What is a forward proxy?
A forward proxy sits between the client and the origin server; the client sends requests to the proxy, which forwards them to the origin server and returns the response.
7. What is a reverse proxy?
A reverse proxy accepts Internet requests, forwards them to internal servers, and returns the responses to the client, effectively hiding the origin servers.
8. Nginx directory structure
[root@localhost ~]# tree /usr/local/nginx
/usr/local/nginx
├── client_body_temp
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp
├── html
│ ├── 50x.html
│ └── index.html
├── logs
│ ├── access.log
│ ├── error.log
│ └── nginx.pid
├── proxy_temp
├── sbin
│ └── nginx
├── scgi_temp
└── uwsgi_temp9. Main directives in nginx.conf
worker_processes 1; # number of workers
events {
worker_connections 1024; # max connections per worker
}
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;
}
}
}10. Difference between cookie and session
Cookie – stored on client browser, one per domain, can be viewed/modified, set via HTTP response.
Session – stored on server (file, DB, Redis), holds sensitive data, acts as a lock.
11. Why doesn’t Nginx use multithreading?
Apache creates many processes/threads, each consuming CPU and memory. Nginx uses a single‑threaded, asynchronous, non‑blocking model (epoll) where workers handle many connections without per‑request CPU allocation, reducing context switches and resource usage.
12. Differences between Nginx and Apache
Lightweight, lower memory and resource usage.
Asynchronous non‑blocking handling vs. Apache’s blocking model.
Modular design, easier to write modules.
Core difference: Apache is process‑based, Nginx is event‑based.
13. What is static‑dynamic resource separation?
Separate immutable static files (CSS, JS, images) from dynamic content so static files can be cached, improving response speed.
14. Why perform static‑dynamic separation?
Static files bypass backend processing, reducing load on application servers and speeding up delivery, especially when combined with CDN caching.
15. What is a CDN?
A Content Delivery Network distributes content to edge nodes close to users, reducing latency and increasing bandwidth.
16. How does Nginx implement static‑dynamic separation?
location /image/ {
root /usr/local/static/;
autoindex on;
}Steps: create directory, upload files, reload Nginx, then access via http://server_name/image/photo.jpg.
17. Nginx load‑balancing algorithms
Round‑robin (default).
Weight‑based – higher weight gets more traffic.
IP‑hash – same client IP always goes to the same upstream.
Fair (third‑party) – considers response time.
URL‑hash (third‑party) – hashes request URI.
# round‑robin example
upstream backserver {
server 1112;
server 1113;
}18. Solving front‑end cross‑origin issues with Nginx
Configure Nginx to proxy cross‑origin API calls as same‑origin requests, then forward to the real backend.
19‑21. Virtual host configurations
Domain‑based virtual host – separate sites by server_name.
Port‑based virtual host – differentiate by listening port.
IP‑based virtual host – differentiate by IP address.
# domain‑based example
server {
listen 80;
server_name www.rumenz.com;
location / { root data/www; index index.html index.htm; }
}
server {
listen 80;
server_name bbs.rumenz.com;
location / { root data/bbs; index index.html index.htm; }
}22. Purpose of the location directive
Matches request URIs and applies specific configuration (root, proxy, return, etc.). Syntax includes exact match (=), prefix (^~), case‑sensitive regex (~), case‑insensitive regex (~*), and generic (/).
23. location regex examples
# exact match
location =/ { return 400; }
# prefix match (case‑sensitive)
location ^~ /av { root /data/av/; }
# case‑sensitive regex
location ~ /media { alias /data/static/; }
# case‑insensitive regex for static files
location ~* .*(jpg|gif|png|js|css)$ { root /data/av/; }
# generic match
location / { return 403; }24‑27. Rate limiting and connection limiting
Nginx uses the ngx_http_limit_req_module (leaky‑bucket) and ngx_http_limit_conn_module (concurrent connections). Example:
# leaky‑bucket: 1 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://lj_seckill;
}
}Burst and nodelay allow short spikes. Connection limiting example:
http {
limit_conn_zone $binary_remote_addr zone=myip:10m;
limit_conn_zone $server_name zone=myServerName:10m;
}
server {
location / {
limit_conn myip 10;
limit_conn myServerName 100;
}
}28‑30. Miscellaneous directives
Block undefined server names with an empty server_name and return a non‑standard code.
Restrict browsers (e.g., block Chrome) using if ($http_user_agent ~ Chrome) { return 500; }.
Rewrite global variables – list of Nginx variables such as $remote_addr, $host, $request_uri, etc.
31‑33. Health checks, gzip compression, and upstream module
Health checks: built‑in ngx_http_proxy_module / ngx_http_upstream_module or third‑party nginx_upstream_check_module.
Enable gzip:
http {
gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain application/javascript text/css application/xml image/jpeg image/gif image/png;
gzip_vary on;
} ngx_http_upstream_moduledefines server groups for proxy, fastcgi, uwsgi, etc.
34‑35. C10K problem and upstream compression
C10K refers to the difficulty of handling 10 000 concurrent sockets. Nginx solves it with its event‑driven model. The gunzip module can decompress upstream gzip responses.
36‑39. Miscellaneous utilities
Get current time: use SSI or $date_gmt header. -s option controls Nginx signals (e.g., reload, stop).
Compile‑time module selection – modules must be compiled into Nginx.
Set worker processes equal to CPU cores for optimal performance.
40. Nginx status codes
499 – client closed connection.
502 – bad gateway; possible causes include FastCGI not started, insufficient workers, timeout, buffer limits, 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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
