Nginx代理varnish的那些姿勢
以前做網(wǎng)站的時候遇到了網(wǎng)站的訪問量很大,而導致后端處理程序響應超時而導致的一些問題。當時采用的架構(gòu)是nginx+php-fastcgi,同事想到了用nginx-proxycache來做頁面緩存,效果也還行。下面我想介紹一下varnish的使用技巧
準備
varnish嚴格來說是可以當作一個代理服務器的軟件,直接將HTTP請求轉(zhuǎn)發(fā)到php-cgi,然后交給php處理,varnish會獲取經(jīng)過php處理后的數(shù)據(jù),***返回給瀏覽器。如圖
但是,現(xiàn)在php-fastcgi已經(jīng)被逐漸淘汰了,也就是說我們一般情況下不會使用php-fastcgi,那么我們不能直接將varnish與php組合,因為php-fpm的交互方式為socket,而不再是監(jiān)聽本機的9000端口
所以我們必須找一個的媒介,連接varnish和php-fpm,nginx可以扮演這個媒介,如下圖:
那么問題來了,根據(jù)研究發(fā)現(xiàn),varnish處理http請求不如nginx那么高效。所以如果我們讓nginx做前鋒,這樣就更***了。那我們需要怎么才能達到這個目的呢,下面我們來整理一下流程
下面就來實現(xiàn)一下圖三的架構(gòu)吧。
事先需要準備nginx,varnish,php-fpm,php這些軟件,OS是ubuntu,所有軟件都可以用apt-get install來安裝,不了解包名全稱的話可以先apt-get update,更新一下源,然后再用apt-cache search xxx來查找軟件包名
安裝完varnish后,可以使用service varnish回車,查看可操作選項* Usage: /etc/init.d/varnish {start|stop|restart|reload|force-reload|configtest},一般安裝完畢后,系統(tǒng)會自動啟動varnish的,nginx也是一樣,便不贅述了
配置
安裝完所需的軟件后,下面需要配置這些軟件,來實現(xiàn)這個架構(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/*;
- }
varnish部分
vi /etc/varnish/default.vcl
- 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;
- }
- }
測試&分析
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瀏覽器訪問查看請求頭
我們再使用curl,在服務器上執(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
然后在服務器上執(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)只返回了頭部信息,然而沒有內(nèi)容返回
然后我們比較兩個命令 發(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這串字符對應的是服務器響應給瀏覽器的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
在服務器端執(zhí)行一下。發(fā)現(xiàn)有內(nèi)容返回,所以這個ETag相當于token,它不是由nginx隨便生成的,且跟請求鏈接應是一一對應的,用來標識緩存的,當服務器返回的狀態(tài)為304的時候,這時候我們?yōu)g覽器會直接找到本地的緩存數(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;
- }
用瀏覽器查看響應頭
發(fā)現(xiàn)X-Cache:MISS from 192.168.99.1.這表示緩存未***,然后我們刷新X-Cache:HIT from 192.168.99.1,這時候發(fā)現(xiàn)已經(jīng)***了。
對于已經(jīng)***的資源文件,我們?nèi)绻麑⑵鋭h除會出現(xiàn)什么效果呢,答案是,其依然可以訪問,除非重啟或者將緩存清除
但是對PURGE顯然是不對外公開的,以下是服務器端用curl清除varnish緩存的命令
- curl -v -k -X PURGE http://localhost/Public/Home/css/t_navigation.css
結(jié)語
varnish是一款內(nèi)存類型的緩存軟件,而非nginx擴展proxy_cache那種物理緩存類型的軟件,存取速度比較快,但是也有弊端,重啟后所有緩存得重寫。不管怎么說,什么架子都適用的場景,要想滿足業(yè)務需求還是得搗鼓透徹,而我也只是將我想到的給實現(xiàn)出來,畢竟資源和精力都是有限的,也就隨便玩玩,諸位看客看看就好,別太認真,知道怎么回事兒就行。