设为首页 收藏本站
查看: 744|回复: 0

[经验分享] 通过Nginx+mongodb-gridfs+squid实现简单的图片存储及图片缓存示例

[复制链接]

尚未签到

发表于 2016-12-28 09:08:33 | 显示全部楼层 |阅读模式
http://www.51document.com/resources/images//upload/20130410/f3ab0c92-92b1-402c-9052-f8f63d5b2b0d.png

图片存储
通过Nginx+mongodb-gridfs+squid实现简单的图片存储及图片缓存.
 
图片先从nginx本地cache里查找,然后到站点去找,再而到squid 里查找,都找不到最后才到mongodb-girdfs查找,然后把图片cachesquid.
 
Nginx编译安装
./configure--prefix=/usr/local/nginx --with-http_realip_module --with-http_addition_module--with-http_gzip_static_module --with-http_secure_link_module--with-http_stub_status_module --with-pcre=../pcre-8.02--add-module=../ngx_cache_purge-1.4 --with-zlib=../zlib-1.2.3/--add-module=../nginx-gridfs --add-module=./nginx_upstream_jvm_route/--with-http_stub_status_module
Make
Makeinstall
 
 
Nginx配置
user nobody;
worker_processes 4;
error_log logs/nginx_error.log  crit;
pid       /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 20480;
events
{
 useepoll;
 worker_connections 20480;
}
http
{
 upstream cd {
   server  127.0.0.1:8082;
  }
 
 include      mime.types;
 default_type application/octet-stream;
 charset UTF-8;
 server_names_hash_bucket_size 128;
 client_header_buffer_size 32k; sendfile on;
 tcp_nopush    on;
 keepalive_timeout 60;
 tcp_nodelay on;
 proxy_connect_timeout 30;
 proxy_read_timeout 60;
 proxy_send_timeout 20;
 proxy_buffer_size 96k;
 proxy_buffers 8 256k;
 proxy_busy_buffers_size 512k;
 proxy_temp_file_write_size 512k;
 proxy_temp_path /usr/local/nginx/proxy_temp;
 proxy_cache_path /usr/local/nginx/wwwlevels=1:2 keys_zone=cache_one:200m inactive=1d max_size=200m;
gzip on;
 gzip_proxied any;
 gzip_min_length  1k;
 gzip_buffers     4 16k;
 gzip_http_version 1.0;
 gzip_comp_level 2;
 gzip_types       text/plain application/x-javascripttext/css application/xml;
 gzip_vary on;
server_tokens off;
 proxy_redirect off;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For$http_x_forwarded_for;
{
  listen       80;
  server_name  192.168.2.248;
  index index.html;
  rewrite ^/image/(.*) /image/$1 last;
 
location ~ /purge(/.*)
   {
   proxy_cache_purge    cache_one$host$1$is_args$args;
   allow           all;
   deny          all;
   }   
 
 
location /  {
    proxy_pass http://cd;
}
 
location ~ .*\.(gif|png|jpg|jpge)$ {
    proxy_cache cache_one;
    proxy_cache_valid all 10m;
    proxy_cache_key $host$uri$is_args$args;
    proxy_pass http://cd;
    if (!-f $request_filename){
       proxy_pass http://192.168.2.248:8000;
       }
    }
 
access_log off;
 }
 
server
{
listen 8000;
server_name 192.168.2.241;
 
location /image/  {
 gridfs gfs
      field=filename
      type=string;
 mongo 192.168.2.241:38000;
}
 access_log off;
}
 
}
 
Mongodb配置
/usr/db/mongodb-linux-x86_64-2.0.2/bin/mongod--dbpath=/usr/db/data/sa --logpath=/usr/db/data/sa.log --port 38000 --fork--logappend --maxConns=500 --oplogSize=1000 –nohttpinterface
 
http://www.51document.com/resources/images//upload/20130410/15667c79-06da-46b9-adbf-f449ac1cabc6.png

Squid配置
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl Safe_ports port 80          # http
acl Safe_ports port 21          # ftp
acl Safe_ports port 443         # https
acl Safe_ports port 70          # gopher
acl Safe_ports port 210         # wais
acl Safe_ports port 1025-65535  # unregistered ports
acl Safe_ports port 280         # http-mgmt
acl Safe_ports port 488         # gss-http
acl Safe_ports port 591         # filemaker
acl Safe_ports port 777         # multiling http
acl CONNECT method CONNECT
acl Purge method PURGE
http_access allow all Purge
http_access allow manager localhost
http_access allow localhost
http_access allow all
icp_access allow all
http_port 8000 accel  vhost vport
cache_peer 192.168.2.241 parent 8000 0no-query originserver
hierarchy_stoplist cgi-bin ?
cache_mem 100 MB
maximum_object_size_in_memory 2000 KB
cache_dir ufs /home/cache 400 16 256  max-size=250000
cache_dir ufs /var/spool/squid 2000 16256       min-size=250000
maximum_object_size 409600 KB
cache_swap_low 80
cache_swap_high 85
logformat squid %ts.%03tu %6tr %>a%Ss/%03Hs %<st %rm %ru %un %Sh/%<A %mt
access_log /var/log/squid/access.log squid
cache_log /var/log/squid/cache.log
cache_store_log /var/log/squid/store.log
pid_filename /var/run/squid.pid
refresh_pattern -i \.gif$   100   20%     1440      ignore-reload
refresh_pattern -i \.jpg$    10    20%     20       ignore-reload
refresh_pattern -i \.png$   100   20%     1440      ignore-reload
refresh_pattern -i \.jpeg$  100   20%     1440      ignore-reload
refresh_pattern -i \.bmp$   100   20%     1440     ignore-reload
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
visible_hostname 192.168.2.248
dns_nameservers 202.96.134.33 8.8.8.8
hosts_file /etc/hosts
coredump_dir /var/spool/squid
 
squid2个地方存储,小于250k的图片存入内存,大于250K的图片存在磁盘
 
cache_dir ufs /home/cache 400 16 256  max-size=250000
cache_dir ufs /var/spool/squid 2000 16256   min-size=250000
通过把内存挂载到硬盘的方法,来把图片保存至内存,提高访问速度
 
效果
页面
http://www.51document.com/resources/images//upload/20130410/d56918b3-cce6-45d2-8b12-d314ba7fb889.png

访问效果
http://www.51document.com/resources/images//upload/20130410/3c28c3d6-e2c3-42f3-961b-728dbe9e42e2.png

Squid缓存效果,这时停掉后端的mongodb,访问还是没有问题的.图片依然可以正常显示
http://www.51document.com/resources/images//upload/20130410/0b694df5-57ec-4cd7-9c1c-7c3c07d15531.png
 
 
http://www.51document.com/1365591863693.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-320452-1-1.html 上篇帖子: Nginx开启Gzip压缩大幅提高页面加载速度及相关测试 下篇帖子: 软件级负载均衡器(LVS/HAProxy/Nginx)的特点和对比(网摘)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表