Nginx interview frequently asked questions





1. What is Nginx and what are its advantages?

Nginx, is a web server and reverse proxy server for HTTP, HTTPS, SMTP, POP3 and IMAP protocols.

The main functions are as follows:

1. Forward and reverse proxy
2. Load balancing, offloading
3. Virtual host (binding host)

advantage:

Cross-platform, simple configuration, non-blocking, high concurrent connection, low memory consumption, low cost.


2. What is the difference between forward proxy and reverse proxy?

Insert picture description here
Forward proxy

The forward proxy is a server located between the client and the origin server. In order to obtain content from the origin server, the client sends a request to the proxy and specifies the origin server, and then the proxy forwards the request to the origin server and returns the obtained content to the client end. The proxy server and the client are in the same LAN.


For example, fanqiang software. I knew I wanted to visit Google, so I told the proxy server to forward it for me.

Reverse proxy

The actual operation of the reverse proxy is that the proxy server accepts connection requests on the network. It forwards the request to the server on the internal network, and returns the result from the server to the client requesting connection on the network. The proxy server and the original server are in the same local area network.


For example, if I want to visit Taobao, I don't know if the pictures, json, css are returned from the same server, but I don't care, it is a reverse proxy processing, I don't know the original server.


3. How does Nginx handle HTTP requests?

It combines multi-process mechanism (single thread) and asynchronous non-blocking method.

1. Multi-process mechanism (single thread)

Whenever the server receives a client, the server master process (master process) generates a child process (worker process) to establish a connection with the client to interact, until the connection is disconnected, the child process ends.

2. Asynchronous non-blocking mechanism

Each worker process uses asynchronous non-blocking mode and can handle multiple client requests. The epoll model is used, a queue is provided, and the queue is resolved.


When a worker process receives the client's request, it calls IO for processing. If the result cannot be obtained immediately, it processes other requests (that is, non-blocking); and the client does not need to wait for a response during this period, and can process other Things (that is, asynchronous).


When the IO returns, the worker process will be notified; the process is notified and temporarily suspends the current transaction to respond to the client request.

Why is it so fast? You can refer to the official introduction of Nginx: http://www.aosabook.org/en/nginx.html


4. How do Nginx master and worker work?

This is related to Nginx's multi-process and single thread. (A process has only one main thread).

Why use single thread?

Single thread is used to process requests asynchronously and non-blocking (administrators can configure the number of worker processes of the Nginx main process), without allocating cpu and memory resources for each request, saving a lot of resources and reducing a lot of CPU context Switching, so that Nginx supports higher concurrency.

Simple process:

After the main program Master process is started, it receives and processes external signals through a for loop; the


main process generates worker subprocesses through the fork() function, and each subprocess executes a for loop to realize the reception and processing of events by the Nginx server.

Detailed process:

1. After Nginx is started, there will be a master process and multiple independent worker processes.


2. The master receives signals from the outside world, first establishes the socket (listenfd) that needs listen, and then forks out multiple worker processes, and then sends signals to each worker process. Each process may handle the connection.


3. The listenfd of all worker processes will become readable when a new connection arrives. To ensure that only one process handles the connection, all worker processes preempt accept_mutex before registering listenfd read events, and the process that grabs the mutex lock registers listenfd read Event, call accept in the read event to accept the connection.


4. When a worker process accepts the connection, it starts to read the request, parse the request, process the request, generate data, and then return it to the client, and finally disconnect.


5. What are the commonly used commands in Nginx?

启动             nginx
停止             nginx -s stop 或 nginx -s quit
重启             nginx -s reload 或 service nginx reload
重载指定配置文件  .nginx -c /usr/local/nginx/conf/nginx.conf
查看 nginx 版本  nginx -v

6. What is the difference between 500, 502, 503, 504 in nginx?

500:

Internal Server Error Internal service error, such as script error, programming language syntax error.

502:

Bad Gateway error, bad gateway. For example, the server currently has too many connections, the response is too slow, the page material is too much, and the bandwidth is slow.

