To enable caching, open nginx configuration file(/etc/nginx/nginx.conf). Add the below entry.
---------
proxy_cache_path /var/nginx_cache levels=1:2 keys_zone=nginx-cache:8m max_size=1000m inactive=100m;
proxy_temp_path /var/nginx_cache/tmp;
---------
Now, open the domain specific configuration fine and include shared memory zone for caching as below:
/etc/nginx/conf.d/techmesrv.conf
location / {
proxy_cache nginx-cache;
proxy_cache_valid 200 302 1000m;
proxy_cache_valid 400 1m;
Execute following command:
#nginx -t
#/etc/init.d/nginx reload
Once after this the cachng folders will be created automatically.
Parameters:
proxy_cache_path
proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time];
Sets the path and other parameters of a cache. Cache data are stored in files. The file name in a cache is a result of applying the MD5 function to the cache key. The levels parameter defines hierarchy levels of a cache. For example, in the following configuration
Syntax:-proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
file names in a cache will look like this:
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
proxy_temp_path
Syntax: proxy_temp_path path [level1 [level2 [level3]]];
Default:
proxy_temp_path proxy_temp;
Defines a directory for storing temporary files with data received from proxied servers. Up to three-level subdirectory hierarchy can be used underneath the specified directory. For example, in the following configuration
proxy_temp_path /spool/nginx/proxy_temp 1 2;
a temporary file might look like this: /spool/nginx/proxy_temp/7/45/00000123457
proxy_cache zone
Syntax: proxy_cache zone | off;
Default:
proxy_cache off;
Context: http, server, location
Defines a shared memory zone used for caching. The same zone can be used in several places. Parameter value can contain variables (1.7.9).
proxy_cache_valid
Sets caching time for different response codes. For example, the following directives
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
set 10 minutes of caching for responses with codes 200 and 302 and 1 minute for responses with code 404.
If only caching time is specified
proxy_cache_valid 5m;
then only 200, 301, and 302 responses are cached.
In addition, the any parameter can be specified to cache any responses:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
---------
proxy_cache_path /var/nginx_cache levels=1:2 keys_zone=nginx-cache:8m max_size=1000m inactive=100m;
proxy_temp_path /var/nginx_cache/tmp;
---------
Now, open the domain specific configuration fine and include shared memory zone for caching as below:
/etc/nginx/conf.d/techmesrv.conf
location / {
proxy_cache nginx-cache;
proxy_cache_valid 200 302 1000m;
proxy_cache_valid 400 1m;
Execute following command:
#nginx -t
#/etc/init.d/nginx reload
Once after this the cachng folders will be created automatically.
Parameters:
proxy_cache_path
proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time];
Sets the path and other parameters of a cache. Cache data are stored in files. The file name in a cache is a result of applying the MD5 function to the cache key. The levels parameter defines hierarchy levels of a cache. For example, in the following configuration
Syntax:-proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
file names in a cache will look like this:
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
proxy_temp_path
Syntax: proxy_temp_path path [level1 [level2 [level3]]];
Default:
proxy_temp_path proxy_temp;
Defines a directory for storing temporary files with data received from proxied servers. Up to three-level subdirectory hierarchy can be used underneath the specified directory. For example, in the following configuration
proxy_temp_path /spool/nginx/proxy_temp 1 2;
a temporary file might look like this: /spool/nginx/proxy_temp/7/45/00000123457
proxy_cache zone
Syntax: proxy_cache zone | off;
Default:
proxy_cache off;
Context: http, server, location
Defines a shared memory zone used for caching. The same zone can be used in several places. Parameter value can contain variables (1.7.9).
proxy_cache_valid
Sets caching time for different response codes. For example, the following directives
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
set 10 minutes of caching for responses with codes 200 and 302 and 1 minute for responses with code 404.
If only caching time is specified
proxy_cache_valid 5m;
then only 200, 301, and 302 responses are cached.
In addition, the any parameter can be specified to cache any responses:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;