Pay attention to some details when compiling the HTTP module of nginx

Source: Internet
Author: User

1. When applying for a buffer, pay attention to where the buffer is used and where to apply for it.

In the handler function, for example, generally, the application buffer is applied from the R-> pool of ngx_http_request_t * r. However, if you need to change the buffer of R-> connection-> addr_text.data to apply for a larger memory, you may encounter problems when applying from R-> pool, at the beginning of the project, I didn't pay attention to it. Then I applied for memory from R-> pool. However, access. the remote_ip is not an IP string, but also affects some code operations.

For example, u_char * addr_buf = ngx_pnalloc (R-> connection-> pool, R-> connection-> addr_text.len + 1 + sizeof (long); ngx_memcpy (addr_buf, r-> connection-> addr_text.data, R-> connection-> addr_text.len); addr_buf [R-> connection-> addr_text.len] = 0 ;... 2. this is what I encountered. It may not happen if I put it in another environment. If two out messages are sent when ngx_http_output_filter (R, & out) is used, ngx_chain_t out1, out2; ngx_buf_t * buf1, * buf2; buf1 = ngx_pnalloc (..) buf1-> Pos = ngx_pnalloc (..); buf2 = ngx_pnalloc (..) buf2-> Pos = ngx_pnalloc (..); out1-> Buf = buf1; out2-> Buf = buf2; out2-> next = NULL; out1-> next = & out2; ngx_http_output_filter (R, & out1 ); this method checks whether the content is in the file and Di is used in the function ngx_output_chain_as_is of the copy filter phase when two content copies are sent. This rectio method may cause a segment error, because out-> file = NULL. Later, we made two out into one and put the data in a Buf-> pos. During the test, I still occasionally send a segment error in the above-mentioned area. Therefore, considering that my data is in the buffer, instead of setting Buf-> in_file = 0 in file; then everything is OK. 3. Self-implemented third-party modules generally aim to process requests and generate content. In fact, the essence is to write handler and submit the requests to the self-implemented processing thread. 1) It should be noted that a function is specified in the postconfiguration stage of the module context, and the function specifies the stage in which the handler is pressed, the latest project uses the nginx 1.0.13 version. in the initial stage of this project, we are in the pressure content stage. However, during handler processing, we need to specify delay, which cannot be entered, later, it was changed to the preaccess stage. I have mentioned the reason why content cannot be specified in another article. 2) Specify the delay function, which must be processed by a defined thread and may not be returned immediately. Therefore, we need to use the timer to set the timeout time, check whether the access is handled after a period of time, so that the subsequent request processing can be affected without blocking. // Set the timeout function r-> write_event_handler = ngx_http_recommend_service_delay; // set the timer time ngx_add_timer (R-> connection-> write, (ngx_msec_t) delay_time); 4. there is no doubt about the loading of processing threads. It is to initialize our own processing threads in the process initialization phase. Why do we need this thread? For example, in a layered architecture, the front end is the web server that accepts requests, and the back end is the distributed redis group. So here, you can use your own processing thread to accept the requests received by the Web server. After simple processing, you can submit them to the processing thread to go to The redis server group to retrieve the actual data. This process may not be completed immediately. Therefore, as mentioned above, you need to set a timer. Finally, this article is separated for a period of time and may not be quite smooth. When similar projects are to be completed in the future, sort out all the details you need to pay attention.

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.