当前位置: > Linux服务器 > nginx >

nginx高速缓存图片服务器配置教程

时间:2014-06-26 00:15来源:linux.it.net.cn 作者:IT网

系统环境:
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

 

安装:
 

复制代码代码示例:
#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
nginx图片缓存服务器

有时出现如下错误:
 

复制代码代码示例:
[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-javascripttext/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;  //方便测试时查看使用
        }
}

}

测试nginx图片缓存器:

1,放一张qq.jpg "/>的图片到后端的/usr/local/cache/img_s 目录下, 
2,访问http://192.168.198.130 , 访问成功!查看后端日志文件如下图:
nginx图片缓存服务器

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

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

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

5,看到下图:
nginx图片缓存服务器

6,重复2,3步骤,再看日志,确认nginx 图片缓存成功。

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容