Method 1
The code is as follows: |
Copy code |
Server { #... N rows of configuration are omitted here # Example:/thumb/export md52.16.png_300x187.png Location ~ "/Thumb/([0-9a-f] {32 }. (png | jpg | gif) _ (d + |-) x (d + | -). (png | jpg | gif) $ "{ Access_log off; Set $ image $1; Set $ width $3; Set $ height $4; # We recommend that you use try_files instead of if Try_files/thumb/$ image = 404; Image_filter resize $ width $ height; Image_filter_buffer 10 M; # It will only be cached on the browser side. If you want to cache on the server side, you can use the proxy solution (this is not provided here, you can google it yourself) Expires 7d; } } |
Nginx also has a kind of cache proxy_cache, which is more advanced than proxy_store in design. It adopts the memory + hard disk cache mode and can set the cache size and cache expiration.
The code is as follows: |
Copy code |
Http { ,,,,, Proxy_cache_path/var/cache/nginx/cache levels = keys_zone = imgcache: 100 m inactive = 1d max_size = 10g; Server { ........Www.111cn.net Location ~ * ^. +. (Js | ico | gif | jpg | jpeg | png | html | htm) $ { Log_not_found off; Access_log off; Expires 7d; Proxy_pass http://www.111cn.net; Proxy_cache imgcache; Proxy_cache_valid 200 302 1d; Proxy_cache_valid 404 10 m; Proxy_cache_valid any 1 h; Proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; } } } |
The above code uses a shared memory of up to MB for the file index on the hard disk, including the file name and the number of requests. If each file is inactive within one day (no request) it is eliminated from the hard disk. The hard disk cache is up to 10 GB. When it is full, the cache is automatically cleared based on the LRU algorithm.