Nginx itself has a cache function, can cache static objects, than slices,CSS,JS and other content directly cached to local, the next time access to the same object, directly from the cache, no need to access the backend static server and storage storage server, can replace squid function.
1 Environment Preparation
We're only testing nginx here. Proxy_cache nginx then we can use epel source, Yum install one:
#添加epel源 [email protected]~>> wget -o /etc/yum.repos.d/epel.repohttp:// Mirrors.aliyun.com/repo/epel-6.repo#yum installation Nginx[email protected]~>> yum install nginx  -Y#RPM -QL View the primary profile location [Email protected]~>> rpm -ql nginx here for simplicity, Use only a simple nginx.conf configuration file:[email protected]>> cat nginx.confuser nginx;worker_processes 1;error_log /var/ log/nginx/error.log;pid /var/run/nginx.pid;events { worker_connections 1024;} http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ' $ remote_addr - $remote _user [$time _local] "$request" ' & NBsp; ' $status $ Body_bytes_sent "$http _referer" "$http _user_agent" "$http _x_forwarded_for" '; sendfile on; keepalive_timeout 65; server { listen 80; location / { root /usr/share/nginx/html; index index.html index.htm; } }}
Start to see if the initial interface is normal:
[Email protected]>> nginx[email protected]>> netstat-tupln|grepnginxtcp 0 0 0.0.0.0:80 0 .0.0.0:* LISTEN 1043/nginx[email protected]>> curl-i 192.168.16.199http/1.1 Inx/1.0.15date:mon, Sep 09:40:53 Gmtcontent-type:text/htmlcontent-length:3698last-modified:tue, June 2015 21 : 34:15gmtconnection:keep-aliveaccept-ranges:bytes
Everything is OK, the home page has 2 pictures, just for the experiment:
[Email protected]>> tree/usr/share/nginx/html//usr/share/nginx/html/|--404.html|--50x.html|--index.html|-- Nginx-logo.png '--poweredby.png
The environment is ready to complete.
2 Configurationcache2.1 Create a directory and Mount itTmpfs
Nginx proxy_cache are memory-and disk-based caches that require a cache directory and a temp directory to be specified:
[Email protected]>> mkdir/tmp/{ngx_tmp,ngx_cache}-p
cache is stored on disk, disk IO affects the speed of the cache, so we are loading the TMPFS in the Ngx_cache directory to speed up the read and write of the cache:
[Email protected]>> mount-t tmpfs-osize=100m tmpfs/tmp/ngx_cache[email protected]>> mount|grep TMPFSTMPFS ON/DEV/SHM type TMPFS (rw) tmpfs on/tmp/ngx_cache type TMPFS (rw,size=100m)
2.2 Configure the cache directory size andKeySpace name
put the following configuration into the HTTP tag:
[Email protected]>> grep proxy_cache_pathnginx.conf
Proxy_cache_path/tmp/ngx_cache levels=1:2 keys_zone=cache_one:100minactive=1d max_size=5g;
# Specify cache directory, cache level, key space name, key space size, expiration time, and maximum disk cache size
2.3 Configuring the Reverse proxy
first Configure the upstream node pool:
Upstream Server_pool {server 127.0.0.1:8080;}
Configure the agent in the location segment of the Server tab :
Proxy_pass Http://server_pool;
To configure the label for the 8080 port:
server {listen 8080; Location/{root/usr/share/nginx/html; Index index.html index.htm; } Access_log/var/log/nginx/access.log main;}
Configure Proxy_cache related parameters to enable caching:
Proxy_pass http://server_pool;proxy_next_upstream http_502 http_504error timeout invalid_header; #出错尝试下一个节点proxy_cache Cache_one; #缓存键空间名proxy_cache_valid 304 12h; #指定对应状态码的缓存时间proxy_cache_valid 301 302 1m;proxy_cache_valid any 1m;proxy_cache_key $host $uri$is_args$args; #指定键key的格式proxy_set_header Host $host; #传递主机名给后端节点proxy_set_header x-forwarded-for$remote_addr; #传递客户端IP给后端节点expires 1d; #超期时间
the final nginx.conf configuration file is as follows:
[email protected]>> cat nginx.confuser nginx;worker_processes 1;error_log /var/log/nginx/ error.log;pid /var/run/nginx.pid;events { worker_connections 1024;} http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ' $ remote_addr - $remote _user [$time _local] "$request" " ' $status $body _bytes_sent "$http _referer" "$http _user_agent" "$http _x_forwarded_for" ' "Addr: $upstream _addr-status: $upstream _status-cachestatus: $upstream _cache_status" '; sendfile on; keepalive_ timeout 65; proxy_cache_path /tmp/ngx_cache levels=1:2 keys_zone=cache_one:100m inactive=1dmax_size=5g; upstream server_pool { server 127.0.0.1:8080; } server { listen 80; location / { proxy_passhttp://server_ pool; proxy_next_upstreamhttp_502 http_504 error timeout invalid_header; proxy_cache cache_one; proxy_cache_valid 200304 12h; proxy_cache_ valid 301302 1m; proxy_cache_valid any 1m; proxy_cache_ key$host$uri$is_args$args; proxy_set_header Host$host; proxy_set_headerX-Forwarded-For $remote _addr; expires 1d; } access_log /var/log/nginx/cache_access.log main; } server { listen 8080; location / { root/usr/share/nginx/html; index index.htmlindex.htm; } }}
2.4 Configuration Log
To observe the cache's hit state, we can record cache-related variables in the log.
To define the log format:
Log_format Main ' $remote _addr-$remote _user[$time _local] "$request" "$status $body _bytes_sent" $http _r Eferer "" "$http _user_agent" "$http _x_forwarded_for" "" Addr: $upstream _addr-status: $upstr Eam_status-cachestatus: $upstream _cache_status "';
# where upstream_addr records the backend node IP of thedistribution, Upstream_status records the status code returned by the backend node;upstream_cache_ Status records the hit condition of the cache.
To reference the log in the reverse proxy label:
Access_log/var/log/nginx/cache_access.log main;
Nginx Reload Configuration:
[Email protected]>> nginx-s Reload
2.5 Monitoring Cache
Monitoring events for cache files
Browse the website:
[Email protected]_cache>> inotifywait -mrq/tmp/ngx_cache//tmp/ngx_cache/ create, Isdir 6/tmp/ngx_cache/ open,isdir 6/tmp/ngx_cache/ close_nowrite,close,isdir6/tmp/ngx_ Cache/ create,isdir 1/tmp/ngx_cache/ open,isdir 1/tmp/ngx_cache/ close_nowrite,close, Isdir1/tmp/ngx_cache/ create,isdir 3/tmp/ngx_cache/ open,isdir 3/tmp/ngx_cache/ close _nowrite,close,isdir3/tmp/ngx_cache/3/ create,isdir fd/tmp/ngx_cache/3/ open,isdir fd/tmp/ ngx_cache/3/close_nowrite,close,isdir fd/tmp/ngx_cache/3/fd/ createdd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ opendd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ Modifydd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ close_write, Closedd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ moved_ fromdd404cd351f6b9efb072e5806dc2efd3.0000000026/tmp/ngx_cache/3/fd/ Moved_todd404cd351f6b9efb072e5806dc2efd3
Description: With the last few lines, the picture is cached in the directory.
Tip: This content comes from older boys education OPS 23, cloud computing and DevOps Senior Architect Course 13 student notes
Read more about the old boys ' Education Linux course http://www.oldboyedu.com
This article is from the "Old Boys Linux Training" blog, make sure to keep this source http://oldboy.blog.51cto.com/2561410/1884326
Caching Web site data practices with Nginx cache