What happened in the middle of a browser to the Nginx server?

Source: Internet
Author: User
Tags echo date nginx server
A url is sent to the nginx server, and the language is PHP. what is the process in the middle. A url is sent to the nginx server, and the language is PHP. what is the process in the middle.

Reply content:

A url is sent to the nginx server, and the language is PHP. what is the process in the middle.

The communication between Nginx and the PHP-FPM can adopt TCP network communication, or the UnixSock which is more lightweight but high concurrency does not have TCP stable (does not need to go through the network), The following describes the TCP communication.

Tcpdump/wireshark analysis firefox/nginx/php-fpm completes a PHP request

Sudo lsof-I-n-P | egrep ": 80 |:9000" | grep ESTABLISHED

firefox   2510  eechen   69u  IPv4 288893      0t0  TCP 127.0.0.1:57939->127.0.0.1:80 (ESTABLISHED)nginx     6843     png    3u  IPv4 286299      0t0  TCP 127.0.0.1:80->127.0.0.1:57939 (ESTABLISHED)nginx     6843     png   17u  IPv4 286300      0t0  TCP 127.0.0.1:58518->127.0.0.1:9000 (ESTABLISHED)php-fpm   6865     png    3u  IPv4 288877      0t0  TCP 127.0.0.1:9000->127.0.0.1:58518 (ESTABLISHED)

Firefox accesses port 80 of nginx through Port 57939.
Nginx accesses Port 58518 of php-fpm through Port 9000.
ESTABLISHED indicates that the connection is keep-alive.

Monitor the lo device in WireShark or use tcpdump for packet capture analysis:
Sudo tcpdump-s 0-I lo-w data. pcap port 80/9000
The following filters can be used in WireShark for Filter analysis:
Tcp. port = 57939 or tcp. port = 58518

57939> http [SYN]
Http & gt; 57939 [SYN, ACK]
57939> http [ACK]
The three packages above represent three handshakes between firefox and nginx.

GET/app/buffer. php HTTP/1.1
Http> 57939 [ACK]
The two packages indicate that firefox requests a URL from nginx.

58518> cslistener [SYN]
Cslistener> 58518 [SYN, ACK]
58518> cslistener [ACK]
The above three packages represent three handshakes between nginx and php-fpm.

58518> cslistener [PSH, ACK]
Cslistener> 58518 [ACK]
The above two packages represent nginx forwarding firefox requests to php-fpm.

Cslistener> 58518 [PSH, ACK] php-fpm push data to nginx
58518> cslistener [ACK] nginx receives data transmitted to firefox
[TCP segment of a reassembled PDU]
57939> http [ACK] firefox receives data
Cslistener> 58518 [PSH, ACK] php-fpm continues to push data to nginx and enters the loop.

Cslistener> 58518 [FIN, ACK] php-fpm close response
58518> cslistener [FIN, ACK] nginx close response
Cslistener> 58518 [ACK] php-fpm response
HTTP/1.1 200 OK (text/html)
57939> http [ACK] firefox response

Buffer. php test code:


  0;$i--){    echo date('H:i:s').'
'; echo str_repeat(' ', 1024*4); ob_flush(); flush(); sleep(1);}echo 'Stop.';ob_end_flush();

Note: gzip of Nginx may output the cache, which will cause the results generated by the flush () function not to be immediately sent to the client browser. pay attention to fastcgi buffer of Nginx under Nginx + PHP-FPM, for example:
Fastcgi_buffer_size 128 k;
Fastcgi_buffers 8 128 k;
It indicates that Nginx will buffer the information output by the PHP-FPM. when it reaches K, it will send the buffer data to the client, then we need to reduce the buffer first:
Fastcgi_buffer_size 4 k;
Fastcgi_buffers 8 4 k;
In addition, gzip must be disabled:
Gzip off;
Then, in php, before ob_flush and flush, output a piece of content that reaches 4 K. for example:
Echo str_repeat ('', 1024*4 );
By now, PHP can output the required content through ob_flush and flush row by row.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.