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

Source: Internet
Author: User
Tags ack echo date nginx server
A URL to the Nginx server, the language is PHP, the middle of the process is how.

Reply content:

A URL to the Nginx server, the language is PHP, the middle of the process is how.

The communication between Nginx and PHP-FPM can use TCP network communication, or it is more lightweight but high concurrency without TCP stable unixsock (no need to go through the network), the following TCP traffic description.

Tcpdump/wireshark Analysis FIREFOX/NGINX/PHP-FPM complete 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 the 80 port of Nginx via Port 57939.
Nginx accesses the 9000 port of PHP-FPM via Port 58518.
Established indicates that the connection is keep-alive.

Monitor the LO device in the Wireshark or use the tcpdump to capture the packet analysis:
sudo tcpdump-s 0-i lo-w data.pcap port or 9000
The Wireshark can be filtered with the following filter analysis:
tcp.port==57939 or tcp.port==58518

57939 > http [SYN]
HTTP > 57939 [SYN, ACK]
57939 > http [ACK]
The top three packages represent the three handshakes of Firefox and Nginx.

get/app/buffer.php http/1.1
HTTP > 57939 [ACK]
These two packages represent Firefox's request for a URL to nginx.

58518 > Cslistener [SYN]
Cslistener > 58518 [SYN, ACK]
58518 > Cslistener [ACK]
The above three packages represent the three-time handshake between Nginx and PHP-FPM.

58518 > Cslistener [PSH, ACK]
Cslistener > 58518 [ACK]
The above two packages represent nginx to forward the request of Firefox to PHP-FPM.

Cslistener > 58518 [PSH, ACK] php-fpm push data to Nginx
58518 > Cslistener [ACK] Nginx received data transfer 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 into the loop.

Cslistener > 58518 [FIN, ACK] PHP-FPM Close response
58518 > Cslistener [FIN, ACK] nginx shutdown response
Cslistener > 58518 [ACK] PHP-FPM response
http/1.1 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();

Also note: Nginx gzip may perform output caching, which will result in the flush () function resulting in not being immediately sent to the client browser. Under NGINX+PHP-FPM, you should also pay attention to nginx fastcgi buffer, for example:
Fastcgi_buffer_size 128k;
Fastcgi_buffers 8 128k;
Indicates that Nginx will buffer the information of the PHP-FPM output, and when it reaches 128k, the buffer data will be sent to the client, then we need to make this buffer smaller first:
Fastcgi_buffer_size 4k;
Fastcgi_buffers 8 4k;
Also, gzip must be disabled:
gzip off;
Then, in PHP, before Ob_flush and flush, output a piece of content that reaches 4k, for example:
Echo str_repeat (', 1024*4);
In this case, PHP can normally output the required content by Ob_flush and flush.

  • 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.