• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

利用Proxy Cache使Nginx对静态资源进行缓存

mysql 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论

前言 Nginx是高性能的HTTP服务器,通过Proxy Cache可以使其对静态资源进行缓存。其原理就是把静态资源按照一定的规则存在本地硬盘,并且会在内存中缓存常用的资源,从而加快静态资源的响应。 配置Proxy Cache 以下为nginx配置片段: proxy_temp_path /usr/lo

前言

Nginx是高性能的HTTP服务器,通过Proxy Cache可以使其对静态资源进行缓存。其原理就是把静态资源按照一定的规则存在本地硬盘,并且会在内存中缓存常用的资源,从而加快静态资源的响应。

配置Proxy Cache

以下为nginx配置片段:

<code>proxy_temp_path   /usr/local/nginx/proxy_temp_dir 1 2;#keys_zone=cache1:100m 表示这个zone名称为cache1,分配的内存大小为100MB#/usr/local/nginx/proxy_cache_dir/cache1 表示cache1这个zone的文件要存放的目录#levels=1:2 表示缓存目录的第一级目录是1个字符,第二级目录是2个字符,即/usr/local/nginx/proxy_cache_dir/cache1/a/1b这种形式#inactive=1d 表示这个zone中的缓存文件如果在1天内都没有被访问,那么文件会被cache manager进程删除掉#max_size=10g 表示这个zone的硬盘容量为10GBproxy_cache_path  /usr/local/nginx/proxy_cache_dir/cache1  levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g;server {    listen 80;    server_name *.example.com;    #在日志格式中加入$upstream_cache_status    log_format format1 '$remote_addr - $remote_user [$time_local]  '        '"$request" $status $body_bytes_sent '        '"$http_referer" "$http_user_agent" $upstream_cache_status';    access_log log/access.log fomat1;    #$upstream_cache_status表示资源缓存的状态,有HIT MISS EXPIRED三种状态    add_header X-Cache $upstream_cache_status;    locati<b>本文来源gao@!dai!ma.com搞$$代^@码5网@</b>on ~ .(jpg|png|gif|css|js)$ {        proxy_pass http://127.0.0.1:81;        #设置资源缓存的zone        proxy_cache cache1;        #设置缓存的key        proxy_cache_key $host$uri$is_args$args;        #设置状态码为200和304的响应可以进行缓存,并且缓存时间为10分钟        proxy_cache_valid 200 304 10m;        expires 30d;    }}</code>

安装Purge模块

Purge模块被用来清除缓存

<code>$ wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz$ tar -zxvf ngx_cache_purge-1.2.tar.gz</code>

查看编译参数

<code>$ /usr/local/nginx/sbin/nginx -V </code>

在原有的编译参数后面加上--add-module=/usr/local/ngx_cache_purge-1.2

<code>$ ./configure --user=www --group=www --prefix=/usr/local/nginx \--with-http_stub_status_module --with-http_ssl_module \--with-http_realip_module --add-module=/usr/local/ngx_cache_purge-1.2$ make && make install</code>

退出nginx,并重新启动

<code>$ /usr/local/nginx/sbin/nginx -s quit$ /usr/local/nginx/sbin/nginx</code>

配置Purge

以下是nginx中的Purge配置片段

<code>location ~ /purge(/.*) {    #允许的IP    allow 127.0.0.1;    deny all;    proxy_cache_purge cache1 $host$1$is_args$args;}</code>

清除缓存

使用方式:

<code>$ wget http://example.com/purge/uri</code>

其中uri为静态资源的URI,如果缓存的资源的URL为http://example.com/js/jquery.js,那么访问http://example.com/purge/js/jquery.js则会清除缓存。

命中率

保存如下代码为hit_rate.sh:

<code>#!/bin/bash# author: Jeremy Wei # proxy_cache hit rateif [ $1x != x ] then    if [ -e $1 ] then        HIT=`cat $1 | grep HIT | wc -l`        ALL=`cat $1 | wc -l`        Hit_rate=`echo "scale=2;($HIT/$ALL)*100" | bc`        echo "Hit rate=$Hit_rate%"    else        echo "$1 not exsist!"    fielse    echo "usage: ./hit_rate.sh file_path"fi</code>

使用方式

<code>$ ./hit_rate.sh /usr/local/nginx/log/access.log</code>

参考:

http://wiki.nginx.org/HttpProxyModule


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:利用Proxy Cache使Nginx对静态资源进行缓存

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址