Nginx cache cache 5 options

  
                  

It seems that I have never written the caching function of nginx. I just can't share it. This is a bad habit.

1. One of the traditional caches (404)

This method is to direct the nginx 404 error to the backend, and then use the proxy_store to save the page returned by the backend.

Configuration:

location /{ root /home/html/;#home directory expires 1d;#page expiration time error_page 404 =200 /fetch$request_uri;#404directed to /fetch Directory }

location /fetch/{#404Directed here internal;# indicates that this directory cannot be directly accessed externally to expires 1d;#page expiration time alias /home/html/;#virtual directory file The system address should be the same as locaion /, proxy_store will save the file to this directory proxy_pass http://www.45it.com/; # backend upstream address, /fetch is also a proxy proxy_set_header Accept-Encoding '';# let The backend does not return the contents of the compression (gzip or deflate). Saving the compressed content will cause confusion. Proxy_store on; #Specify nginx to save the file returned by the proxy proxy_temp_path /home/tmp; #temp directory, this directory should be in the same hard disk partition as /home/html }

Is nginx to have permission to write files to /home/tmp and /home/html. In Linux, nginx will be configured as nobody user to run, so these two directories will be chown nobody, set to nobody user-specific Of course, you can also use chmod 777, but all experienced system administrators will not recommend using 777.

2, the traditional cache of two (!-e)

The principle and 404 jump are basically the same, but more concise:

location /{ root /home/html /; proxy_store on; proxy_set_header Accept-Encoding ''; proxy_temp_path /home/tmp; if ( !-f $request_filename ) { proxy_pass http://www.45it.com/; } }

This configuration saves a lot of code than 404. It uses !-f to judge whether the requested file does not exist on the file system. If it does not exist, proxy_pass is sent to the backend, and the return is also saved by proxy_store.

Both traditional caches have basically the same advantages and disadvantages: Disadvantage 1: Dynamic links with parameters are not supported, such as read.php?id=1, because nginx only saves file names, so this link only Save it as read.php under the file system, so that when the user accesses read.php?id=2, it will return incorrect results. At the same time, it does not support http://www.45it.com/this form of home page and secondary directory http://www.45it.com/sos/, because nginx is very honest, will write such a request link to the file System, and this link is obviously a directory, so the save fails. These situations require a write of rewrite to save properly. Disadvantage 2: There is no mechanism for cache expiration and cleanup inside nginx. These cached files will be permanently saved on the machine. If there are so many things to cache, it will smash the entire hard disk space. For this purpose, you can use a shell script to clean up regularly, and you can write dynamic programs such as php to do real-time updates. Disadvantage 3: Only 200 status codes can be cached, so the backend returns status codes such as 301/302/404 will not be cached. If there is a pseudo-static link with a large amount of access deleted, it will continue to penetrate. The backend carries a lot of pressure. Disadvantage 4: nginx does not automatically select memory or hard disk as the storage medium, everything is determined by the configuration, of course, there will be an operating system-level file caching mechanism in the current operating system, so there is no need to worry too much about concurrent reading on the hard disk. The resulting io performance problem.

The disadvantage of nginx traditional cache is that it has different characteristics from cache software such as Squid, so it can also be regarded as its advantage. In production applications, it is often used as a partner with Squid. Squid's links to the link are often unstoppable, and nginx can block its access, for example: http://45it.com/? and http://45it.com /Will be treated as two links on Squid, so it will cause two penetrations; and nginx will only be saved once, regardless of whether the link becomes http://45it.com/?1 or http://45it.com/? 123, can not pass the nginx cache, which effectively protects the backend host.

nginx will very honestly save the link form to the file system, so that for a link, you can easily check its cache status and content on the cache machine, and it can be easily combined with other files. Managers such as rsync, etc., are completely a file system structure.

These two traditional caches can save files to /dev/shm under linux. Generally, I do the same, so I can use the system memory to do the cache. If I use the memory, I can clean up the expired content. It will be much faster. When using /dev/shm/, in addition to pointing the tmp directory to the /dev/shm partition, if there are a large number of small files and directories, modify the inode number and maximum capacity of the memory partition:

mount -o size=2500M -o nr_inodes=480000 -o noatime,nodiratime -o remount /dev/shm

The above command is used on a machine with 3G memory because /dev/shm defaults The maximum memory is half of the system memory is 1500M, this command will increase it to 2500M, and the number of inode system inodes may not be enough by default, but the interesting thing is that it can be adjusted at will, here the adjustment is 480000 conservative point, But it is basically enough.

3, based on memcached cache

nginx support memcached, but the function is not particularly strong, performance is still very good.

location /mem/{ if ( $uri ~ "^/mem/([0-9A-Za-z_]*)$" ) { set $memcached_key "$1"; memcached_pass 192.168 .1.2:11211; } expires 70; }

This configuration will point http://45it.com/mem/abc to the abc key of memcached to fetch data.

nginx does not currently have any mechanism for writing memcached, so to write data to memcached is done in the background dynamic language, you can use 404 to direct to the back end to write data.

4, based on third-party plug-ins ncache

ncache is a good project developed by Sina Brothers, it uses nginx and memcached to implement some of the functions similar to squid cache, I did not use this plug-in Experience, can refer to:

http://code.google.com/p/ncache/

5, nginx newly developed proxy_cache function

from nginx-0.7 At the beginning of the .44 version, nginx supports a more regular cache function similar to Squid. Currently, it is still in the development stage. The support is quite limited. This cache is to save the link with md5 encoding hash, so it can support any link, and also supports 404/. 301/302 is a non-200 state.

Configuration:

First configure a cache space:

proxy_cache_path /path/to/cache levels=1:2 keys_zone=NAME:10m inactive=5m max_size=2m clean_time =1m;

Note that this configuration is outside the server tag. Levels specify that the cache space has two layers of hash directories. The first directory is 1 letter, the second layer is 2 letters, and the saved file name. It will be similar to /path/to/cache/c/29/b7f54b2df7773722d382f4809d65029c; keys_zone will give the space a name, 10m means the space size is 10MB; inactive 5m means the cache default time is 5 minutes; max_size 2m means that the single file exceeds 2m It is not cached; clean_time specifies a cache to clean up once.

location /{ proxy_pass http://www.45it.com/;

proxy_cache NAME;#Use NAME this keys_zone

proxy_cache_valid 200 302 1h;#200 and 302 The status code is saved for 1 hour. proxy_cache_valid 301 1d; #301 status code saves one day proxy_cache_valid any 1m; #other saves for one minute }

ps: supports stable versions of 0.7.44 to 0.7.51 of cache There are problems with sex. There are errors in accessing some links, so these versions are best not to be used in a production environment. The more stable version currently known under nginx-0.7 is 0.7.39. The stable version 0.6.36 is also a recent update. If you do not use some of the new new features in 0.7 in the configuration, you can also use version 0.6.36.

Copyright © Windows knowledge All Rights Reserved