Nginx+mongodb-gridfs+squid
图片存储
通过Nginx+mongodb-gridfs+squid实现简单的图片存储及图片缓存.
图片先从nginx本地cache里查找,然后到站点去找,再而到squid 里查找,都找不到最后才到mongodb-girdfs查找,然后把图片cache到squid.
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
squid分2个地方存储,小于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,访问还是没有问题的.图片依然可以正常显示