Set of interfaces for receiving and sending nginx data

Source: Internet
Author: User
Tags sendfile
Set the nginx data receiving and sending interfaces. 1. Data receiving: // readv distributes the data read to the buffer zone in the same order as above. Readv always fills up a buffer before entering the next one. Readv returns the total number of bytes read. If the end of the file is reached, no more...
Set the nginx data receiving and sending interfaces. 1. Data receiving: // readv distributes the data read to the buffer zone in the same order as above. Readv always fills up a buffer before entering the next one. Readv returns the total number of bytes read. If no data is readable at the end of the file, 0 is returned.
N = readv (c-> fd, (struct iovec *) vec. elts, vec. nelts); put iov = ngx_array_push (& vec) in the list; 2. process a large amount of data sent: read data if (iov = NULL) {return NGX_CHAIN_ERROR ;} set the set interface // TCP_NODELAY option to disable the Nagle algorithm. // The Nagle algorithm saves unconfirmed data into the buffer until a packet is sent together to reduce the number of small packets sent by the host. // but for some applications, this algorithm will reduce system performance. Therefore, TCP_NODELAY can be used to disable this algorithm. // The TCP_NODELAY option is set only when the application writer has a definite understanding of its effect and is required, because it has a significant negative impact on network performance.
// TCP_NODELAY is the only option to use the IPPROTO_TCP layer. All other options use the SOL_SOCKET layer. If (setsockopt (c-> fd, IPPROTO_TCP, TCP_NODELAY, (const void *) & tcp_nodelay, sizeof (int) =-1) {err = ngx_errno; sending data // sendfile () is an operation function that acts on copying data between two file descriptors. this copy operation is operated in the kernel, so it is called "zero copy ". the sendfile function is much more efficient than the read and write functions, because the read and write functions need to copy data to the user's application layer. // parameter description: // out_fd is the file descriptor used for write operations. // in_fd is the file descriptor used for read operations) file descriptor;
// Offset. it indicates the offset from which the sendfile function reads data from in_fd. if it is zero, it indicates reading from the beginning of the file, otherwise it will be read from the corresponding cheap amount. for cyclic reading, the next offset value should be the value of this offset added to the return value of the sendfile function. // count is the number of bytes copied between two descriptors (bytes) // sendfile () is advantageous in both cases, but I have no environment test: /// 1. file server or HTTP server with large concurrency; // 2. embedded systems with insufficient memory resources; // 3. used with setsockopt // why do I use sendfile? // The reason is very simple. one requirement in the project is that the backend program is responsible for packaging and encrypting the source file to generate the target file, and then the program reads the target file and returns it to the browser; this method has a fatal defect: it occupies a large amount of backend program resources. if some visitors download slowly, a large amount of resources will be occupied for a long time and cannot be released, soon, the backend program will be unable to provide services because there is no available resources. Nginx reports a 502 error! Second, in nginx, I want to "check whether the target file exists by nginx. if so, I will directly return it to the browser without the need to process the backend Program ", in this way, the backend program is only responsible for generating the target file. when a single target file is generated, the service is basically no longer provided, while nginx provides the full static file browsing service. It can be imagined that the performance improvement is still much greater! //// How do I enable sendfile? // The detailed configuration steps will not be mentioned, and the official wiki has already explained clearly. Note: // 1. location must be defined as internal; // 2. if alias is used in location, pay attention to the "/" at the end of the Directory.
// 3. when matching location, use only the directory name. I encountered a crazy problem during the test. // // first let's talk about my final solution: // 1. add a location as the check for the target file. If yes, it will be sent to the location of internal for further processing, if it does not exist, rewrite it to the back-end program for processing. // download and so on may be used // Mainly improves server resource performance rc = sendfile (c-> fd, file-> fd, & offset, file_size); // Big data transmission is called the zero copy technology, so it is often used as a high-performance file server. 3. process sending a small amount of data: read data if (iov = NULL) {return NGX_CHAIN_ERROR ;} send data // it should be small data transmission // store multiple data together and write the data residing in two or more non-connection buffers at a time. // UNIX and WINSOCK provide different implementation methods. with writev, you can specify a series of buffers to collect data to be written, so that data can be stored in multiple buffers and written at the same time, so as to avoid the mutual influence between Nagle and the latency ACK algorithm. Rc = writev (c-> fd, header. elts, header. nelts );
Related Article

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