开发者社区 问答 正文

自建OSS前端教程( 节省OSS流量 )

相信广大坛友知道OSS的都是被他的各种特性所吸引。但是应用做大了,流量上去了,钱花的太快,怎么办?没事,你可以用云主机来中转OSS的流量,节约流量和请求开销。
I think everyone know oss was attracted by it's some feature.But the application grows day by day and the traffic rises,money takes more and more,what should we do?it's okay,we may build our owned oss front end to transfer the traffic and request to save their spending.

组成:
What we need before we continuing?

1.主机
配置自己的阿里云主机,做OSS的前端服务器。建议采用 tengine 做OSS前端服务器。
1.A alibaba cloud ecs server,it will turn to the oss front end ,i advise you us the tengine to compile to oss front end http service.

2.DNS
使用DNSPOD免费DNS服务,和DNSPOD免费的D监控服务实现故障切换。
2.A smart dns service ,such as dnspod.cn . it will change your a record to oss origin after your front end down.

教程部分
本教程以笔者博客的图片的OSS前端实现为例, 描述一次OSS前端搭建的完整过程:
*.主机配置: (1G 1核 5mbps) x 3  建议生产环境的应用使用时,主机带宽选用5mbps或更大,建议购买多台以便热备切换。
oss bucket :  safe121
一  服务器环境:
1.搭建前端环境:
*.如果你安装了tengine 1.4.x,此步可以无视。
下载tengine: wget http://tengine.taobao.org/download/tengine-1.4.5.tar.gz;tar zxvf tengine-1.4.5.tar.gz;cd tengine-1.4.5;
编译:需要开启--with-http_upstream_check_module 主动检查OSS的内外网IP的健康性
./configure --with-openssl=***修改成openssl路径*** --with-pcre=***修改成pcre路径*** --with-zlib=***修改成zlib路径*** --with-http_upstream_check_module ;make && make install
编译完成后,你的tengine就安装成功了。
编辑nginx.conf
vi /usr/local/nginx/conf/nginx.conf
在http{}中添加缓存区配置
proxy_cache_path /dev/shm/ngx levels=1:2 keys_zone=global:256m inactive=10m max_size=10m;在http{}中添加 OSS后端池
upstream oss_backend {
    server oss-internal.aliyuncs.com:80 max_fails=1 fail_timeout=3s;
    server oss-internal.aliyuncs.com:8080 max_fails=1 fail_timeout=3s;
    server oss.aliyuncs.com:80 max_fails=1 fail_timeout=3s;
    server oss.aliyuncs.com:8080 max_fails=1 fail_timeout=3s;
    keepalive_timeout 3600;
    check interval=3000 rise=2 fall=1 timeout=1000;
}

然后在http{}中添加server
server{
    server_name lebastatic.vip.thiscache.com;
    listen 80;

    location /safe121/ { #此处的safe121要改成你的bucketname
        expires 3650d;   #缓存十年
        proxy_pass http://oss_backend;
        proxy_no_cache 0;
        proxy_ignore_headers "Cache-Control" "Expires";
        proxy_cache_valid 200 301 302 304 86400s;
        proxy_cache_valid 403 404 400 500 10s;
        proxy_cache_key $host$http_referer$uri$is_args$args$http_if_none_match$http_if_modified_since;
        proxy_cache global;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; #当出现指定错误时,自动转发请求到下一个可用的后端
        proxy_connect_timeout 1s;
        proxy_read_timeout 2s;
        add_header X-Thislan-Cache '$upstream_cache_status from $hostname';
    }
}

然后运行下面命令重载nginx
/usr/local/nginx/sbin/nginx -s reload
用以上方法将所有要做前端的伺服器都配置好。


二.DNSPOD


将想要做前端的域名,A记录指向到你的前端IP。


添加D监控,如下配置:
*.监控任务设置为http://你的前端域名/bucketname/a_small_size_object。
切换规则的备用IP要设置为oss的公网IP。可以ping oss.aliyuncs.com获得。




保存,然后坐等DNS生效, 应该就生效了。


演示图片[来源 www.it68.com.cn]:
http://lebastatic.vip.thiscache.com/safe121/attachment/blogpost/-1gOPkIirJkY/UYUK0HXPd4I/AAAAAAAAA_Q/6R2DRz1z5z8/h56/it68_logo.png
http://oss.aliyuncs.com/safe121/attachment/blogpost/-1gOPkIirJkY/UYUK0HXPd4I/AAAAAAAAA_Q/6R2DRz1z5z8/h56/it68_logo.png

展开
收起
enj0y 2013-05-12 00:52:34 19341 分享 版权
2 条回答
写回答
取消 提交回答
  • 回 1楼(时尚网事) 的帖子
    看命中率。
    2013-05-14 01:45:50
    赞同 展开评论
  • Re自建OSS前端教程节省OSS流量
    很好很强大!!!!    大概能省多少呢~
    2013-05-13 18:05:07
    赞同 展开评论