1.安装nginx相关依赖环境
yum groupinstall "Development Tools"
yum install pcre-devel zlib-devel openssl-devel
2.下载ngx_cache_purge-2.3缓存模块
wget -O /usr/src/ngx_cache_purge-2.3.tar.gz http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar xfvz ngx_cache_purge-2.3.tar.gz
3.安装nginx并添加缓存模块
wget http://nginx.org/download/nginx-1.6.0.tar.gz
tar -xzf nginx-1.6.0.tar.gz
./configure --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_stub_status_module --with-http_flv_module --with-http_ssl_module --with-http_dav_module --with-ipv6 --add-module=/usr/src/ngx_cache_purge-2.3
make && make install
4.检查是否安装成功
[root@vm10-20-10-32 ~]# nginx -V
nginx version: nginx/1.13.6
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --sbin-path=/usr/sbin --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_stub_status_module --with-http_flv_module --with-http_ssl_module --with-http_dav_module --with-ipv6 --add-module=/usr/src/ngx_cache_purge-2.3
5.配置nginx
proxy_cache_path /usr/local/nginx/proxy_cache_dir levels=1:2 keys_zone=cache1:200m inactive=5m max_size=1g;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://localhost.com;
}
location ~ /purge(/.*) {
proxy_cache_purge cache1 $host$1$is_args$args;
}
location ~ .*\.html$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost.com;
proxy_cache cache1; #设置资源缓存的zone
proxy_cache_key $host$uri$is_args$args; #设置缓存的key,以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_valid 200 304 5m;
access_log logs/cache.log main;
}