nginx+mongodb-gridfs+squid

 

Nginx+mongodb-gridfs+squid

圖片存儲

通過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

Make install

 

 

Nginx配置

user nobody;

worker_processes 4;

error_log  logs/nginx_error.log  crit;

pid        /usr/local/nginx/nginx.pid;

worker_rlimit_nofile 20480;

events

{

 use epoll;

 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/www levels=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-javascript text/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;

 

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

 

 

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 0 no-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 16 256       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 16 256   min-size=250000

通過把內存掛載到硬盤的方法,來把圖片保存至內存,提高訪問速度

 

效果

頁面

 

 

訪問效果

 

Squid緩存效果,這時停掉後端的mongodb,訪問還是沒有問題的.圖片依然可以正常顯示

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章