Trong bài này, xTraffic.pep.vn sẽ cùng các bạn tìm hiểu cách cài đặt Varnish Cache trên Linux CentOS.
Varnish Cache là gì?
Varnish Cache là một phần mềm tăng tốc website (web application accelerator) và thường được biết đến như là caching HTTP reverse proxy. Varnish Cache sẽ tăng tốc website của bạn lên nhiều lần bằng cách tạo những dữ liệu cache của trang web rồi trả về cho người dùng mà không cần phải thông qua php xử lý. Với kỹ thuật này, Varnish Cache có thể giúp tăng tốc website của bạn lên từ nhiều lần so với trước kia, nhất là những trang web cần xử lý PHP nhiều.
Cách cài đặt Varnish Cache trên CentOS
Để thực hiện cài đặt Varnish Cache phiên bản 4.0 (mới nhất tại thời điểm viết bài) trên CentOS thì bạn thực hiện lần lượt các lệnh sau :
yum install -y epel-release rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm yum install -y varnish sudo chkconfig varnish on
Khởi động Varnish bằng lệnh :
service varnish start
File cấu hình của là “/etc/sysconfig/varnish” & “/etc/varnish/default.vcl”
Cách cấu hình Varnish Cache với Nginx
Do mục tiêu của bài này là hướng dẫn cách sử dụng cơ bản Varnish Cache với Nginx, nên bạn nào muốn sử dụng Varnish Cache với WordPress thì xTraffic.pep.vn xin hẹn với bạn trong một bài viết chi tiết khác.
Cài đặt Nginx trên CentOS
yum install -y nginx
Chúng ta sẽ cấu hình để Varnish Cache chạy trên port 80, còn Nginx sẽ chạy trên port 81, như vậy Varnish Cache sẽ xử lý các truy vấn đến từ browser, sau đó chuyển tiếp cho Nginx để xử lý.
Để cấu hình Nginx chạy trên Port 81, bạn mở file “/etc/nginx/conf.d/default.conf” và sửa dòng sau :
“listen 80 default_server;” => “listen 81 default_server;”
Lưu lại và khởi động lại Nginx
service nginx restart
Mở trình duyệt và truy vấn vào url “http://your-ip-address:81/” để kiểm tra.
Để đơn giản trong ví dụ này thì mình sẽ tắt iptables bằng lệnh :
service iptables stop
Cấu hình Varnish Cache chạy trên port 80
Bạn mở file “/etc/sysconfig/varnish” tìm dòng “VARNISH_LISTEN_PORT=6081” và sửa thành “VARNISH_LISTEN_PORT=80“.
Tiếp theo bạn mở file “/etc/varnish/default.vcl” tìm và chỉnh sửa dòng .port = “8080”; thành .port = “81”; (81 ở đây là port 81 của Nginx
Hoặc bạn có thể sử dụng cấu hình đã tối ưu Varnish Cache cơ bản của xTraffic.pep.vn :
# # This is an basic VCL config file for Varnish by http://blog-xtraffic.pep.vn/ . # # It does not do anything by default, delegating control to the # builtin VCL. The builtin VCL is called when there is no explicit # return statement. # # See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/ # and http://varnish-cache.org/trac/wiki/VCLExamples for more examples. # Marker to tell the VCL compiler that this VCL has been adapted to the # new 4.0 format. vcl 4.0; # Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "81"; # Nginx is running port 81 } acl purge { "localhost"; "127.0.0.1"; "192.168.55.0"/24; } 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. # Allow PURGE from localhost + 127.0.0.1 and 192.168.55... if (req.method == "PURGE") { if (!client.ip ~ purge) { return(synth(405,"Not allowed.")); } return (purge); } if (req.http.X-Forwarded-Proto == "https" ) { set req.http.X-Forwarded-Port = "443"; } else { set req.http.X-Forwarded-Port = "80"; } } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. # Allow the backend to serve up stale content if it is responding slowly. set beresp.grace = 6h; # Enable gzip compress for text content (html,javascript,css,...) if (beresp.http.content-type ~ "text") { set beresp.do_gzip = true; } } sub vcl_deliver { # Happens when we have all the pieces we need, and are about to send the # response to the client. # # You can do accounting or modifying the final object here. }
Những cách cấu hình nâng cao hơn của Varnish Cache xin hẹn với bạn một bài viết chuyên sâu khác.
Lưu lại và khởi động lại Varnish Cache
service varnish restart
Cài đặt Apache Benchmark (ab)
Apache Benchmark (ab) là một công cụ kiểm tra khá năng chịu tải của Server rất phổ biến, do đó xTraffic.pep.vn sẽ sử dụng công cụ này để so sánh hiệu quả của Varnish Cache với Nginx
Bạn cài đặt Apache Benchmark (ab) bằng lệnh sau :
sudo yum install -y httpd-tools
Kiểm tra khả năng chịu tải của Nginx (file tĩnh index.html)
Trong lần benchmark này, xTraffic.pep.vn đã kích hoạt keepalive và gzip và tối ưu một số thứ cho Nginx.
ab -c 1000 -k -r -t 60 -n 2000000 http://127.0.0.1:81/index.html
Kết quả sau khi benchmark Nginx (index.html) là :
[root@vultr ~]# ab -c 1000 -k -r -t 60 -n 2000000 http://127.0.0.1:81/index.html This is ApacheBench, Version 2.3 < $Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Completed 200000 requests Completed 400000 requests Completed 600000 requests Completed 800000 requests Finished 855322 requests Server Software: nginx/1.0.15 Server Hostname: 127.0.0.1 Server Port: 81 Document Path: /index.html Document Length: 3698 bytes Concurrency Level: 1000 Time taken for tests: 60.000 seconds Complete requests: 855322 Failed requests: 0 Write errors: 0 Keep-Alive requests: 855322 Total transferred: 3368262380 bytes HTML transferred: 3162984620 bytes Requests per second: 14255.33 [#/sec] (mean) Time per request: 70.149 [ms] (mean) Time per request: 0.070 [ms] (mean, across all concurrent requests) Transfer rate: 54821.82 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 17.1 0 1021 Processing: 1 70 161.9 23 14386 Waiting: 1 69 158.2 23 14386 Total: 1 70 163.2 23 14386 Percentage of the requests served within a certain time (ms) 50% 23 66% 27 75% 30 80% 32 90% 245 95% 252 98% 683 99% 696 100% 14386 (longest request) [root@vultr ~]#
Kiểm tra khả năng chịu tải của Varnish Cache + Nginx (file tĩnh index.html)
ab -c 1000 -k -r -t 60 -n 2000000 http://127.0.0.1:80/index.html
Kết quả kiểm tra Varnish Cache (index.html) là :
[root@vultr ~]# ab -c 1000 -k -r -t 60 -n 2000000 http://127.0.0.1:80/index.html This is ApacheBench, Version 2.3 < $Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Completed 200000 requests Finished 305337 requests Server Software: nginx/1.0.15 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /index.html Document Length: 3698 bytes Concurrency Level: 1000 Time taken for tests: 60.001 seconds Complete requests: 305337 Failed requests: 264 (Connect: 0, Receive: 0, Length: 264, Exceptions: 0) Write errors: 0 Keep-Alive requests: 0 Total transferred: 1210719831 bytes HTML transferred: 1128159954 bytes Requests per second: 5088.84 [#/sec] (mean) Time per request: 196.509 [ms] (mean) Time per request: 0.197 [ms] (mean, across all concurrent requests) Transfer rate: 19705.29 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 171 495.1 16 15009 Processing: 3 23 40.8 19 3155 Waiting: 0 23 39.7 19 3155 Total: 4 194 497.6 35 15032 Percentage of the requests served within a certain time (ms) 50% 35 66% 39 75% 42 80% 45 90% 1032 95% 1040 98% 1049 99% 3031 100% 15032 (longest request) [root@vultr ~]#
Tiếp theo chúng ta sẽ cài đặt PHP-FPM để test hiệu quả cache khi chạy PHP
sudo yum install -y php-fpm sudo yum install -y php-pecl-apc service php-fpm start
Bạn mở file “/etc/nginx/conf.d/default.conf” và thêm vào các dòng sau vào cuối block server {…}
location ~ \.php$ { root /usr/share/nginx/html; fastcgi_intercept_errors on; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
Khởi động lại Nginx
service nginx restart
Tạo file “/usr/share/nginx/html/phpinfo.php” có nội dung sau :
<?php phpinfo();
Kiểm tra khả năng chịu tải của Nginx + PHP-FPM (file phpinfo.php)
ab -c 1000 -k -r -t 60 -n 2000000 http://127.0.0.1:81/phpinfo.php
Kết quả sau khi kiểm tra là :
[root@vultr ~]# ab -c 1000 -k -r -t 60 -n 2000000 http://127.0.0.1:81/phpinfo.php This is ApacheBench, Version 2.3 < $Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Finished 45553 requests Server Software: nginx/1.0.15 Server Hostname: 127.0.0.1 Server Port: 81 Document Path: /phpinfo.php Document Length: 44127 bytes Concurrency Level: 1000 Time taken for tests: 60.000 seconds Complete requests: 45553 Failed requests: 804 (Connect: 0, Receive: 0, Length: 804, Exceptions: 0) Write errors: 0 Non-2xx responses: 804 Keep-Alive requests: 804 Total transferred: 1985587529 bytes HTML transferred: 1977850787 bytes Requests per second: 759.22 [#/sec] (mean) Time per request: 1317.149 [ms] (mean) Time per request: 1.317 [ms] (mean, across all concurrent requests) Transfer rate: 32317.46 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 10.8 0 1008 Processing: 81 1140 3272.4 189 48552 Waiting: 62 1130 3272.9 177 48552 Total: 107 1141 3272.8 190 48554 Percentage of the requests served within a certain time (ms) 50% 190 66% 238 75% 1149 80% 1173 90% 2365 95% 5095 98% 10531 99% 16013 100% 48554 (longest request) [root@vultr ~]#
Kiểm tra khả năng chịu tải của Varnish Cache + Nginx + PHP-FPM (file phpinfo.php)
ab -c 1000 -k -r -t 60 -n 2000000 http://127.0.0.1:80/phpinfo.php
Kết quả sau khi benchmark là :
[root@vultr ~]# ab -c 1000 -k -r -t 60 -n 2000000 http://127.0.0.1:80/phpinfo.php This is ApacheBench, Version 2.3 < $Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Finished 85036 requests Server Software: nginx/1.0.15 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /phpinfo.php Document Length: 44749 bytes Concurrency Level: 1000 Time taken for tests: 60.000 seconds Complete requests: 85036 Failed requests: 36265 (Connect: 0, Receive: 55, Length: 36155, Exceptions: 55) Write errors: 0 Keep-Alive requests: 0 Total transferred: 3816758768 bytes HTML transferred: 3795313598 bytes Requests per second: 1417.27 [#/sec] (mean) Time per request: 705.584 [ms] (mean) Time per request: 0.706 [ms] (mean, across all concurrent requests) Transfer rate: 62121.70 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 1 159 526.4 45 31024 Processing: 38 525 1137.8 192 37150 Waiting: 0 185 493.4 56 14107 Total: 45 684 1260.6 248 37168 Percentage of the requests served within a certain time (ms) 50% 248 66% 314 75% 705 80% 1128 90% 1554 95% 2510 98% 4225 99% 5988 100% 37168 (longest request) [root@vultr ~]#
Đồ thị Apache Benchmark
Kết luận về hiệu quả của Varnish Cache
Sau khi benchmark thì kết quả cho thấy Nginx vượt trội so với Varnish Cache khi xử lý các truy vấn đến các file tĩnh (.html, .jpg,…). Tuy nhiên, với các truy vấn cần xử lý bằng PHP thì Varnish Cache lại tỏ ra ưu thế hơn hẳn. Trong bài này, xTraffic.pep.vn chỉ kiểm tra với xử lý PHP đơn giản là phpinfo();, trong thực tế thì các website cần phải xử lý nhiều hơn thế rất nhiều lần và Varnish Cache là một giải pháp đơn giản và cần thiết để giảm tải và tăng hiệu quả sử dụng cho Web Server của bạn.
Thông tin về Varnish Cache các bạn có thể tìm hiểu tại website chính thức tại link https://www.varnish-cache.org/