Thoughts on modifying cahce key generation rules in nginx

Source: Internet
Author: User
: This article describes how to modify the rules for generating cahce keys in nginx. For more information about PHP tutorials, see. Aside from the logical deviation from the previous steps, modifying this key is not huge in terms of code volume. The cache contains proxy_cache and memcached in nginx, that is, file cache and memory cache. The file cache copies the Accessed page to the cache file. The next access to the file will be directly read from the cache file based on the key if hit, without the need to access the upstream server. As the name implies, the memory cache caches files to the memory.

Because memcached cannot be broken for a long time and cannot be debugged, you should first use proxy_cahce to modify the key generation rules. What is interesting in this process is that after the key is treated as a string, then this operation becomes a string operation, and there is no insurmountable obstacle to the string operation. However, it is important to note that the cache_key generation rules are in nginx. conf can be configured, so the flexibility is high. as required, you need to clear several fields in the key. these fields must also be written to the configuration file, therefore, the flexibility of del_args parameters is also very high. After this analysis, the hard-written and dead-string operations will adversely affect subsequent operations. Therefore, this rule is further analyzed.

Abstract this problem and extract the model to be solved from closely related details in nginx. this is essentially a string processing problem.

String A. the string format is similar to the URL format. the most complete format is $ scheme $ domain $ uri $ is_args $ args. in this way, the string is sorted by the longest watermarked list, the subsequent configuration table will only reduce parameters than the longest key.

Character B: The key to be deleted from the key. it is the key of the parameter section in the URL, such as name, age, and birth. it is used to clear 127.0.0.1/index.html? Name = ** & age = ** indicates the character that exists in it. split the character and combine it again.

Why is it so troublesome to split A and try again? Because the cache_key to be read, including the ngx_http_request_t, whether it is to read the configured cache_key or the url that exists in the struct itself, the args parameter is defined as the ngx_string_t type, the data in this type, it is of the u_char * type, and the strings in it are stored in global constants and read-only. Therefore, you must re-create a new string for storage during modification, after the function is assigned a new Key after the operation, the content cannot disappear, so it is set to static to make the static volume, and the storage location is changed to the global data zone. However, this operation also has a hidden danger. after it is set to static, this value will only have this one. after this modification, it will be assigned to the Key. if it is used properly immediately, otherwise, once another process or the value is modified, the content in it will be replaced. however, to prevent the lock, the lock overhead will be taken into account, in combination, it seems that the amount of static data is not as good as setting a variable directly at the upper layer of the call. the function that calls the key generation function is in this scope. after processing, the variable is processed in the stack.

The other one is about the effect display. it is amazing that you need to directly check whether the cache is hit on the front-end interface. the reason why this is required is that on the one hand, I guess no one wants to change themselves, in particular, it is just a habit to change your habits, whether it is wonderful or beneficial to you or improve your technical accomplishments. on the other hand, if you look at the good, it should be necessary to establish a monitoring system to monitor the server status directly at the front end. if this is the reason, the development volume will increase dramatically, and the development time will need to continue to be added.

In general, the module of Nginx design is still clear, and there is something wrong with it. I will go into details in the next day, as it is like crawling a huge shit, A mountain that is about to collapse.

The above describes how to modify the generation rules of the cahce key in nginx, including some content, and hope to help those who are interested in the PHP Tutorial.

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.