Nginx caching application of

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42191317/article/details/100527763

Nginx cache Introduction

For a website contains a lot of content is, with the increase in traffic, for the contents of frequently accessed users, if every time we get to the back-end server, the server will bring enormous pressure to this end, the use of anti cache the contents of the frequency of access to the proxy server more conducive to saving back-end server resources. Nginx Web caching provides two ways, one is persistent cache, the other is the temporary cache.

Web cache server caching information content source located between the Web server and the client, when a user accesses the URL client, Web cache server will request the corresponding content source Web server, and in response to a memory or disk, and the next time when the request, if access is the same URL, Web cache server will direct the output cached content to the client, without accessing the content source Web server again.

Using the cache server can effectively reduce the load on the origin server and content database to improve user access speed.

Nginx configuration caching services

Persistent cache

proxy_store instructions Nginx can be used to provide content caching server response content source to a local file, if not manually delete the cache files remain in effect. Therefore, the method is applicable to permanent cache contents of the cache site in almost no change.

        location / {
             root cache;
             proxy_store on;
             proxy_store_access user:rw group:rw all:r;
             proxy_temp_path cache_tmp;
             proxy_pass http://59.110.171.154;
        }

The first line of the target server response result files are saved in the root directory cache nginx, and the second is to open a cache line, the third line is the access to the cache file, the fourth generation of the behavior of the cache file location, the fourth reverse behavior proxy.

After the access, cache directory as follows:

These two documents are the result just returned from the target server

root@iZ2ze488eeqkbsq8cxntxbZ logs]# tail -20f access.log 
94.191.112.250 - - [03/Sep/2019:21:13:16 +0800] "GET /WWW/phpMyAdmin/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:16 +0800] "GET /phpMyAdmln/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:17 +0800] "GET /program/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:17 +0800] "GET /shopdb/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:17 +0800] "GET /phppma/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:18 +0800] "GET /phpmy/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:18 +0800] "GET /mysql/admin/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:19 +0800] "GET /mysql/dbadmin/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:19 +0800] "GET /mysql/sqlmanager/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:20 +0800] "GET /mysql/mysqlmanager/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:20 +0800] "GET /wp-content/plugins/portable-phpmyadmin/wp-pma-mod/index.php HTTP/1.0" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:28.0) Gecko/20100101 Firefox/28.0"
94.191.112.250 - - [03/Sep/2019:21:13:20 +0800] "GET /manager/html HTTP/1.0" 404 555 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
94.191.112.250 - - [03/Sep/2019:21:17:21 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
94.191.112.250 - - [03/Sep/2019:21:22:24 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
124.89.2.70 - - [03/Sep/2019:21:24:50 +0800] "GET / HTTP/1.1" 200 628 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
124.89.2.70 - - [03/Sep/2019:21:24:57 +0800] "GET /demo.html HTTP/1.1" 200 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
124.89.2.70 - - [03/Sep/2019:21:25:02 +0800] "GET /demo2.html HTTP/1.1" 200 22 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
94.191.112.250 - - [03/Sep/2019:21:25:12 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
94.191.112.250 - - [03/Sep/2019:21:25:18 +0800] "GET /demo.html HTTP/1.0" 200 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
94.191.112.250 - - [03/Sep/2019:21:25:24 +0800] "GET /demo2.html HTTP/1.0" 200 22 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
94.191.112.250 - - [03/Sep/2019:21:29:17 +0800] "GET /demo2.html HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"

Check the destination server access logs, or discovery will forward the request, and did not use the local cache

The reason is simple, just open the above configuration cache, the cached response, but still meet the request will be forwarded, want to use the local cache, also you need to add the following configuration

        location / {
             root cache;
             proxy_store on;
             proxy_store_access user:rw group:rw all:r;
             proxy_temp_path cache_tmp;
             if (!-e $request_filename){
                proxy_pass http://59.110.171.154;
                }
        }  

The above expression! -e means to check whether a file exists, when there is, it will forward the request.

The successful use of the cache, the target server has not received the request.

PS: Nginx caching is based on permanent file storage, and sometimes use Redis cache server compared to the speed may also be slower.

Temporary Cache

There Nginx server configuration using proxy_cache temporary cache instruction set, which uses the algorithm md5 connection request after the Hash, the specific configuration according to generate the cache file directory, in response to stored data.

   proxy_temp_path /root/local/nginx/proxy_temp_dir;
   proxy_cache_path /root/local/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1m max_size=500m;

Configuring the target server returns the file to a temporary directory and Web cache directory and parameter settings

levels: represents the hierarchy in the cache directory, request URL address after Hash created.

Cache_one name and size of the designated buffer area 50M: keys_zone

inactive: active empty the cache have not been accessed within a specified time 1m on behalf of one minute

max_size: Specifies the maximum cache size

Then add a temporary cache coherency block disposed Server

        add_header X-Via $server_addr;
        add_header X-Cache $upstream_cache_status;
        location / {
                proxy_cache cache_one;
                proxy_cache_key $host$uri$is_args$args;
                proxy_cache_valid 200 10m;
                proxy_cache_valid 304 1m;
                proxy_cache_valid any 1m;
                proxy_pass http://59.110.171.154;
         }

The first line buffer is provided for the name, set the key rules second row, behind the lines set the cache time different status codes.

At this point the temporary cache configuration is complete.

 

 

Guess you like

Origin blog.csdn.net/qq_42191317/article/details/100527763