使用tmpfs 进行nginx proxy_cache 加速

实际碰到的一个问题,使用的虚拟机,发现压测效果不是很好,静态资源请求有时都会存在问题(超时,加载比较慢)
但是因为实际没有太好的虚拟机存储可以挂载,同时请求的静态资源比较多(图片,css,js,小文件),tmpfs 是一个
基于内存的文件系统,可以很好的支持文件操作,同时可以提高nginx cache 文件的访问

参考配置

  • tmpfs 创建
    /etc/fstab 挂载
 
mkdir -p   /data/nginx_caches1
tmpfs /data/nginx_caches1 tmpfs defaults,size=3072M 0 0
  • nginx 配置
    定义proxy_cache
 
proxy_cache_path  /data/nginx_caches1 levels=1:2   keys_zone=static_cache1:256m max_size=3g;

配置静态资源cache

location ~ .*\.(gif|jpg|jpeg|png|css|js|ico|mp4) { 
        proxy_cache static_cache1; 
        proxy_cache_key $uri$is_args$args;
        expires 30d;
        client_body_buffer_size 10M;
        client_max_body_size 10G;
        proxy_buffering on;
        proxy_buffers 1024 4k;
        proxy_cache_valid any 100h;
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 http_429;
        proxy_read_timeout 300;
        proxy_http_version 1.1;
        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_set_header Host $http_host;
        proxy_set_header Connection "";
        set $cip $remote_addr;
        if ($http_x_forwarded_for != "") {
         set $cip $http_x_forwarded_for;
        }
        proxy_set_header X-Forwarded-For $cip;  
        proxy_pass http://<upstreams>;
}

说明

如果文件系统不行,但是内存够用,临时使用tmpfs 是一个不错的选择,但是基于tmpfs 的问题也不少,比如易失性,空间大小限制。。。。可以参考nginx
官方的文档

参考资料

https://www.kernel.org/doc/html/v5.18/filesystems/tmpfs.html
https://en.wikipedia.org/wiki/Tmpfs
https://www.nginx.com/blog/cache-placement-strategies-nginx-plus/

posted on   荣锋亮  阅读(55)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-02-22 几款开源git server ssh 协议forced command 参考格式
2022-02-22 基于golang+openssh 服务实现一个简单的git over ssh 服务
2021-02-22 dremio sql server 查询中文的问题
2021-02-22 规避dremio数据查询限制的一些方法
2020-02-22 docker 几个不错的镜像加速地址
2020-02-22 chproxy 一个功能强大的clickhouse http 代理以及负载均衡中间件
2020-02-22 pgspider kafka fdw docker 镜像

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示