图片服务器搭建 开源springboot 图片服务器 搭建图片web服务器
一、图片服务器机制
为了提高图片服务器性能,可利用Nginx中反向代理缓存机制达到目的;本机制共需两台服务器,在图片存储服务器外另部署一台服务器作为图片代理服务器(在一台服务器上同时部署也可),当客户端请求图片信息时,首先访问图片代理服务器,代理服务器会缓存热点图片,从而提高效率。
1、架构图
2、反向代理简介
反向代理是Web服务器隐藏在代理服务器之后,实现这种机制的服务器称作反向代理服务器(Reverse Proxy Server)。此时,Web服务器成为后端服务器,反向代理服务器称为前端服务器。
3、Nginx反向代理缓存机制简介
3.1 工作原理
Nginx的proxy_cache缓存机制为当用户请求图片等静态资源时,首先在代理服务器内存索引中查找是否存在该资源的缓存,如果不存在,则访问存储服务器,查找并读取该资源,响应请求并将其加入缓存;相反如果存在则在缓存中读取并响应请求,从而减少了对存储服务器的访问,提升了性能。
3.2 缓存机制
缓存文件采用内存+硬盘方式缓存,采用md5编码hash后保存,内存中保存的为缓存的索引信息,资源的缓存文件则存在硬盘中,但由于采用了hash算法,相对与无缓存查找还是提升了性能。
相对与memcache缓存,由于memcache将缓存的文件全部缓存在内存中,速度上更优于proxy_cache,但当遇到断电、宕机时,memcache中缓存将全部丢失,可谓各有利弊。此外memcache向缓存中加入数据需要通过编码的方式主动去添加,这样需要统计访问量识别哪些是访问量高的热点图片需要缓存,哪些不需要,还需要日后的维护,而proxy_cache只需一次设置图片的路径后,通过配置文件采取合适的方案即可,无需日后的维护。
3.3 缓存清理
Proxy_cache的清理方式有两种,自动清理和手动清理:
自动清理,配置inactive参数后,如1天,则该缓存中有1天未被请求,则自动清理该资源;此外当内存区达到上限后,将用新的资源取代访问次数最少的。
手动清理,需集成第三方proxy_cache_purge,如想清理URL为http://192.168.1.100/aaa.png 图片,请求http://192.168.1.100/purge/aaa.png即可。
4、Nginx gzip简介
gzip是GNU zip的缩写,它是一个GNU自由软件的文件压缩程序,可以极大的加速网站,但对于图片压缩来说,几乎没有提升,最好不要使用Gzip。
二、测试
第一次访问某图片时:
***22/Oct/2013:19:29:14 -0700 MISSCache-Control: - Expires: - "GET /web/upload/slide/13706572516644398.jpgHTTP/1.1" (200) "Mozilla/5.0 (X11; U; Linux x86_64; en-US;rv:1.9.2.9) Gecko/20110412 CentOS/3.6.9-2.el6.centos Firefox/3.6.9"MISS表示未被缓存。
第二次访问某图片时:
***22/Oct/2013:19:30:40 -0700 HITCache-Control: - Expires: - "GET /web/upload/slide/13706572516644398.jpgHTTP/1.1" (200) "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2;WOW64; Trident/6.0; MALCJS)"HIT表示已被缓存。
共包含以下几种状态可以在缓存日志中查看:
· MISS 未命中,请求被传送到后端
· HIT 缓存命中
· EXPIRED 缓存已经过期请求被传送到后端
· UPDATING 正在更新缓存,将使用旧的应答
· STALE 后端将得到过期的应答
· BYPASS 缓存被绕过了
附录
附录为本次测试的配置文件:
#user nobody; # nginx进程数,多少cpu多少进程 worker_processes 4; #允许打开的连接数 worker_rlimit_nofile 10240; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { #单进程允许打开的链接数 worker_connections 10240; } http { include mime.types; default_type application/octet-stream; charset utf-8; #服务器名的hash库大小 server_names_hash_bucket_size 128; #服务器名的hash库大小 client_header_buffer_size 4 8k; #客户端长头文件缓存大小及分配模式 4块 每块8K large_client_header_buffers 4 32k; #客户端最大数据段大小 client_max_body_size 300m; sendfile on; tcp_nopush on; #连接存活时间 keepalive_timeout 60; tcp_nodelay on; client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; log_format cache '***$time_local ' '$upstream_cache_status ' 'Cache-Control:$upstream_http_cache_control ' 'Expires:$upstream_http_expires ' '"$request"($status) ' '"$http_user_agent" '; #gzipon; #gzip_min_length 1k; #gzip_buffers 4 16k; #gzip_http_version1.1; #gzip_comp_level2; #gzip_types text/plain application/x-javascript text/cssapplication/xml; #gzip_varyon; proxy_temp_path /home/cache/proxy_temp_path; #缓存路径,级别,检测时间,活动时间 proxy_cache_path /home/cache/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; server { listen 80; server_name 192.168.242.130; location / { proxy_cache cache_one; proxy_cache_valid 200 304 12h; proxy_cache_key $host$uri$is_args$args; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://192.168.242.130:8080; access_log /opt/nginx/cache.logcache; expires 1d; } location ~ /purge(/.*) { allow 127.0.0.1; #allow 192.168.0.0/255; #deny all; proxy_cache_purge cache_one $host$1$is_args$args; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://192.168.242.130:80; } access_log off; } server { listen 8080; server_name 192.168.242.130; location / { root /home/www/; } access_log off; } }
SpringBoot 整合 Langchain4j RAG 技术深度使用解析springboot整合slf4j
springboot 服务器ip springboot 服务器推送
springboot 连接mqtt服务器 springboot tcp服务器
windows服务器 springboot自动部署 部署springboot到服务器
springboot windows服务器启动 springboot应用服务器
在windows server2012 r2 服务器上部署springboot 服务器运行springboot
springboot内置服务器 springboot 文件服务器
springboot windows 部署 springboot如何部署到服务器