Home Backend Development PHP Tutorial nginx analysis of keepalive and pipeline request processing

nginx analysis of keepalive and pipeline request processing

Aug 08, 2016 am 09:30 AM
keepalive nbsp pipeline request

Original article, please indicate when reprinting: Reprinted from pagefault

The link address of this article: nginx analysis of keepalive and pipeline request processing

This time we mainly look at the processing of keepalive and pipeline in nginx. There is no need to introduce the concepts here. . Let’s look directly at how nginx does it. First let’s look at the processing of keepalive. We know that keepalive is the default in http 1.1, unless the client explicitly specifies the connect header as close. The following is the code for nginx to determine whether keepalive is needed.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

void

ngx_http_handler(ngx_http_request_t * r)

{

…. ....................................

                                                                                                                                       headers_in.connection_type) {

case

0://If the version is greater than 1.0, the default is keepalive

r- >keepalive = (r->http_version > NGX_HTTP_VERSION_10);

//If the connection header is specified as close, keepalive is not required keepalive = 0; "

}................................

}


Then we know that keepalive means that the current connection will not be closed directly after the current http request is executed, so the related processing of nginx's keepalive is also the function to clean up the request. nginx’s function to clean up requst is ngx_http_finalize_request. In this function, ngx_http_finalize_connection is called to release the connection, and the relevant judgment of keepalive is in this function.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

staticvoid

ngx_http_finalize_connection(ngx_http_request_t *r)

{

ngx_http_core_loc_conf_t *clcf;

clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

. ..................... ................................................

//You can see that if keepalive is set and timeout is greater than 0, keepalive processing will be entered.

                                                                               r->keepalive   

&& clcf->keepalive_timeout > 0)

{

ngx_http_set_keepalive(r);

} else

if(r->lingering_close && clcf->lingering_timeout > 0) {

                                                                                  return;

}ngx_http_close_request(r, 0);

}

From the above we can see that keepalive is set through ngx_http_set_keepalive. Next we will look at this function in detail. In this function, pipeline requests will be processed incidentally, so let’s take a look at it together. First, how nginx distinguishes pipeline requests. It will assume that if the data read from the client contains more data, that is, parsing After completing the current request, there is still some data. At this time, it is considered a pipeline request. Another very important point is http_connection. We know from the previous blog that if we need to alloc a large header, it will be taken from hc->free first. If not, it will be created and then handed over to hc-> ;Busy to manage. And this buf will be reused here, because if the large buf is large, it needs to be re-allocated a second time. If the buf is reused here, one allocation will be reduced.

1314

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

2 7

28

29

30

31

32

33

34

35

36

37

38

39

40

41

hc = r->http_connection;

b = r->header_in;

//Generally, after parsing header_in, pos will be set to last. That is to say, the data read is just a complete http request. When pos is less than last, it means it may be a pipeline request.

if(b->pos < b->last) {

if

(b != c->buffer) {

                                                      * request processing then we do not use c-> buffer for

                                                                                                           ​​ * Now we would move the large header buffers to the free list.

*/

                                                                                                                                                                                                                                                                                    == NULL) {

//You can see the number of large_client_headers                                                                                         

                                                   cscf->large_client_header_buffers.num * sizeof

(ngx_buf_t *));                            

                                                                                                                      

                                                                                                                                                                                     ​ <p><code>                }

//Then clean up the busy of the current request

                                                                                                    1; i++) {

f = hc->busy[i];

                                          ​​​​​​​

f->pos = f->start;

                                                                                                         er_in buf for free use next time .                                                                                                                                            

}

}

Then the next part is the free request and setting the keepalive timer.

12

3

45

6

78

9

1011

12

15

r->keepalive = 0; if

ngx_http_free_request(r, 0);

c->data = hc;

//Set timer

ngx_add_timer(rev, clcf->keepalive_timeout);

//Then set readable Event

