Nginx缓存代理服务器

缓存代理服务器

在代理服务器的磁盘中保存请求目标的内容,加快响应速度,减少应用服务器(后端服务器)上的资源开销,比如多客户端请求相同的资源,代理缓存命中后,对于应用服务器来说,只发生了一次资源调度。而浏览器上的缓存配置,一般来说是用来减少本地IO的,请求目标的内容会存放在浏览器本地。

 

缓存代理服务器192.168.179.99 http配置

http {
    include       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"';

    access_log  logs/access.log  main;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    proxy_cache_path /data/nginx/cache  max_size=10g levels=1:2  keys_zone=nginx_cache:10m 
    inactive=60m use_temp_path=off;
    include /usr/local/nginx/conf/vhost/*.conf;

}

--/data/nginx/cache   缓存资源存放路径

-- max_size   最大cache空间,如果不指定,会使用掉所有disk space,当达到配额后,会删除不活跃的cache文件

--  levels   设置缓存资源的递归级别,默认为levels=1:2,表示Nginx为将要缓存的资源生成的key从后依次设置两级保存,两级目录的意思。

--key_zone  在共享内存中设置一块存储区域来存放缓存的key和metadata,这样nginx可以快速判断一个request是否命中或者未命中缓存,1m可以存储8000个key,10m可以存储80000个key

--inactive 未被访问文件在缓存中保留时间,本配置中如果60分钟未被访问则不论状态是否为expired,缓存控制程序会删掉文件。inactive默认是10分钟。需要注意的是,inactive和expired配置项的含义是不同的,expired只是缓存过期,但不会被删除,inactive是删除指定时间内未被访问的缓存文件

-- use_temp_path 如果为off,则nginx会将缓存文件直接写入指定的cache文件中,而不是使用temp_path存储,official建议为off,避免文件在不同文件系统中不必要的拷贝

[root@www ~]# mkdir -p /data/nginx/cache  --创建缓存目录

 

代理缓存服务器192.168.179.99 server配置

server {
        listen       80;
        server_name  www.test.com;
        charset utf-8;
        location /{
        proxy_pass http://192.168.179.100;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_cache nginx_cache;
        proxy_cache_key $host$uri$is_args$args;
       proxy_cache_valid 200 304 302 1d;
 }
}

proxy_cache pcache:表示使用pcache缓存,这个使用之前必须先用proxy_cache_path定义。

proxy_cache_key string:缓存中用于“键”的内容;proxy_cache_key$host$uri$is_args$args;这里设置$host$uri$is_args$args作为键

 

后端服务器 192.168.179.100 server配置很简单就是客户端通过访问代理服务器拿到的是后端服务器上的index.html资源

server{
      listen  80;
      server_name localhost;
      charset utf-8;
      error_page 404 =200 /404.html;
      location /{
      root html;
     }
}

[root@www ~]# echo "this is houduan fuwuqi 192.68.179.100 index.html resouce" > /usr/local/nginx/html/index.html  --在后端服务器上修改index.html里面的信息

 

客户端访问缓存服务器

[root@localhost network-scripts]# curl 192.168.179.99  -- --客户端去访问代理服务器,拿到的是后端服务器上的资源

this is houduan fuwuqi 192.68.179.100 index.html resouce

 

[root@www 83]# cd /data/nginx/cache/  --这个时候可以看到代理服务器上缓存了后端服务器上的资源

[root@www cache]# ls

[root@www cache]# ls

e

[root@www cache]# cd e

[root@www e]# ls

72

[root@www e]# cd 72/

[root@www 72]# ls

1959bec6fcc5a5f1d12590dfe299f72e

可以看到生成的缓存,72红色构成了二级目录,e构成了第一级目录,剩下蓝色部分就是哈希值

 

测试缓存服务器是否真的可以缓存

[root@www ~]# rm -rf /usr/local/nginx/html/index.html  --在后端服务器上将资源删除

[root@localhost network-scripts]# curl 192.168.179.99   --可以看到即使删除了后端服务器上的资源也还是可以从代理服务器上的缓存里拿到资源,不再去通过代理服务器去访问后端去拿index.html资源了  

this is houduan fuwuqi 192.68.179.100 index.html resouce 

 

[root@www cache]# rm -rf /data/nginx/cache/*   --代理服务器删除缓存

[root@localhost network-scripts]# curl 192.168.179.99  --可以看到已经拿不到资源了

<html>

<head><title>403 Forbidden</title></head>

<body>

<center><h1>403 Forbidden</h1></center>

<hr><center>nginx/1.16.1</center>

</body>

</html>

 

 

 

 

 

发布了289 篇原创文章 · 获赞 323 · 访问量 39万+

猜你喜欢

转载自blog.csdn.net/qq_34556414/article/details/105017733