current position:Home>Interviewer: how does nginx achieve high concurrency? What are the common optimization methods?

Interviewer: how does nginx achieve high concurrency? What are the common optimization methods?

2021-08-27 04:12:48 Migrant worker brother

This is an interview question brought back by a reader

Nginx How to achieve concurrency ? Why? Nginx No multithreading ?Nginx What are the common optimization methods ?502 What are the possible causes of the error ?

Psychological analysis of interviewers

Mainly depends on the candidate's right NGINX Are you familiar with the basic principles of , Because most people know more or less NGINX, But few of them really understand the principle . Understand its principle , To optimize , Otherwise, we can only copy the same , If something goes wrong, there is no way to start .

Interview questions :Nginx How to achieve high concurrency ? What are the common optimization methods ?

People who know fur , I usually do Web Server, Building a Web Site ; Primary operation and maintenance may be HTTPS 、 Configure a reverse proxy ; Intermediate operation and maintenance defines a upstream、 Write a regular judgment ; Old bird, do a performance optimization 、 Write a ACL, It is also possible to change the source code ( Xiaobian said he had no ability to change the source code ).

Analysis of interview questions

Nginx How to achieve high concurrency ?

asynchronous , Non blocking , Used epoll And a lot of underlying code optimization .

If one server Use one process to be responsible for one request The way , So the number of processes is the number of concurrent . Under normal circumstances , There will be many processes waiting .

and nginx Use a master process , Multiple woker The pattern of the process .

  • master The process is mainly responsible for collecting 、 Distribute the requests . Every time a request comes ,master Just pick up one worker The process is responsible for processing the request .
  • meanwhile master The process is also responsible for monitoring woker The state of , High reliability
  • woker The process is generally set to follow cpu The number of cores is the same .nginx Of woker The number of requests a process can process at the same time is limited by memory , Can handle multiple requests .
  • Nginx The asynchronous non blocking working mode of is taking advantage of the waiting time . When it's time to wait , These processes are idle and on standby , So a few processes solve a lot of concurrency problems .

Every time I come in request, There will be one. worker Process to deal with . But not the whole process , To what extent ? Deal with where there might be a blockage , Like upstream ( Back end ) Server forwarding request, And wait for the request to return . that , This one deals with worker Very clever , He will send the request after , Register an event :“ If upstream Back to , Tell me , I'll go on with it ”. So he went to rest .

here , If there is more request Come in , He can deal with it this way soon . And once the upstream server returns , Will trigger this event ,worker To take over , This request And then I'll go down .

Why? Nginx No multithreading ?

Apache: Create multiple processes or threads , And each process or thread allocates cpu And memory ( Threads are much smaller than processes , therefore worker Support than perfork High concurrency ), Concurrent conferences consume server resources .

Nginx: Using a single thread to process requests asynchronously and non blocking ( Administrators can configure Nginx The number of worker processes in the main process )(epoll), No... Is assigned to each request cpu And memory resources , It saves a lot of resources , It also reduces a lot of CPU Context switch of . That's why Nginx Support for higher concurrency .

Nginx What are the common optimized configurations ?

1) adjustment worker_processes

finger Nginx To generate the worker Number , The best practice is every CPU function 1 Working process .

Understand... In the system CPU The core number , Input

$ grep processor / proc / cpuinfo | wc -l
2) Maximize worker_connections

Nginx Web The number of clients that the server can serve at the same time . And worker_processes When used in combination , Get the maximum number of clients that can be served per second

Maximum number of clients / second = Working process * Number of worker connections

To maximize Nginx The full potential of , The worker connection should be set to the maximum number of processes allowed for the core to run at one time 1024.

3) Enable Gzip Compress

Compressed file size , Reduce the number of clients http The transmission bandwidth of , So it improves the page loading speed

Suggested gzip An example configuration is as follows :( stay http In part )

4) Enable caching for static files

Enable caching for static files , To reduce bandwidth and improve performance , You can add the following command , Limit the static file of the computer cache web page :

location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {

expires 365d;


keepalive Connections reduce the time required to open and close connections CPU And network overhead , For the variables that need to be adjusted to obtain the best performance, refer to :

6) Ban access_logs

Access logging , It records every nginx request , So it consumes a lot of CPU resources , So it reduces nginx performance .

Completely disable access logging

access_log off;

If you must have access logging , Enable access log buffering

access_log /var/log/nginx/access.log The main buffer = 16k
502 What are the possible reasons for the error report ?

  • 1)FastCGI Is the process started
  • 2)FastCGI worker Whether the number of processes is not enough
  • 3)FastCGI Execution time is too long
  • 4)FastCGI Buffer Not enough

nginx and apache equally , There is a front-end buffer limit , Buffer parameters can be adjusted

fastcgi_buffer_size 32k;
fastcgi_buffers 8 32k;
  • 5)Proxy Buffer Not enough

If you use Proxying, adjustment

proxy_buffer_size 16k;
proxy_buffers 4 16k;
  • 6)php Script takes too long to execute

take php-fpm.conf Of <value name="request_terminate_timeout">0s</value> Of 0s Change to a time


copyright notice
author[Migrant worker brother],Please bring the original link to reprint, thank you.

Random recommended