(ngx_handle_read_event(rev, 0) != NGX_OK) {

ngx_http_close_connection(c);

​​​​​​​​

return

;

​​​​​

}

wev = c->write;

wev->handler = ngx_http_empty_handler;

Then the next part is the processing of the pipeline.

12

3

456

7

89

10

1112

13

14 15

if(b->pos < b->last) {

>log

, 0,

"pipelined request"

);

#if (NGX_STAT_STUB)

                 ​_reading, 1);

#endif

//Set tag.

                                                                                                                                                                                                                                 

"reading client pipelined request line"

;

//Then throw it into the post queue and continue processing.

​​​​​ngx_post_event( rev, &ngx_posted_events);

When it reaches the bottom, it means that it is not a pipeline request, so we start to clean up the request and http_connection.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

2 7

28

29

30

                                                                                                    }

b = c->buffer;

if(ngx_pfree(c->pool, b->start) == NGX_OK) {

​​​​

* the special note for ngx_http_keepalive_handler() that

           * c->buffer's memory was freed

                                                                                                                                                                                                                                                       

} "

}

................................................ ................

if(hc->busy) {

for

(i = 0; i < hc->nbusy; i++) {                                                  hc ->busy[i] = NULL;

set keepalive handler.

12

3456

789

//This function will be analyzed in detail later

      rev->handler = ngx_http_keepalive_handler;

                                                                                                                                                                                                              0) != NGX_OK) {

                                                                                                                                                          

The last thing is the processing of tcp push. I won’t introduce it here for now. Next, I will have a special blog to introduce nginx’s operation of tcp push. Then let’s look at the ngx_http_keepalive_handler function. This function handles the keepalive connection. When there is a readable event on the connection again, this handler will be called. This handler is relatively simple, it just creates a new buf, and then restarts the execution of an http request (calling ngx_http_init_request).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

size = b->end - b ->start;

​​​​​​​if

(b->pos == NULL) {​​​​​

/*​​​​​​

* The c->buffer's memory was freed by ngx_http_set_keepalive(). .

                                                                                                                                                  

                                                                        ->pos == NULL) {

                                                                                                                                                                                       ->pos;

                                                          }

Then try to read the data, if there is no readable data, the handle will be added to the readable event again

123

4 567

89

n = c->recv(c, b->last, size);c->log_error = NGX_ERR OR_INFO ;

if(n == NGX_AGAIN) {

                         ​ ngx_http_close_connection(c);

}

return

;}

Finally, if the data is read, enter request processing.

1

ngx_http_init_request(rev);

Finally, let’s look at the ngx_http_init_request function. This time we mainly look at how nginx reuses the request when the pipeline is requested.
Pay attention to hc->busy[0] here. We know earlier that if it is a pipeline request, we will save the request header_in that has not been parsed before. This is because we may have read the second request of the pipeline request. Some heads.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

//Get the request, here we know that in the pipeline request , we will save the previous request.

r = hc->request;

if(r) {

//If it exists, we reuse the previous request .

                                                                            sizeof(ngx_http_request_t));

    

r->pipeline = hc->pipeline;

//If nbusy exists

                                                                                                          ;nbusy) {

//Save this header_in, and then parse it directly below. GR-& gt; header_in = hc-& gt; busy [0];

}}

}

else{

    r = ngx_pcalloc(c->pool,

sizeof

(ngx_http_request_t)); _connection(c);

                                               

                                                                                                 c->data = r;

From the above code, and then combined with my previous blog, we know that large header is mainly for pipeline, because in pipeline, if the previous request reads more of the next request header, the next time it is parsed, it may exceed the originally allocated client_header_buffer_size. At this time, we need to reallocate a header, which is a large header, so the httpconnection here is mainly for the pipeline situation, and the keepalive If the connection is not a pipeline request, in order to save memory, the previous request will be released. The above introduces nginx's analysis of keepalive and pipeline request processing, including aspects of the process. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

See all articles