Nginx实战-缓存服务proxy(1)

2018-12-23 00:06:43 george518 ...

什么是Nginx缓存服务

nginx作为web服务器或者代理服务器,可以将应用的内容缓存到nginx服务器,第二次访问的时候,nginx不用再次分发请求给应用服务器,而是把自己缓存的内容直接返回给用户,这样提高了访问速度,也减轻了应用服务器的负担。

当然,缓存服务的内容是相对静态的,或者说在一定时间内不会变化的。

Nginx缓存服务配置

upstream slbserver_cache {
         server  127.0.0.1:8081 weight=1 max_fails=2  fail_timeout=2;
         server  127.0.0.1:8082 weight=1 max_fails=2  fail_timeout=2;
         server 127.0.0.1:8083 backup; #调用backup服务器,可以是本机或其他服务器。
}
#要想开启nginx的缓存功能,需要添加此处的两行内容!注意配置在http模块下
#设置Web缓存区名称为cache_test,内存缓存空间大小为10M,缓存的数据超过1天没有被访问就自动清除;
#访问的缓存数据,硬盘缓存空间大小为1G,如果缓存量超过1g,则采用lru算法淘汰。 
proxy_cache_path /opt/app/cache_test levels=1:2 keys_zone=cache_test:10m inactive=1d max_size=1g;
#创建缓存的时候可能生成一些临时文件存放的位置
proxy_temp_path /opt/app/cache_temp; 
server {
    listen       1564;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {

         proxy_pass  http://slbserver_cache; #通过upstrean定义的服务器组名调用后端服务器
         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          #跟后端服务器连接超时时间,发起握手等候响应时间
         proxy_connect_timeout 300;
         #后端服务器回传时间,就是在规定时间内后端服务器必须传完所有数据
          proxy_send_timeout 300;
          #连接成功后等待后端服务器的响应时间,已经进入后端的排队之中等候处理
         proxy_read_timeout 600;
         #代理请求缓冲区,会保存用户的头信息以供nginx进行处理
         proxy_buffer_size 256k;
         #同上,告诉nginx保存单个用几个buffer最大用多少空间
         proxy_buffers 4 256k;
         #如果系统很忙时候可以申请最大的proxy_buffers
         proxy_busy_buffers_size 256k;
         #proxy缓存临时文件的大小 
         proxy_temp_file_write_size 256k;
         #如果某台应用服务器出现500,400或者超时情况,直接访问下一台
         proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
         proxy_max_temp_file_size 128m;
         #使用Web缓存区cache_test,proxy_cache_path中设置好的那个名字,关闭参数为off。
         proxy_cache cache_test ;
         #对不同HTTP状态码缓存设置不同的缓存时间
         proxy_cache_valid 200 304 1m ; 
         #设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存,这里根据"域名,URI,
         #参数"组合成Key
         proxy_cache_key $uri$is_args$args;
    }

如何清除缓存

使用到第三方模块,ngx_cache_purge.
如果使用yum安装nginx,那先下载对应的nginx源码包
查看nginx版本

nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

然后下载对应版本

#wget http://nginx.org/download/nginx-1.14.2.tar.gz

下载ngx_cache_purge ,并解压

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz

解压nginx源码包,cd进去,根据Nginx -V命令获取的编译配置,在加上:
—add-module=/opt/download/ngx_cache_purge-2.3
如下:

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module  --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/opt/download/ngx_cache_purge-2.3 

#make 

#make install

如果没有安装openssl 请先安装

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

nginx 配置

 #用于清除缓存的url设置
    #假设一个URL为http://www.wangshibo.com/test.gif,那么就可以通过访问http://www.wangshibo.com/purge/test.gif清除该URL的缓存。
    location ~ /purge(/.*) {
        #设置只允许指定的IP或IP段才可以清除URL缓存
        allow all;
        #allow 192.168.11.139;
        #deny all ;
        proxy_cache_purge cache_test $1$is_args$args;
    }

访问:http://192.168.11.139:1564/default.png
两次访问,可以看到缓存生效
删除缓存使用 http://192.168.11.139:1564/purge/default.png

1、配置ngx_cache_purge完毕后一定要重启,先stop在start,reload不好使
2、ngx_cache_purge一定要放到location /最后

其他

缓存可以选择文件缓存

未完待续

相似文章