【Nginx基础】缓存服务proxy(1)

1 Nginx缓存服务简介

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


2 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;
}


3 如何清除缓存

使用到第三方模块,ngx_cache_purge;如果使用yum安装nginx,那先下载对应的nginx源码包
(1) 查看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'

(2) 下载对应版本

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

(3) 下载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

(4) 解压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

(5) 如果没有安装openssl 请先安装

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

(6) 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;
}

(7) 查看效果

访问: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 /最后


相关推荐

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3

  • Thinkphp5.1路径常量

    1 配置文件位置根目录/config/template.php2 配置文件内容<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reser

  • Thinkphp5.0路径常量

    1 配置文件位置根目录/application/模块名/config.php2 配置文件内容<?php//配置文件return [ // 后台视图输出字符串内容替换 'view_replace_str' => [ '__PUBLIC__' => '/', '__STATIC__' => '/static', '__CONSOLE__' => '/static/console', '__CONSOLE_CSS__' => '/static/console/css', '__CONSOLE_IMAGES__' => '/static/console/ima

  • wp站点防止别人进行DDOS攻击

    1 简介wp站点防止别人进行DDOS攻击。2 配置位置位置:根目录/wp-config.php3 配置内容在【根目录/wp-config.php】文件的开头添加如下代码:if(strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false){ $protocol = $_SERVER['SERVER_PROTOCOL'] ?? ''; if(!in_array($protocol, ['HTTP/1.1', 'HTTP/2', 'HTTP/2.0', 'HTTP/3'], true)){ $protocol = 'HTTP/1.0'; } hea

  • 只读属性

    1 只读属性简介只读属性的声明方式类似于普通属性,但需要使用 readonly 关键字。2 只读属性例子class Point { public readonly float $x; public readonly float $y; public function __construct(float $x, float $y) { $this->x = $x; $this->y = $y; }}$point = new Point(3.5, 2.8);echo $point->x; // 输出: 3.5echo $point->y; // 输出: 2.8// 下面的尝