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. Khi có request đến, Varnish sẽ gửi request đến server để xử lý, nhận lại dữ liệu, lưu lại dữ liệu, gửi dữ liệu cho người dùng. Và những lần request sau Varnish sẽ sử dụng các dữ liệu cache trước đó trả lại …
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. Khi có request đến, Varnish sẽ gửi request đến server để xử lý, nhận lại dữ liệu, lưu lại dữ liệu, gửi dữ liệu cho người dùng. Và những lần request sau Varnish sẽ sử dụng các dữ liệu cache trước đó trả lại cho người dùng luôn mà ko cần phải request về server nữa.
Để có thể sử dụng Varnish cho server của mình thì cần phải cài đặt cấu hình theo các bước cơ bản sau:
VARNISH SETUP
Step 1 – Hãy vào website của Varnish để xem version mới nhất
https://www.varnish-cache.org/releases
Click vào Instructions link để xem hướng dẫn về cài đặt
Step 2 – Add Varnish repository
- yum install epel-release
- rpm –nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.1.el7.rpm
(or: rpm –nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.1.el6.rpm for RHEL6) - yum install varnish
Notes: Khi chạy lệnh install rất có thể bạn sẽ gặp lỗi sau
Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile * base: mirror.vodien.com * epel: mirror.smartmedia.net.id * extras: mirror.vodien.com * updates: mirror.vodien.com https://repo.varnish-cache.org/redhat/varnish-4.1/el7/i386/repodata/repomd.xml: [Errno 14] PYCURL ERROR 22 - "The requested URL returned error: 404 Not Found" Trying other mirror. Error: Cannot retrieve repository metadata (repomd.xml) for repository: varnish-4.1. Please verify its path and try again
Hãy vào /etc/yum.repos.d để xóa repos bị lỗi của varnish đi và add 1 repos khác vào để cài đặt. Với trường hợp của mình là bị lỗi với version 4.1.el7 nên mình đã instal bản varnish-4.0.el6.rpm
yum install epel-release rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm yum install varnish
Và đây là kết quả khi cài đặt thành công
[[email protected] ~]# yum install varnish Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile * base: mirror.readyspace.com * epel: mirror.smartmedia.net.id * extras: mirror.readyspace.com * updates: mirror.readyspace.com vesta | 2.9 kB 00:00 vesta/primary_db | 54 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package varnish.i686 0:2.1.5-5.el6 will be installed --> Processing Dependency: varnish-libs = 2.1.5-5.el6 for package: varnish-2.1.5-5.el6.i686 --> Processing Dependency: libvcl.so.1 for package: varnish-2.1.5-5.el6.i686 --> Processing Dependency: libvarnishcompat.so.1 for package: varnish-2.1.5-5.el6.i686 --> Processing Dependency: libvarnishapi.so.1 for package: varnish-2.1.5-5.el6.i686 --> Processing Dependency: libvarnish.so.1 for package: varnish-2.1.5-5.el6.i686 --> Processing Dependency: libjemalloc.so.1 for package: varnish-2.1.5-5.el6.i686 --> Processing Dependency: jemalloc for package: varnish-2.1.5-5.el6.i686 --> Processing Dependency: gcc for package: varnish-2.1.5-5.el6.i686 --> Running transaction check ---> Package gcc.i686 0:4.4.7-16.el6 will be installed --> Processing Dependency: cpp = 4.4.7-16.el6 for package: gcc-4.4.7-16.el6.i686 --> Processing Dependency: glibc-devel >= 2.2.90-12 for package: gcc-4.4.7-16.el6.i686 --> Processing Dependency: cloog-ppl >= 0.15 for package: gcc-4.4.7-16.el6.i686 ---> Package jemalloc.i686 0:3.6.0-1.el6 will be installed ---> Package varnish-libs.i686 0:2.1.5-5.el6 will be installed --> Running transaction check ---> Package cloog-ppl.i686 0:0.15.7-1.2.el6 will be installed --> Processing Dependency: libppl_c.so.2 for package: cloog-ppl-0.15.7-1.2.el6.i686 --> Processing Dependency: libppl.so.7 for package: cloog-ppl-0.15.7-1.2.el6.i686 ---> Package cpp.i686 0:4.4.7-16.el6 will be installed --> Processing Dependency: libmpfr.so.1 for package: cpp-4.4.7-16.el6.i686 ---> Package glibc-devel.i686 0:2.12-1.166.el6_7.7 will be installed --> Processing Dependency: glibc-headers = 2.12-1.166.el6_7.7 for package: glibc-devel-2.12-1.166.el6_7.7.i686 --> Processing Dependency: glibc = 2.12-1.166.el6_7.7 for package: glibc-devel-2.12-1.166.el6_7.7.i686 --> Processing Dependency: glibc-headers for package: glibc-devel-2.12-1.166.el6_7.7.i686 --> Running transaction check ---> Package glibc.i686 0:2.12-1.166.el6_7.3 will be updated --> Processing Dependency: glibc = 2.12-1.166.el6_7.3 for package: glibc-common-2.12-1.166.el6_7.3.i686 ---> Package glibc.i686 0:2.12-1.166.el6_7.7 will be an update ---> Package glibc-headers.i686 0:2.12-1.166.el6_7.7 will be installed --> Processing Dependency: kernel-headers >= 2.2.1 for package: glibc-headers-2.12-1.166.el6_7.7.i686 --> Processing Dependency: kernel-headers for package: glibc-headers-2.12-1.166.el6_7.7.i686 ---> Package mpfr.i686 0:2.4.1-6.el6 will be installed ---> Package ppl.i686 0:0.10.2-11.el6 will be installed --> Running transaction check ---> Package glibc-common.i686 0:2.12-1.166.el6_7.3 will be updated ---> Package glibc-common.i686 0:2.12-1.166.el6_7.7 will be an update ---> Package kernel-headers.i686 0:2.6.32-573.18.1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved =============================================================================================================== Package Arch Version Repository Size =============================================================================================================== Installing: varnish i686 2.1.5-5.el6 epel 260 k Installing for dependencies: cloog-ppl i686 0.15.7-1.2.el6 base 93 k cpp i686 4.4.7-16.el6 base 3.4 M gcc i686 4.4.7-16.el6 base 8.2 M glibc-devel i686 2.12-1.166.el6_7.7 updates 987 k glibc-headers i686 2.12-1.166.el6_7.7 updates 623 k jemalloc i686 3.6.0-1.el6 epel 97 k kernel-headers i686 2.6.32-573.18.1.el6 updates 3.9 M mpfr i686 2.4.1-6.el6 base 153 k ppl i686 0.10.2-11.el6 base 1.3 M varnish-libs i686 2.1.5-5.el6 epel 88 k Updating for dependencies: glibc i686 2.12-1.166.el6_7.7 updates 4.3 M glibc-common i686 2.12-1.166.el6_7.7 updates 14 M Transaction Summary =============================================================================================================== Install 11 Package(s) Upgrade 2 Package(s) Total download size: 37 M Is this ok [y/N]: y Downloading Packages: (1/13): cloog-ppl-0.15.7-1.2.el6.i686.rpm | 93 kB 00:00 (2/13): cpp-4.4.7-16.el6.i686.rpm | 3.4 MB 00:00 (3/13): gcc-4.4.7-16.el6.i686.rpm | 8.2 MB 00:00 (4/13): glibc-2.12-1.166.el6_7.7.i686.rpm | 4.3 MB 00:00 (5/13): glibc-common-2.12-1.166.el6_7.7.i686.rpm | 14 MB 00:00 (6/13): glibc-devel-2.12-1.166.el6_7.7.i686.rpm | 987 kB 00:00 (7/13): glibc-headers-2.12-1.166.el6_7.7.i686.rpm | 623 kB 00:00 (8/13): jemalloc-3.6.0-1.el6.i686.rpm | 97 kB 00:00 (9/13): kernel-headers-2.6.32-573.18.1.el6.i686.rpm | 3.9 MB 00:00 (10/13): mpfr-2.4.1-6.el6.i686.rpm | 153 kB 00:00 (11/13): ppl-0.10.2-11.el6.i686.rpm | 1.3 MB 00:00 (12/13): varnish-2.1.5-5.el6.i686.rpm | 260 kB 00:00 (13/13): varnish-libs-2.1.5-5.el6.i686.rpm | 88 kB 00:00 --------------------------------------------------------------------------------------------------------------- Total 14 MB/s | 37 MB 00:02 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Warning: RPMDB altered outside of yum. Installing : kernel-headers-2.6.32-573.18.1.el6.i686 1/15 Updating : glibc-common-2.12-1.166.el6_7.7.i686 2/15 Updating : glibc-2.12-1.166.el6_7.7.i686 3/15 Installing : glibc-headers-2.12-1.166.el6_7.7.i686 4/15 Installing : glibc-devel-2.12-1.166.el6_7.7.i686 5/15 Installing : jemalloc-3.6.0-1.el6.i686 6/15 Installing : varnish-libs-2.1.5-5.el6.i686 7/15 Installing : mpfr-2.4.1-6.el6.i686 8/15 Installing : cpp-4.4.7-16.el6.i686 9/15 Installing : ppl-0.10.2-11.el6.i686 10/15 Installing : cloog-ppl-0.15.7-1.2.el6.i686 11/15 Installing : gcc-4.4.7-16.el6.i686 12/15 Installing : varnish-2.1.5-5.el6.i686 13/15 Cleanup : glibc-common-2.12-1.166.el6_7.3.i686 14/15 Cleanup : glibc-2.12-1.166.el6_7.3.i686 15/15 Verifying : cloog-ppl-0.15.7-1.2.el6.i686 1/15 Verifying : glibc-2.12-1.166.el6_7.7.i686 2/15 Verifying : glibc-devel-2.12-1.166.el6_7.7.i686 3/15 Verifying : cpp-4.4.7-16.el6.i686 4/15 Verifying : glibc-common-2.12-1.166.el6_7.7.i686 5/15 Verifying : jemalloc-3.6.0-1.el6.i686 6/15 Verifying : varnish-libs-2.1.5-5.el6.i686 7/15 Verifying : mpfr-2.4.1-6.el6.i686 8/15 Verifying : ppl-0.10.2-11.el6.i686 9/15 Verifying : glibc-headers-2.12-1.166.el6_7.7.i686 10/15 Verifying : gcc-4.4.7-16.el6.i686 11/15 Verifying : kernel-headers-2.6.32-573.18.1.el6.i686 12/15 Verifying : varnish-2.1.5-5.el6.i686 13/15 Verifying : glibc-2.12-1.166.el6_7.3.i686 14/15 Verifying : glibc-common-2.12-1.166.el6_7.3.i686 15/15 Installed: varnish.i686 0:2.1.5-5.el6 Dependency Installed: cloog-ppl.i686 0:0.15.7-1.2.el6 cpp.i686 0:4.4.7-16.el6 gcc.i686 0:4.4.7-16.el6 glibc-devel.i686 0:2.12-1.166.el6_7.7 glibc-headers.i686 0:2.12-1.166.el6_7.7 jemalloc.i686 0:3.6.0-1.el6 kernel-headers.i686 0:2.6.32-573.18.1.el6 mpfr.i686 0:2.4.1-6.el6 ppl.i686 0:0.10.2-11.el6 varnish-libs.i686 0:2.1.5-5.el6 Dependency Updated: glibc.i686 0:2.12-1.166.el6_7.7 glibc-common.i686 0:2.12-1.166.el6_7.7 Complete!
Vậy là OK rồi. Mọi người nhớ thiết lập chế độ tự khởi động cho nó khi reboot nhé
sudo chkconfig varnish on
Vậy là xong phần cài đặt Varnish (vẫn chưa chạy nhé)
VARNISH CONFIGURATION
Port mặc định của webserver là 80. Nên ta cần cấu hình cho Varnish sẽ nghe port này. và chuyển port của web server qua 1 port khác như là 8080. Sau khi chuyển xong thì phải cấu hình cho Varnish kết nối với web server thông qua port mới đổi (8080).
Về cơ bản thì có 3 bước là vậy, nhưng nếu server của bạn đã cài đặt soft quản lý host thì sẽ có thêm 1 vài bước để sửa cấu hình của soft nữa. Bài viết này mình sẽ hướng dẫn cấu hình với VestaCP:
Step 1 – We have to setup Varnish so it will run on port 80. The scenario will be like this: Varnish listen on port 80, Nginx on port 8082, then Apache on port 8080
Web request = Varnish (80) -> Nginx (8082) -> Apache (8080)
Trước tiên ta sẽ thay đổi cấu hình của Varnish theo đường dẫn sau: /etc/sysconfig/varnish
Hãy đổi:
VARNISH_LISTEN_PORT=6081 ————–> đổi sang port 80
VARNISH_LISTEN_ADDRESS=”địa chỉ IP public của bạn” —-> thêm dòng này vào nếu chưa có nhé
VARNISH_STORAGE_SIZE=1G ————–> chỉnh lại Ram cho Varnish, mình để 256MB
VARNISH_STORAGE ————> “malloc,${VARNISH_STORAGE_SIZE}”
Step 2 – Tiếp theo, ta sẽ thay đổi cấu hình cho file VCL của Varnish trong /etc/varnish/default
.vcl
. File VCL này sẽ cho Varnish biết được rằng sẽ phải tìm webserver ở đâu:
- Đổi .host ip từ 127.0.0.1 sang IP public của server
- Đổi .port 80 sang .port 8082 — là port mà nginx sẽ lắng nghe.
- Phần comment ở bên dưới mình chưa hiểu lắm nên vẫn để nguyên, ko thay đổi gì
Step 3 – Cấu hình cho Nginx
Trong folder này /etc/nginx/conf.d/ có chưa file cấu hình cho địa chỉ IP public của server: xxx.xxx.xxx.xxx.conf
server { listen xxx.xxx.xxx.xxx:80 default; server_name _; #access_log /var/log/nginx/xxx.xxx.xxx.xxx.log main; location / { proxy_pass http://xxx.xxx.xxx.xxx:8080; } }
Chúng ta cần sửa port :80 sang :8082
Tương tự đối với các file cấu hình cho các website của Nginx. VestaCP lưu cấu hình cho các website theo từng user, ta cần phải vào từng folder của từng user và sửa port từ :80 sang :8082
/home/user/conf/web
Step 3 – Cấu hình cho VestaCP
Hãy tìm trong file cấu hình /usr/local/vesta/conf/vesta.conf dòng khai báo PROXY_PORT và đổi từ 80 -> 8082
WEB_SYSTEM='httpd' WEB_RGROUPS='apache' WEB_PORT='8080' WEB_SSL_PORT='8443' WEB_SSL='mod_ssl' PROXY_SYSTEM='nginx' PROXY_PORT='8082' PROXY_SSL_PORT='443' STATS_SYSTEM='webalizer,awstats' FTP_SYSTEM='vsftpd' DNS_SYSTEM='named'
Vậy là xong phần cấu hình. Với VestaCP ta cần phải thêm port trong phần Firewall nữa.
Hãy login vào Vesta CP dashboard -> Firewall
Sau đó click vào /WEB và thêm port mới : 80,443 –> 80,443,8082
VestaCP đặt port cho Apache là 8080 nên ta ko cần sửa gì nữa. Hãy khởi động Varnish lên, restart lại nginx và test thử nhé. Load thử 1 trang web trên server và xem header, bạn sẽ thấy response nhận được là thông qua Varnish:
Age 52 Connection keep-alive Content-Encoding gzip Content-Length 1986 Content-Type text/html; charset=UTF-8 Date Thu, 17 Mar 2016 17:04:11 GMT Keep-Alive timeout=60 Link <http://dt-blog.local/wp-json/>; rel="https://api.w.org/" Server nginx Vary Accept-Encoding Via 1.1 varnish X-Powered-By PHP/5.4.45 X-Varnish 1068740041 1068740040
Vậy là hết rồi đó. Enjoy please ^_^!
Be First to Comment