利用Nginx的proxy_cache搭建緩存服務(wù)器一:編譯ngx_cache_purge
1、Nginx的Proxy_cache是根據(jù)Key值md5哈希存儲緩存,支持任意的Key,例如你可以根據(jù)”域名、URI、參數(shù)”組合成key,也支持非200狀態(tài)碼,如404/302等。
2、要利用Nginx的Proxy_cache,你需要在Nginx編譯進(jìn)ngx_cache_purge 模塊,執(zhí)行:nginx -V,查看有沒有ngx_cache_purge 字樣,沒有的話需要自己手動編譯。
Nginx搭建CDN手動編譯
3、這里以O(shè)neinstack編譯ngx_cache_purge 模塊作為操作演示,如果你用的是其它的LNMP包可以參考,基本過程是差不多的。命令如下:
cd /root/oneinstack/src #進(jìn)入安裝包目錄 nginx -V tar xzf nginx-1.10.3.tar.gz #根據(jù)上面查看到的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 后加的參數(shù),你可以直接復(fù)制剛剛用nginx -V得到的參數(shù),然后在最后加上–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已經(jīng)成功編譯進(jìn)了ngx_cache_purge 了。
二、利用Nginx的proxy_cache搭建緩存服務(wù)器二:修改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、然后在你的虛擬主機(jī)的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、將配置文件保存重新上傳,然后執(zhí)行:
nginx -t service nginx restart
5、先執(zhí)行檢查Nginx配置是否正確,確認(rèn)沒有問題的就是重啟Nginx了。
6、如果你想緩存gravatar頭像,那么代碼就是:
location /avatar{ proxy_pass http://cn.gravatar.com; proxy_redirect off; proxy_set_header Host cn.gravatar.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; }
7、現(xiàn)在打開你的二級域名:pic.freehao123.com,你就可以看到已經(jīng)正確緩存了圖片了。
8、這里再給出另一個Nginx緩存代碼,實現(xiàn)效果和上面是一樣的。
#先在Nginx配置中寫入以下命令: proxy_temp_file_write_size 128k; proxy_temp_path /data/wwwroot/pic.ucblog.net/temp; proxy_cache_path /data/wwwroot/pic.ucblog.net levels=1:2 keys_zone=cache_one:500m inactive=7d max_size=5g; #再在虛擬主機(jī)的Nginx配置中寫入以下命令: 先在server listen 80 和listen 443代碼前面加入: upstream gravatar { server secure.gravatar.com:443; } #再在server listen 80 和listen 443 里面加入: location / { proxy_pass_header Server; proxy_set_header Host cn.gravatar.com; proxy_set_header Accept-Encoding ”; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_pass https://gravatar; proxy_cache cache_one; proxy_cache_valid 200 304 365d; proxy_cache_key $host$uri$is_args$args; expires max; }
9、在VPS主機(jī)上,你可以看到proxy_cache生成的哈希文件,就表示緩存已經(jīng)成功了。
三、利用Nginx的proxy_store搭建鏡像服務(wù)器:修改Nginx配置方法
1、Nginx的proxy_store作用是直接把靜態(tài)文件在本地硬盤創(chuàng)建并讀取,類似于七?;蛘哂峙倪@樣的鏡像CDN功能,首次訪問會自動獲取源站的靜態(tài)圖片等文件,之后的訪問就是直接從CDN服務(wù)器讀取,加快了速度。
2、直接修改Nginx的虛擬主機(jī)配置文件(這里以img.freehao123.com.conf為演示),加入以下代碼:
location / { expires 3d; proxy_set_header Accept-Encoding ”; root /data/wwwroot/img.freehao123.com; proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /data/wwwroot/img.freehao123.com/temp; if ( !-e $request_filename) { proxy_pass https://www.freehao123.com; } }
3、再次保存配置上傳,然后重啟Nginx。你可以看到img.freehao123.com請求的圖片等靜態(tài)文件已經(jīng)成功從源站中獲得到了。
4、在VPS主機(jī)上的存目錄中也可以看到proxy_store已經(jīng)完整地將圖片等靜態(tài)文件的目錄都保存下來了,相當(dāng)于一個網(wǎng)站的鏡像存儲CDN了。
5、這里還有一個使用,效果和上面是一樣的,記得替換好路徑,代碼如下:
upstream http_tornado { server www.freehao123.com:443; } server { # 省略其他配置 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|html|htm|css)$ { root /opt/data/product/blog/cache; proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /opt/data/product/blog/cache; if ( !-e $request_filename) { proxy_pass http://http_tornado; } } }
四、Nginx的proxy_store和proxy_cache有什么區(qū)別?
1、鏡像與緩存的區(qū)別。從上面的介紹我們也可以看出來,proxy_store相當(dāng)于鏡像一個網(wǎng)站了,第二次訪問圖片等靜態(tài)文件是直接讀取CDN服務(wù)器上的,大大減輕了源站的負(fù)擔(dān)。proxy_cache相當(dāng)于緩存,即把請求生成Key,第二次訪問就可以加快速度了。
2、proxy_store適合靜態(tài),proxy_cache適合動態(tài)。proxy_store是將圖片完整保存在CDN服務(wù)器上,所以它更適合于圖片CDN加速,而proxy_cache是緩存生成Key,更加適合動態(tài)網(wǎng)站加速,可用于負(fù)載均衡,減輕服務(wù)器負(fù)擔(dān)。
五、搭建鏡像CDN服務(wù)器后要做的事情?
1、第一,因為搭建鏡像CDN服務(wù)器是完整地復(fù)制了源站的文件和URL,所以為了避免被搜索引擎誤認(rèn)為抄襲重復(fù)站,我們可以給CDN站加上Robots.txt,阻止搜索引擎收錄。命令如下(允許收錄圖片,其它不允許爬?。?/p>
User-agent: Baiduspider Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: 360Spider Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: Baiduspider-image Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: 360Spider-Image Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: Sosospider Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: sogou spider Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: YodaoBot Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: Googlebot Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: Bingbot Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: Slurp Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: MSNBot Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: googlebot-image Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: googlebot-mobile Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: yahoo-blogs/v3.9 Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: psbot Allow: /wp-content/uploads/*.jpg$ Allow: /wp-content/uploads/*.png$ Allow: /wp-content/uploads/*.gif$ Disallow: / User-agent: * Disallow: /
2、第二,做好Nginx防盜鏈。如果你的CDN服務(wù)器流量不怎么夠的話,建議還是做好防盜鏈措施,同時還可以幫你減輕服務(wù)器負(fù)擔(dān)。在你的虛擬主機(jī)配置文件中加入以下代碼:
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { valid_referers none blocked freehao123.com *.freehao123.com *.google.cn *.google.com *.google.com.hk image.baidu.com *.baidu.com; if ($invalid_referer) { rewrite ^/ https://www.freehao123.com; #return 403; } }
3、第三,設(shè)置好Nginx默認(rèn)圖片。這個主要是針對緩存Gravatar頭像的,當(dāng)源站服務(wù)器不存在某一個圖片或者文件時,我們可以給Nginx設(shè)置一個默認(rèn)的圖片或者鏈接,這樣緩存看起來就完美了。
location /avatar { try_files $uri /avatar/set-avatar.png; } #或者使用: location /{ try_files $uri /set-avatar.png; }
4、效果見下圖:
到此這篇關(guān)于詳解用Nginx搭建CDN服務(wù)器方法(圖文)的文章就介紹到這了,更多相關(guān)Nginx搭建CDN服務(wù)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:渭南 常州 96 克拉瑪依 鹽城 日照 棗莊 東莞
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解用Nginx搭建CDN服務(wù)器方法(圖文)》,本文關(guān)鍵詞 詳解,用,Nginx,搭建,CDN,服務(wù)器,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。