摘要:以前做網(wǎng)站的時(shí)候遇到了網(wǎng)站的訪(fǎng)問(wèn)量很大,而導(dǎo)致后端處理程序響應(yīng)超時(shí)而導(dǎo)致的一些問(wèn)題。下面我想介紹一下的使用技巧準(zhǔn)備嚴(yán)格來(lái)說(shuō)是可以當(dāng)作一個(gè)代理服務(wù)器的軟件,直接將請(qǐng)求轉(zhuǎn)發(fā)到然后交給處理會(huì)獲取經(jīng)過(guò)處理后的數(shù)據(jù)最后返回給瀏覽器。
準(zhǔn)備以前做網(wǎng)站的時(shí)候遇到了網(wǎng)站的訪(fǎng)問(wèn)量很大,而導(dǎo)致后端處理程序響應(yīng)超時(shí)而導(dǎo)致的一些問(wèn)題。當(dāng)時(shí)采用的架構(gòu)是nginx+php-fastcgi,同事想到了用nginx-proxycache來(lái)做頁(yè)面緩存,效果也還行。下面我想介紹一下varnish的使用技巧
varnish嚴(yán)格來(lái)說(shuō)是可以當(dāng)作一個(gè)代理服務(wù)器的軟件,直接將HTTP請(qǐng)求轉(zhuǎn)發(fā)到php-cgi,然后交給php處理,varnish會(huì)獲取經(jīng)過(guò)php處理后的數(shù)據(jù),最后返回給瀏覽器。如圖
但是,現(xiàn)在php-fastcgi已經(jīng)被逐漸淘汰了,也就是說(shuō)我們一般情況下不會(huì)使用php-fastcgi,那么我們不能直接將varnish與php組合,因?yàn)閜hp-fpm的交互方式為socket,而不再是監(jiān)聽(tīng)本機(jī)的9000端口
所以我們必須找一個(gè)的媒介,連接varnish和php-fpm,nginx可以扮演這個(gè)媒介,如下圖:
那么問(wèn)題來(lái)了,根據(jù)研究發(fā)現(xiàn),varnish處理http請(qǐng)求不如nginx那么高效。所以如果我們讓nginx做前鋒,這樣就更完美了。那我們需要怎么才能達(dá)到這個(gè)目的呢,下面我們來(lái)整理一下流程
下面就來(lái)實(shí)現(xiàn)一下圖三的架構(gòu)吧。
事先需要準(zhǔn)備nginx,varnish,php-fpm,php這些軟件,OS是ubuntu,所有軟件都可以用apt-get install來(lái)安裝,不了解包名全稱(chēng)的話(huà)可以先apt-get update,更新一下源,然后再用apt-cache search xxx來(lái)查找軟件包名
安裝完varnish后,可以使用service varnish回車(chē),查看可操作選項(xiàng)* Usage: /etc/init.d/varnish {start|stop|restart|reload|force-reload|configtest},一般安裝完畢后,系統(tǒng)會(huì)自動(dòng)啟動(dòng)varnish的,nginx也是一樣,便不贅述了
配置安裝完所需的軟件后,下面需要配置這些軟件,來(lái)實(shí)現(xiàn)這個(gè)架構(gòu)
nginx部分vi /etc/nginx/nginx.conf
http { ## proxy global setting 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; ##END ## cache proxy pass upstream cache { server 127.0.0.1:6081; } ##END ## php proxy pass upstream php { server 127.0.0.1:8080; } ##END # Basic Settings sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; server_tokens off; #depend on nginx-extras 需要安裝nginx-extras才能定義Server more_set_headers "Server: Bird-shark"; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
vi /etc/nginx/sites-available/default
server { listen 80 default_server; listen [::]:80 default_server; index index.html index.htm index.php; server_name localhost; location ~ .*.(gif|jpg|png|css|js|flv|ico|swf|html)$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://cache; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location / { proxy_pass http://php; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass_header Server; } } server { listen 8080; root /var/www/html; index index.html index.htm index.php; location / { if (!-e $request_filename){ rewrite ^(.*)$ /index.php?s=$1 last; break; } try_files $uri $uri/ =404; } location ~ ^(.+.php)(.*)$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_intercept_errors on; fastcgi_buffers 8 128k; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }varnish部分
vi /etc/varnish/default.vcl
vcl 4.0; # Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; } acl purgers { "localhost"; #"103.22.188.169"; } sub vcl_recv { # Happens before we check if we have this in cache already. # # Typically you clean up the request here, removing cookies you don"t need, # rewriting the request, etc. if (req.restarts == 0) { unset req.http.X-Purger; } if(req.method == "PURGE"){ if(!client.ip ~ purgers){ return(synth(405,"Not Allowed.")); } return (purge); #ban("obj.http.x-url ~ " + req.url); } if(req.method == "GET" && req.url ~ ".(js|css|jpg|png|gif|swf|jpeg|ico)$"){ unset req.http.cookie; } } sub vcl_backend_response { #set beresp.http.x-url = bereq.url; if (bereq.url ~ ".(js|css|jpg|png|gif|swf|jpeg|ico)$") { unset beresp.http.Cache-Control; unset beresp.http.set-cookie; set beresp.ttl = 10h; set beresp.http.Cache-Control = "max-age=36000"; set beresp.do_gzip = true; } if(bereq.url ~ ".html$"){ set beresp.ttl = 10m; set beresp.do_gzip = true; unset beresp.http.Cache-Control; unset beresp.http.Pragma; set beresp.http.Cache-Control = "max-age=600"; unset beresp.http.Expires; } } sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT from " + req.http.host; #set resp.http.X-Cache-Hits = obj.hits; } else { set resp.http.X-Cache = "MISS from " + req.http.host; } if (req.http.X-Purger) { set resp.http.X-Purger = req.http.X-Purger; } unset resp.http.X-Powered-By; unset resp.http.Server; unset resp.http.Via; unset resp.http.X-Varnish; unset resp.http.Age; #unset resp.http.x-url; # Optional } sub vcl_hit { if (req.method == "PURGE") { return (synth(200,"Purged.")); } } sub vcl_miss { if (req.method == "PURGE") { return (synth(404,"Purged.")); } } sub vcl_purge { if (req.method == "PURGE") { #set req.http.X-Purge = "Purged"; ban("req.url ~ "+req.url); #return (restart); set req.method = "GET"; set req.http.X-Purger = "Purged"; return (restart); } } sub vcl_hash { hash_data(req.url); if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); } if (req.http.cookie) { hash_data(req.http.cookie); } if (req.http.Accept-Encoding ~ "gzip") { hash_data("gzip"); } elseif (req.http.Accept-Encoding ~ "deflate") { hash_data("deflate"); } }測(cè)試&分析 1. 在不使用緩存模塊的情況下
vi /etc/nginx/sites-available/default
#location ~ .*.(gif|jpg|png|css|js|flv|ico|swf|html)$ { # proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # proxy_pass http://cache; #}
先用chrome瀏覽器訪(fǎng)問(wèn)查看請(qǐng)求頭
我們?cè)偈褂胏url,在服務(wù)器上執(zhí)行以下命令
curl -k -v "http://192.168.99.1/Public/Home/images/t_navigation_logo.png" -H "Pragma: no-cache" -H "Accept-Encoding: gzip, deflate, sdch" -H "Accept-Language: zh,en;q=0.8,zh-CN;q=0.6" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Connection: keep-alive" --compressed
發(fā)現(xiàn)有輸出內(nèi)容。
然后,反選disable cache
然后在服務(wù)器上執(zhí)行以下命令
curl -k -v "http://192.168.99.1/Public/Home/images/t_navigation_logo.png" -H "If-None-Match: "57c6b733-1962"" -H "Accept-Encoding: gzip, deflate, sdch" -H "Accept-Language: zh,en;q=0.8,zh-CN;q=0.6" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Connection: keep-alive" -H "If-Modified-Since: Wed, 31 Aug 2016 10:53:39 GMT" --compressed
發(fā)現(xiàn)只返回了頭部信息,然而沒(méi)有內(nèi)容返回
然后我們比較兩個(gè)命令 發(fā)現(xiàn)區(qū)別就在-H "Pragma: no-cache"和 -H "If-Modified-Since: Wed, 31 Aug 2016 10:53:39 GMT" -H "If-None-Match: "57c6b733-1962""
57c6b733-1962這串字符對(duì)應(yīng)的是服務(wù)器響應(yīng)給瀏覽器的ETag部分的內(nèi)容,然后我們修改一下部分的內(nèi)容
curl -k -v "http://192.168.99.1/Public/Home/images/t_navigation_logo.png" -H "If-None-Match: "57c6b733-1234"" -H "Accept-Encoding: gzip, deflate, sdch" -H "Accept-Language: zh,en;q=0.8,zh-CN;q=0.6" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Connection: keep-alive" -H "If-Modified-Since: Wed, 31 Aug 2016 10:53:39 GMT" --compressed
在服務(wù)器端執(zhí)行一下。發(fā)現(xiàn)有內(nèi)容返回,所以這個(gè)ETag相當(dāng)于token,它不是由nginx隨便生成的,且跟請(qǐng)求鏈接應(yīng)是一一對(duì)應(yīng)的,用來(lái)標(biāo)識(shí)緩存的,當(dāng)服務(wù)器返回的狀態(tài)為304的時(shí)候,這時(shí)候我們?yōu)g覽器會(huì)直接找到本地的緩存數(shù)據(jù)
2. 在使用緩存模塊的情況下vi /etc/nginx/sites-available/default
location ~ .*.(gif|jpg|png|css|js|flv|ico|swf|html)$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://cache; }
用瀏覽器查看響應(yīng)頭
發(fā)現(xiàn)X-Cache:MISS from 192.168.99.1.這表示緩存未命中,然后我們刷新X-Cache:HIT from 192.168.99.1,這時(shí)候發(fā)現(xiàn)已經(jīng)命中了。
對(duì)于已經(jīng)命中的資源文件,我們?nèi)绻麑⑵鋭h除會(huì)出現(xiàn)什么效果呢,答案是,其依然可以訪(fǎng)問(wèn),除非重啟或者將緩存清除
但是對(duì)PURGE顯然是不對(duì)外公開(kāi)的,以下是服務(wù)器端用curl清除varnish緩存的命令
curl -v -k -X PURGE http://localhost/Public/Home/css/t_navigation.css結(jié)語(yǔ)
varnish是一款內(nèi)存類(lèi)型的緩存軟件,而非nginx擴(kuò)展proxy_cache那種物理緩存類(lèi)型的軟件,存取速度比較快,但是也有弊端,重啟后所有緩存得重寫(xiě)。不管怎么說(shuō),什么架子都適用的場(chǎng)景,要想滿(mǎn)足業(yè)務(wù)需求還是得搗鼓透徹,而我也只是將我想到的給實(shí)現(xiàn)出來(lái),畢竟資源和精力都是有限的,也就隨便玩玩,諸位看客看看就好,別太認(rèn)真,知道怎么回事兒就行。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/61807.html
摘要:以前做網(wǎng)站的時(shí)候遇到了網(wǎng)站的訪(fǎng)問(wèn)量很大,而導(dǎo)致后端處理程序響應(yīng)超時(shí)而導(dǎo)致的一些問(wèn)題。下面我想介紹一下的使用技巧準(zhǔn)備嚴(yán)格來(lái)說(shuō)是可以當(dāng)作一個(gè)代理服務(wù)器的軟件,直接將請(qǐng)求轉(zhuǎn)發(fā)到然后交給處理會(huì)獲取經(jīng)過(guò)處理后的數(shù)據(jù)最后返回給瀏覽器。 以前做網(wǎng)站的時(shí)候遇到了網(wǎng)站的訪(fǎng)問(wèn)量很大,而導(dǎo)致后端處理程序響應(yīng)超時(shí)而導(dǎo)致的一些問(wèn)題。當(dāng)時(shí)采用的架構(gòu)是nginx+php-fastcgi,同事想到了用nginx-pr...
摘要:我覺(jué)得結(jié)合的反向代理,緩存和圖像過(guò)濾處理三大模塊來(lái)為我托管在上的圖片來(lái)創(chuàng)建一個(gè)縮略圖服務(wù)器會(huì)很優(yōu)雅。后者將作為的反向代理,產(chǎn)生并提供調(diào)整大小后的圖像。它返回有效的響應(yīng),緩存服務(wù)器將緩存該響應(yīng)天,其它任何東西都只緩存秒。 一兩個(gè)月前,我決定從我的站點(diǎn)中移除Varnish ,并用Nginx內(nèi)置的緩存系統(tǒng)替代它。我本來(lái)已經(jīng)在我的python站點(diǎn)上用了nginx來(lái)反向代理,所以擺脫Varnis...
摘要:我覺(jué)得結(jié)合的反向代理,緩存和圖像過(guò)濾處理三大模塊來(lái)為我托管在上的圖片來(lái)創(chuàng)建一個(gè)縮略圖服務(wù)器會(huì)很優(yōu)雅。后者將作為的反向代理,產(chǎn)生并提供調(diào)整大小后的圖像。它返回有效的響應(yīng),緩存服務(wù)器將緩存該響應(yīng)天,其它任何東西都只緩存秒。 一兩個(gè)月前,我決定從我的站點(diǎn)中移除Varnish ,并用Nginx內(nèi)置的緩存系統(tǒng)替代它。我本來(lái)已經(jīng)在我的python站點(diǎn)上用了nginx來(lái)反向代理,所以擺脫Varnis...
摘要:轉(zhuǎn)發(fā)通過(guò),將請(qǐng)求通過(guò)負(fù)載均衡,均衡給后端處理的服務(wù)。圖床同時(shí)也支持同步上傳回調(diào)通知的方式,將圖片上傳結(jié)果反饋給業(yè)務(wù)方。 Hulk 圖床是支持 360 公司絕大部分業(yè)務(wù)的圖片服務(wù),支持多種圖片處理功能,如:裁剪、壓縮、濾鏡、pHash 計(jì)算、人臉識(shí)別、格式轉(zhuǎn)換、gif 首幀提取……等等,支持的業(yè)務(wù)線(xiàn)包括:搜索、圖搜、新聞、信息流、廣告……等等,每天 CDN 回源圖床后端 150+ 億 P...
閱讀 2329·2021-08-26 14:14
閱讀 2689·2019-08-29 13:07
閱讀 2095·2019-08-26 11:44
閱讀 686·2019-08-26 10:11
閱讀 2423·2019-08-23 15:43
閱讀 3087·2019-08-23 14:17
閱讀 398·2019-08-23 12:36
閱讀 2102·2019-08-22 15:20