503:

Service Temporarily Unavailable, the service is unavailable, the web server cannot handle HTTP requests, it may be temporarily overloaded or the server is down for maintenance.

504:

Gateway timeout Gateway timeout, the program execution time is too long and the response timeout, for example, the program needs to be executed for 20 seconds, and the maximum response waiting time of nginx is 10 seconds, so there will be a timeout.


7. Do you understand Nginx compression and how to enable compression?

After nginx gzip compression is enabled, the size of static resources such as pictures, css, js, etc. will be reduced, which can save bandwidth and improve transmission efficiency, but will consume CPU resources.

gzip on;  
#开启gzip压缩功能
gzip_min_length 1k;
#设置允许压缩的页面最小字节数,页面字节数从header头的content-length中获取。默认值是0,不管页面多大都进行压缩。建议设置成大于1k。如果小于1k可能会越压越大。
gzip_buffers 4 16k;
#压缩缓冲区大小。表示申请4个单位为16k的内容作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果。
gzip_http_version 1.0;
#压缩版本(默认1.1,前端为squid2.5时使用1.0)用于设置识别http协议版本,默认是1.1,目前大部分浏览器已经支持gzip解压,使用默认即可。
gzip_comp_level 2;
#压缩比率。用来指定gzip压缩比,1压缩比量小,处理速度快;9压缩比量大,传输速度快,但处理最慢,也必将消耗cpu资源。
gzip_types text/plain application/x-javascript text/css application/xml;
#用来指定压缩的类型,“text/html”类型总是会被压缩。
gzip_vary on;
#vary header支持。该选项可以让前端的缓存服务器缓存经过gzip压缩的页面,例如用squid缓存经过nginx压缩的数据。

Note: Objects that need and do not need to be compressed
(1) Plain text files html, js, css, xml, html that are larger than 1k.
(2) Do not compress pictures, videos, etc., because not only will it not be reduced, but will be consumed during compression cpu and memory resources.


8. The difference between Nginx and Apache and Tomcat

The difference between Tomcat and Nginx/Apache:

1. Nginx/Apache is a Web Server, and Apache Tomact is a servlet container

2. Tomcat can parse jsp, nginx and apache are just web servers, which can be simply understood as only providing html static file services.

The difference between Nginx and Apache:

1) Nginx is light-weight, which also serves as a web service, and takes up less memory and resources than Apache.

2) Nginx is anti-concurrency, nginx processes requests asynchronously and non-blocking, while apache is blocking. Under high concurrency, nginx can maintain low resources, low consumption and high performance.

3) Nginx provides load balancing, which can be used as a reverse proxy and front-end server

4) Nginx multi-process single thread, asynchronous non-blocking; Apache multi-process synchronization, blocking.


9. What load balancing strategies does Nginx have

Load balancing strategies provided by Nginx by default:

1. Polling (default) round_robin

Each request is allocated to different back-end servers one by one in chronological order. If the back-end server is down, it can be automatically eliminated.

2. IP hash ip_hash

Each request is allocated according to the hash result of the access ip, so that each visitor has fixed access to a back-end server, which can solve the problem of session sharing.
Of course, in actual scenarios, generally do not consider using ip_hash to solve session sharing.

3. At least connect least_conn

The next request will be dispatched to the server with the least number of active connections

4. weight

The larger the value of weight, the higher the access probability allocated. It is mainly used to achieve reasonable resource utilization when the performance of each back-end server is uneven. Other strategies can also be supported through plugins.


10. Has Nginx done the separation of dynamic and static resources? Why do you want to do it?

The separation of dynamic resources and static resources is to allow dynamic web pages in dynamic websites to distinguish constant resources from constantly changing resources according to certain rules.

For example, js, css, hrml are returned from the A server. The picture is returned from the B server, and other requests are returned from the Tomcat server C.

The background applications are deployed separately to improve the speed of users accessing static code. And now there are CDN services, there is no need to limit the bandwidth of the server.


