`
hai0378
  • 浏览: 533643 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

nginx 专用高速缓存图片服务器

 
阅读更多

nginx 专用高速缓存图片服务器

<iframe id="baidu_clb_slot_iframe_523974" style="margin: 0px; display: block; vertical-align: bottom; border: 0px; padding: 0px;" src="about:blank" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="438" height="55"></iframe>

nginx 专用高速缓存图片服务器

系统环境:CentOS release 5.3 (Final) 2.6.18-128.el5 #1 SMP Wed Jan 21 10:44:23 EST 2009 i686 i686 i386 GNU/Linux

软件包:
http://www.51app.org/tools/nginx-1.3.9.tar.gz
http://www.51app.org/tools/ngx_cache_purge-2.0.tar.gz
http://www.51app.org/tools/pcre-8.32.tar.bz2

#useradd www   //我们将使用www用户运行nginx
#yum -y install openssl  //使nginx 加载openssl模块

软件包存放路径:/usr/local/src/

#cd /usr/local/src/

#tar -jxvf pcre-8.32.tar.bz2 && cd pcre-8.32
#./configure 

出现如下信息:
pcre-8.32 configuration summary:

    Install prefix .................. : /usr/local
    C preprocessor .................. : gcc -E
    C compiler ...................... : gcc
    C++ preprocessor ................ : g++ -E
    C++ compiler .................... : g++
    Linker .......................... : /usr/bin/ld
    C preprocessor flags ............ : 
    C compiler flags ................ : -O2 -fvisibility=hidden
    C++ compiler flags .............. : -O2 -fvisibility=hidden -fvisibility-inlines-hidden
    Linker flags .................... : 
    Extra libraries ................. : 
    Build 8 bit pcre library ........ : yes
    Build 16 bit pcre library ....... : no
    Build 32 bit pcre library ....... : no
    Build C++ library ............... : yes
    Enable JIT compiling support .... : no
    Enable UTF-8/16/32 support ...... : no
    Unicode properties .............. : no
    Newline char/sequence ........... : lf
    \R matches only ANYCRLF ......... : no
    EBCDIC coding ................... : no
    EBCDIC code for NL .............. : n/a
    Rebuild char tables ............. : no
    Use stack recursion ............. : yes
    POSIX mem threshold ............. : 10
    Internal link size .............. : 2
    Match limit ..................... : 10000000
    Match limit recursion ........... : MATCH_LIMIT
    Build shared libs ............... : yes
    Build static libs ............... : yes
    Use JIT in pcregrep ............. : no
    Buffer size for pcregrep ........ : 20480
    Link pcregrep with libz ......... : no
    Link pcregrep with libbz2 ....... : no
    Link pcretest with libedit ...... : no
    Link pcretest with libreadline .. : no
    Valgrind support ................ : no
    Code coverage ................... : no

#make && make install

#tar -zxvf ngx_cache_purge-2.0.tar.gz
#tar -zxvf nginx-1.3.9.tar.gz
#cd nginx-1.3.9
#./configure --user=www --group=www --add-module=../ngx_cache_purge-2.0 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

出现如下信息:
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

#make && make install

我们先直接启动下nginx,看有没有安装成功,界面如下:
#cd /usr/local/nginx/sbin
#./nginx       //测试配置文件语法 #./nginx -t


有时出现如下错误:
[root@localhost sbin]# ./nginx 
./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

处理如下:
x86 [root@localhost sbin]# ln -s /usr/local/lib/libpcre.so.1 /lib
x64 [root@localhost sbin]# ln -s /usr/local/lib/libpcre.so.1 /lib64

这边是用一台server开启了2个应用测试,一台缓存,一台后端。
用到了2个目录:img_temp:临时文件 img_cache:缓存文件

nginx 配置文件如下(需要按实际服务器性能情况修改):

user  www;
worker_processes  1;
worker_rlimit_nofile 30000;
        
error_log  logs/error.log; 

pid        logs/nginx.pid;
        
events {
        use epoll;
        worker_connections  35000;
 

http {  
        include       mime.types; 
        default_type  application/octet-stream;
        charset utf-8;
        sendfile        on;
        tcp_nopush     on;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 10m;

        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;

        keepalive_timeout  65;

        gzip on;
        gzip_disable     "MSIE [1-6]\.";
        gzip_proxied any;
        gzip_comp_level 5;
        gzip_buffers 16 8k;
        gzip_min_length    1k;
        gzip_vary on;
        gzip_types text/plain text/css image/gif image/jpeg image/png application/json application/x-javascript text/xml application
/xml application/xml+rss text/javascript;

        proxy_temp_path /usr/local/cache/img_temp;
        proxy_cache_path /usr/local/cache/img_cache levels=1:2 keys_zone=pic_cache:500m inactive=1d max_size=10g;

    server {
        listen       80;
        server_name  192.168.198.130;
        access_log  logs/pic.log;

        location / {
                proxy_cache pic_cache;
                proxy_cache_valid 200 304 24h;
                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.198.130:88;
                expires      1d;
        }

        location ~ /purge(/.*) {
                allow       127.0.0.1;
                allow       192.168.198.0/24;
                deny    all;
                proxy_cache_purge    pic_cache   $host$1$is_args$args;
        }
    error_page   500 502 503 504  /50x.html;
    }

#假设后端
server {
        listen 88;
        server_name 192.168.198.130;
        root /usr/local/cache/img_s;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
                expires      1d;
                access_log logs/88pic.log;  //方便测试时查看使用
        }
}

}

测试:

1.我们放一张qq.jpg的图片到后端的/usr/local/cache/img_s 目录下, 
2.访问http://192.168.198.130 , 访问成功!查看后端日志文件如下图:


3.清楚浏览器缓存,再次访问http://192.168.198.130,访问成功!查看后端日志文件如下图:

说明我们这次访问没有去到后端了!直接拿缓存文件。

4.在看下缓存目录下,多了基层目录和一个串文件了,那么我们清除下缓存,格式如下:
http://192.168.198.130/purge/qq.jpg

5.看到如下图:

6.我们重复2,3步骤,再看日志,确认成功。
 

分享到:
评论

相关推荐

    强烈推荐 打造高性能nginx缓存服务器

    ### 强烈推荐打造高性能Nginx缓存服务器 #### Nginx缓存概述 Nginx是一款广泛使用的高性能HTTP服务器及反向代理服务器。从0.7.48版本开始,Nginx引入了缓存功能,允许将请求的结果缓存起来,从而提高响应速度并...

    nginx图片缓存服务器配置文档.pdf

    为了实现主站与Nginx之间的数据交互,myimages目录被设置为Nginx图片服务器的主目录,并通过Samba服务与Windows系统共享,使得.NET程序能够方便地进行图片的上传和管理。尽管这种方案简化了操作,但最佳实践建议将...

    nginx配置图片服务器

    2. 配置 Nginx 配置文件:在 Nginx 的配置文件中,需要指定图片服务器的访问地址、缓存目录、临时目录等信息。 Nginx 配置文件的参数 1. user:指定 Nginx 运行的用户和用户组 2. worker_processes:指定 Nginx 的...

    Nginx服务器上搭建图片缓存服务的基本配置解析

    -e $request_filename)` 判断如果请求的文件在本地不存在,则通过`proxy_pass`转发到图片服务器(`http://img.example.com`)。默认的缓存路径位于 `/var/cache/nginx/proxy_temp`,可以根据需要更改。 然而,proxy_...

    nginx 负载均衡与缓存服务器标准配置文件

    nginx 负载均衡与缓存服务器标准配置文件

    nginx静态文件缓存的解决方案1

    【Nginx 静态文件缓存解决方案】 Nginx 是一款高性能的 HTTP 和反向代理服务器,常用于处理静态资源,以减轻后端服务器的压力。为了进一步提高静态资源的访问效率,我们可以利用 Nginx 的缓存功能。下面详细介绍...

    nginx作为http图片服务器示例

    总结,使用Nginx作为HTTP图片服务器,不仅可以充分利用其高性能,还能通过优化配置实现高效的图片访问和缓存管理,从而为用户提供流畅的浏览体验。在实际应用中,根据具体需求,还可以进一步配置防盗链、限速、负载...

    Nginx+ftp搭建图片服务器

    搭建一个图片服务器通常涉及到两部分:Nginx服务器和FTP服务器。Nginx作为一个高性能的HTTP和反向代理服务器,适合处理静态资源如图片,而FTP服务器则用于上传和管理这些图片。 一、Nginx作为图片访问服务 1. **...

    Nginx高性能Web服务器详解(完整版)pdf下载

    7. **缓存机制**:Nginx可以作为缓存服务器,对常用或者动态生成的页面进行缓存,减少对后端服务器的请求次数,提高响应速度。 8. **SSL/TLS支持**:Nginx可以配置处理HTTPS请求,提供安全的网络通信,支持SNI...

    nginx图片服务器配置和https配置

    nginx图片服务器配置和https配置

    Nginx构建反向代理缓存服务器电子书

    Nginx构建反向代理缓存服务器电子书,电子书很详细的介绍了nginx构建反向代理的过程与知识

    Nginx 搭建图片服务器

    搭建图片服务器的业务场景分析: 在集群应用中,用户图片资源的分散管理会带来不便,导致数据同步变得复杂。因此,搭建图片服务器成为了解决这一问题的关键步骤。图片服务器不仅能集中存储和管理图片资源,还能通过...

    Nginx搭建图片服务器(静态资源缓存服务器).zip

    在构建Web服务时,Nginx因其高性能、轻量级和...总的来说,通过Nginx搭建图片服务器并实现静态资源缓存,可以显著提升用户访问速度,减轻后端服务器压力,同时提供了一种高效、可靠的解决方案来处理高并发的图片请求。

    windows服务器部署 nginx+tomcat+mysql服务器端部署 阿里云服务器部署及配置

    详细说明了windows服务器nginx+tomcat+mysql部署及配置(配置阿里云后台安全组,配置域名)很适合新手学习 附件中包含: 1.操作说明文档 2.操作录屏 3.安装所用到的软件安装包 1)Windows Server 2019 数据中心版 ...

    java实现客户端上传图片到ftp服务器,nginx提供http服务下载图片

    在Java分布式项目中,涉及到客户端上传图片到FTP服务器并由Nginx提供HTTP服务进行图片下载,这是一个典型的文件传输和Web服务集成的场景。这里主要涉及三个关键知识点:Java FTP客户端编程、Nginx服务器配置以及Java...

    nginx高性能web服务器.pdf

    对于静态资源处理,Nginx有高效的缓存机制,可以减少对后端应用服务器的压力。通过配置缓存策略,可以智能地缓存常访问的静态文件,加快用户访问速度。 在安全方面,Nginx支持SSL/TLS加密,可以保护网站数据传输的...

    使用Nginx作缓存服务器以及删除其缓存文件的方法

    Nginx作为缓存服务器,可以将代理服务器转发的请求结果缓存到磁盘上,当相同的请求再次出现时,直接从缓存中加载结果,而不需要重新连接到后端服务器。这样可以显著减少响应时间,提高系统性能。Nginx缓存服务器的...

    使用Nginx搭建图片服务器(windows环境下)

    根据给定文件信息,以下知识点将详细阐述如何在Windows环境下使用Nginx搭建图片服务器。 首先,搭建图片服务器涉及到的基本步骤是在Windows系统上下载并安装Nginx。在进行安装之前,访问Nginx官方网站下载适合...

    nginx-简单图片服务器解决方案.docx

    【Nginx 简单图片服务器解决方案】 在IT行业中,构建高效且可扩展的图片服务器对于提升用户体验至关重要。特别是随着互联网应用的发展,图片服务的需求日益增长,如何处理大量图片的上传、存储和访问成为了一个重要...

    Nginx高性能Web服务器实战教程+高清+完整书签

    - **缓存机制**:讲解Nginx的缓存功能,如何配置缓存策略以减少服务器压力和提高响应速度。 4. **URL重写** - **URL规则**:解析Nginx的rewrite模块,学习如何制定和应用URL重写规则,优化网站URL结构。 5. **...

Global site tag (gtag.js) - Google Analytics