nginx配置图片服务器

这几天研究了一下nginx配置图片服务器的相关内容,个人的一些收获与大家分享一下:

Nginx是目前非常流行的web服务器,它起源于俄罗斯。它具有处理速度快,并发量大,占用资源极低等优点,尤其对于静态资源的处理更佳,有测试证明是apache的30倍。现在已经广泛的应用于多家门户网站、中大型网站中,作为反向代理、图片缓存服务器等。
本例是结合张宴的blog(http://blog.s135.com/nginx_cache/),做的变动。
主站是IIS7.5(192.168.36.70),用的是.net 3.5+sql 2005,用于处理aspx,ashx等动态网页程序,而网站的图片服务器为nginx(centos 5.6,IP:192.168.33.243).


nginx图片服务器配置:

 1.centos5.6上安装nginx.安装步骤如下:

 wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.gz
tar zxvf pcre-8.00.tar.gz
cd pcre-8.00/
./configure
make && make install
cd ../

wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
tar zxvf ngx_cache_purge-1.0.tar.gz

wget http://nginx.org/download/nginx-0.8.32.tar.gz
tar zxvf nginx-0.8.32.tar.gz
cd nginx-0.8.32/
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.0--prefix=/usr/local/webserver/nginx --with-http_stub_status_module--with-http_ssl_module
make && make install
cd ../

 注意:要建立www用户和www用户组

2. 配置nginx的配置文件

   注意nginx的图片服务器访问地址为:192.168.33.243:8080,nginx缓存服务器为:192.168.33.243,故我们需要在Nginx上配置两个web服务器,让后一个缓存前一个,iis主站的程序直接调用缓存服务器的图片,绕过了真实的图片服务器。

  配置文件如下:

 user  www www;

 worker_processes 8;

error_log  /usr/local/webserver/nginx/logs/nginx_error.log  crit;

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

#Specifies the value for maximum file descriptors that can be opened by thisprocess.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}

http
{
  include       mime.types;
  default_type  application/octet-stream;

  charset  utf-8;
      
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  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;

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types       text/plainapplication/x-javascript text/css application/xml;
  gzip_vary on;

  #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
  proxy_temp_path   /data0/proxy_temp_dir;
  #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
  proxy_cache_path  /data0/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200minactive=1d max_size=30g;
  
  #upstream backend_server {
   # server   192.168.8.43:80 weight=1max_fails=2 fail_timeout=30s;
    #server   192.168.8.44:80 weight=1max_fails=2 fail_timeout=30s;
    #server   192.168.8.45:80 weight=1max_fails=2 fail_timeout=30s;
  #}

  server //此处为缓存服务器
  {
    listen       80;
    server_name  192.168.33.243;
    

    location /
    {
       
         proxy_cache cache_one;
         #对不同的HTTP状态码设置不同的缓存时间
         proxy_cache_valid  200 304 12h;
         #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
         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.33.243:8080; //此处跳转到真实的图片服务器

log_format cache '***$time_local '

                    '$upstream_cache_status '

                    'Cache-Control: $upstream_http_cache_control '

                    'Expires: $upstream_http_expires '

                    '"$request" ($status) '

                    '"$http_user_agent" '; //定义日志格式(此日志格式可以显示hit miss等,显示缓存是否被击中,老版本默认可以,但是新版本,发现需要加上这个)

                  access_log  /var/log/nginx/cache.log cache; //使用这个日志格式


         expires      1d;
    }
    
    #用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
    location ~ /purge(/.*)
    {
     #设置只允许指定的IP或IP段才可以清除URL缓存。
     allow            127.0.0.1;
     allow            192.168.0.0/16;
     deny            all;
     proxy_cache_purge    cache_one   $host$1$is_args$args;
    }    

    #扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
    location ~ .*\.(php|jsp|cgi)?$
    {
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://backend_server;
    }

    access_log  off;
  }

server//真实的图片服务器

{

   Listen 8080;

   Server_name 192.168.33.243;

  Location /

  {

    root /images/;

}

access_log /***/***combined; //访问日志,一般都off掉。

}
}


至此,图片服务器搭建成功可以访问了








  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值