11. Do you understand the ngx_http_upstream_module module?

The ngx_http_upstream_module module is used to define multiple servers into server groups, which can be referenced by fastcgi delivery, proxy delivery, uwsgi delivery, memcached delivery, and scgi delivery instructions.

For example, visit www.a.com cache + scheduling:

http{
    
    
    proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:2 keys_zone=proxycache:20m inactive=120s max_size=1g; # 缓存
    upstream mysqlsrvs{
    
    
        ip_hash;                        # 源地址hash调度方法 写了backup就不可用
        server 172.18.99.1:80 weight=2; # weight权重
        server 172.18.99.2:80;          # 标记down,配合ip_hash使用,实现灰度发布
        server 172.18.99.3:80 backup;   # backup将服务器标记为“备用”,即所有服务器均不可用时才启用 
    }
}
server{
    
    
    server_name www.a.com;
    proxy_cache proxycache;
    proxy_cache_key $request_uri;
    proxy_cache_valid 200 302 301 1h;
    proxy_cache_valid any 1m;
    location / {
    
    
        proxy_pass http://mysqlsrvs;
    }
}

12. Do you understand the current limitation and how is the current limitation?

Nginx provides two current limiting methods, one is to control the rate, and the other is to control the number of concurrent connections.

1. Control rate

Reference article .

The ngx_http_limit_req_module module provides a leaky bucket algorithm, which can limit the processing frequency of a single IP request.

1.1 Normal current limit:

基于客户端192.168.1.1进行限流,
定义了一个大小为10M,名称为myLimit的内存区,用于存储IP地址访问信息。
rate设置IP访问频率,rate=5r/s表示每秒只能处理每个IP地址的5个请求。
http {
    
    
	limit_req_zone 192.168.1.1 zone=myLimit:10m rate=5r/s;
}

server {
    
    
	location / {
    
    
		limit_req zone=myLimit;
		rewrite / http://www.hac.cn permanent;
	}
}

Parameter explanation:

limit_req_zone: Define the objects that need to be restricted.
zone: Define a shared memory zone to store access information.
rate: Used to set the maximum access rate.

Nginx current limit is based on the unit of milliseconds, which means that processing 5 requests in 1 second will become only processing one request every 200ms. If one request has been processed within 200ms, but a new request arrives, Nginx will refuse to process the request.

1.2 Burst traffic limits access frequency

The above rate is set to 5r/s. If sometimes the traffic suddenly becomes large, the request that exceeds the limit will be rejected and a 503 will be returned. The sudden traffic will affect the business. At this time, the burst parameter can be added, and it is generally used in conjunction with nodelay.

server {
    
    
	location / {
    
    
		limit_req zone=myLimit burst=20 nodelay;
		rewrite / http://www.hac.cn permanent;
	}
}

burst=20 nodelay means that these 20 requests are processed immediately and cannot be delayed, which is equivalent to special work. However, even if these 20 sudden requests are processed immediately, subsequent requests will not be processed immediately.

Burst=20 is equivalent to 20 pits in the cache queue. Even if the request is processed, these 20 positions can only be released in 100ms.


2, the control concurrent connections
reference articles .
Referenced article 2 .

ngx_http_limit_conn_module provides the function of limiting the number of connections.

limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m; 

server {
    
      
    listen       80;
    server_name  localhost;
    charset utf-8;
    location / {
    
    
        limit_conn perip 10;      # 单个客户端ip与服务器的连接数.
        limit_conn perserver 100; #限制与服务器的总连接数
        root   html;
        index  index.html index.htm;
    }
}

The key of limit_conn perip 10 is $binary_remote_addr, which means that a single IP can hold up to 10 connections at the same time.
The key of limit_conn perserver 100 is $server_name, which represents the total number of concurrent connections that the virtual host (server) can handle at the same time.

Guess you like

Origin blog.csdn.net/QiuHaoqian/article/details/112257367