详解用详解用Nginx搭建搭建CDN服务器方法服务器方法(图文图文)
主要介绍了详解用Nginx搭建CDN服务器方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
利用利用Nginx的的proxy_cache搭建缓存服务器一:编译搭建缓存服务器一:编译ngx_cache_purge
1、Nginx的Proxy_cache是根据Key值md5哈希存储缓存,支持任意的Key,例如你可以根据”域名、URI、参数”组合成key,也支持非200状态码,如404/302等。
2、要利用Nginx的Proxy_cache,你需要在Nginx编译进ngx_cache_purge 模块,执行:nginx -V,查看有没有ngx_cache_purge 字样,没有的话需要自己手动编译。
Nginx搭建CDN手动编译
3、这里以Oneinstack编译ngx_cache_purge 模块作为操作演示,如果你用的是其它的LNMP包可以参考,基本过程是差不多的。命令如下:
cd /root/oneinstack/src #进入安装包目录
nginx -V
tar xzf nginx-1.10.3.tar.gz #根据上面查看到的nginx版本选择解压包
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar zxvf ngx_cache_purge-2.3.tar.gz
cd /root/oneinstack/src/nginx-1.10.3
# 下面的./configure 后加的参数,你可以直接复制刚刚用nginx -V得到的参数,然后在最后加上–add-module=../ngx_cache_purge-2.3即可,参考:
./configure –prefix=/usr/local/nginx –user=www –group=www –with-http_stub_status_module –with-http_v2_module –with-http_ssl_module –with-http_gzip_static_module –with-http_realip_module –with-http_flv_module –with-http_mp4_module –with-openssl=../openssl-1.0.2k –with-pcre=../pcre-8.39 –with-pcre-jit –with-ld-opt=-ljemalloc –add-module=../ngx_cache_purge-2.3
make
mv /usr/local/nginx/sbin/nginx{,$(date +%m%d)}
cp objs/nginx /usr/local/nginx/sbin #oneinstack,其它的可以不用这个操作
nginx -t
service nginx restart
4、安装完成后,再次nginx -V你就可以看到Nginx已经成功编译进了ngx_cache_purge 了。
二、利用二、利用Nginx的的proxy_cache搭建缓存服务器二:修改搭建缓存服务器二:修改Nginx配置文件配置文件
1、先找到你的Nginx配置文件:nginx.conf(路径一般是在/usr/local/nginx/conf/nginx.conf),在配置文件Http中加入以下代码:(注意修改路径为你自己的路径)
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;
proxy_cache_path /data/wwwroot/pic.freehao123.com levels=1:2 keys_zone=cache_one:200m inactive=30d max_size=5g;
proxy_temp_path /data/wwwroot/pic.freehao123.com/temp;
2、操作如下图:
3、然后在你的虚拟主机的nginx.conf(路径一般是/usr/local/nginx/conf/vhost/pic.freehao123.com.conf),在server listen 80 和 listen 443 ssl http2 都加入下面命令:
location /{
proxy_pass https://www.freehao123.com;
proxy_redirect off;
proxy_set_header Host www.freehao123.com;
proxy_cache cache_one;
proxy_cache_valid 200 302 304 365d;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
add_header Images-Cache “$upstream_cache_status from $host”;
add_header Pragma public;
add_header Cache-Control “public, must-revalidate, proxy-revalidate”;
access_log off; log_not_found off; expires max;
}
4、将配置文件保存重新上传,然后执行:
nginx -t
service nginx restart
5、先执行检查Nginx配置是否正确,确认没有问题的就是重启Nginx了。
评论0