Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of loading performance of Nginx Startup configuration

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "sample analysis of the loading performance of Nginx startup configuration", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample analysis of loading performance of Nginx startup configuration".

I. Test content

The script creates three types of configuration files with the following rules:

1. Create 20, 000 Server {} configurations

2. Server_name basic fixed length, PerformanceTestxxx

3. The first category is that the IP of listen is exactly the same, the port is completely different, and the configuration file example:

Http {server {listen 192.168.0.1 listen 8080; server_name PerformanceTest8080;}... Server {listen 192.168.0.1 server_name PerformanceTest8081;... . }}

3. The second category is that the Port of listen is exactly the same, the IP is completely different, and the configuration file example:

Http {server {listen 192.168.0.1 listen 80; server_name PerformanceTest1;}... Server {listen 192.168.0.2 server_name PerformanceTest2;... . }}

4. The third category is that the IP:PORT of listen is exactly the same, the server_name is completely different, and the configuration file example:

Http {server {listen 192.168.0.1 listen 80; server_name PerformanceTest001;}... Server {listen 192.168.0.1 virtual 80; server_name PerformanceTest002;... . }}

Second, test data

For three types of configurations, add three more sets of variables:

1. Do not configure server_name

2. Full configuration of server_name is the same

3. The full configuration of server_name is different.

Look at the effect of server_name on initialization speed

The performance of the nine combinations is as follows:

Nginx startup time: time. / nginx-c / root/nginx.conf.sameport.noloc

Nginx reload time: time. / nginx-c / root/nginx.conf.sameport.noloc-s reload

From the test data, it can be seen that the factor affecting the start-up speed of nginx is the port of listen in server {}, and the server_name instruction basically has no effect.

Third, cause analysis

3.1 brief introduction of http {} initialization process:

Parsing the configuration file is accomplished by recursively calling the ngx_conf_parse function, and the parsing process of the http {} configuration block:

1. Parse to the http instruction, execute the ngx_http_block function, create the configuration context of http module, and then continue ngx_conf_parse to parse the internal contents of http {}

two。 Parse to the server instruction, execute the ngx_http_core_server function, create the configuration context of the server {}, and then continue ngx_conf_parse to parse the contents inside the server {}

3. Parse to the listen instruction and add it to the cscf and cmcf configuration, as shown below:

1. The cmcf- > servers array holds the server data of all the monitors.

2. The cmcf- > ports array holds the data of all the ip aggregated by port. Listen the same ip:port, srv_conf with different server_name will be hung under the servers array of ngx_http_conf_addr_t.

A configured look might look like this:

3.2 time-consuming location

It takes the total time to record the function by adding variables in the code, and it takes two steps to get the startup time:

1) parse the configuration file, corresponding to the ngx_conf_parse function

2) initialize socket, corresponding to ngx_open_listening_sockets function

3.3 ngx_conf_parse time-consuming analysis

It is mainly the time consuming of the ngx_http_block function, which is divided into the following two parts:

Obviously, the performance of the ngx_http_add_addresses function is consumed by finding the existing ip of the same port, which is a linear traversal lookup.

And string comparisons are required. Ngx_memcmp has been executed 2w*2w 400 million times:

For (I = 0; I)

< port->

Addrs.nelts; iTunes +) {/ / traversal lookup. If there are too many IP of the same port in the configuration file, string comparison brings big performance problems, 2w listen, this block takes about 8s if (ngx_memcmp (p, addr [I] .opt.u.sockaddr _ data + off, len)! = 0) {continue } / * the address is already in the address list * / / find the corresponding ip and add cscf to IP if (ngx_http_add_server (cf, cscf, & addr [I])! = NGX_OK) {return NGX_ERROR;}

3.4 ngx_open_listening_sockets time-consuming analysis

Initialize the socket of all listening in the

/ * for each listening socket * / ls = cycle- > listening.elts; for (I = 0; I

< cycle->

Listening.nelts; iTunes +) {if (bind (s, LS [I] .sockaddr, LS [I] .socklen) =-1) {... ... If (listen (s, LS [I] .backlog) =-1) {}

For the same IP, it takes less time to create a new socket with different port than to create multiple socket with different IP.

These are all the contents of the article "sample Analysis of the loading performance of Nginx Startup configuration". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report