# Linux下怎么安裝HTTP加速器Varnish
## 一、Varnish簡介
### 1.1 什么是Varnish
Varnish是一款高性能的開源HTTP加速器,專為內容密集型動態網站設計。它采用現代架構設計,具有以下核心特點:
- **反向代理緩存**:作為反向代理服務器,緩存后端響應內容
- **內存級性能**:所有緩存對象存儲在內存中,響應速度可達毫秒級
- **靈活的配置語言VCL**:使用Varnish Configuration Language實現高度定制化
- **高效的緩存策略**:支持多種緩存過期和清除機制
### 1.2 Varnish與傳統方案對比
| 特性 | Varnish | Squid | Nginx緩存 |
|------------|---------|-------|-----------|
| 架構設計 | 事件驅動 | 進程驅動 | 事件驅動 |
| 緩存存儲 | 純內存 | 磁盤+內存 | 磁盤+內存 |
| 配置靈活性 | VCL語言 | 配置文件 | 配置文件 |
| 并發性能 | 極高 | 中等 | 高 |
### 1.3 典型應用場景
- 高并發新聞門戶網站
- 電商平臺商品頁面
- API網關加速
- 動態內容邊緣緩存
## 二、安裝準備
### 2.1 系統要求
- **操作系統**:主流Linux發行版(CentOS/RHEL 7+, Ubuntu 18.04+)
- **內存**:建議至少4GB(生產環境推薦16GB+)
- **CPU**:現代多核處理器
- **磁盤空間**:100MB用于程序安裝
### 2.2 環境準備
```bash
# 更新系統包(CentOS/RHEL)
sudo yum update -y
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y
# 安裝基礎工具
sudo yum install -y epel-release curl wget # CentOS
sudo apt install -y curl wget vim # Ubuntu
# 添加Varnish倉庫
sudo rpm --nosignature -i https://packagecloud.io/varnishcache/varnish70/el/7/noarch/varnish-release-7.0-1.noarch.rpm
# 安裝Varnish
sudo yum install -y varnish
# 添加GPG密鑰
curl -s https://packagecloud.io/install/repositories/varnishcache/varnish70/script.deb.sh | sudo bash
# 安裝Varnish
sudo apt install -y varnish
# 安裝依賴
sudo yum groupinstall -y "Development Tools"
sudo yum install -y python3-docutils libedit-devel
# 下載源碼
wget https://varnish-cache.org/_downloads/varnish-7.3.0.tgz
tar xvf varnish-7.3.0.tgz
cd varnish-7.3.0
# 編譯安裝
./configure
make
sudo make install
varnishd -V
# 應輸出類似信息:
# varnishd (varnish-7.3.0 revision 123456)
/etc/varnish/default.vcl - VCL配置文件/etc/varnish/varnish.params - 服務參數/lib/systemd/system/varnish.service - systemd服務文件編輯/etc/varnish/varnish.params:
# 修改監聽端口為80
VARNISH_LISTEN_PORT=80
修改/etc/varnish/default.vcl:
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8080"; # 后端服務器端口
}
sub vcl_recv {
# 緩存靜態資源
if (req.url ~ "\.(css|js|jpg|jpeg|png|gif|ico|woff|woff2)$") {
return (hash);
}
}
sub vcl_backend_response {
# 設置緩存時間
if (bereq.url ~ "\.(css|js|jpg|jpeg|png|gif|ico|woff|woff2)$") {
set beresp.ttl = 1d;
} else {
set beresp.ttl = 10m;
}
}
sudo systemctl daemon-reload
sudo systemctl enable --now varnish
# 查看狀態
sudo systemctl status varnish
# 重啟服務
sudo systemctl restart varnish
# 查看實時日志
sudo journalctl -u varnish -f
修改/etc/varnish/varnish.params:
# 根據服務器內存調整(建議不超過物理內存的80%)
VARNISH_STORAGE_SIZE=2G
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
# 生產環境建議值
VARNISH_THREAD_POOL_MAX=4000
VARNISH_THREAD_POOL_MIN=100
# 增加文件描述符限制
echo "fs.file-max = 200000" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# 調整TCP參數
echo "net.ipv4.tcp_tw_reuse = 1" | sudo tee -a /etc/sysctl.conf
echo "net.core.somaxconn = 65535" | sudo tee -a /etc/sysctl.conf
# 實時監控
varnishstat -1
# 監控特定指標
varnishstat -f MN.cache_hit,MN.cache_miss
安裝varnishlog工具:
sudo yum install -y varnishncsa # CentOS
sudo apt install -y varnishncsa # Ubuntu
常用命令:
# 實時訪問日志
varnishncsa
# 分析請求模式
varnishlog -q 'ReqURL ~ "^/products/"' -i requrl
# 清除特定URL
varnishadm "ban req.url == /product/1234"
# 清除整個域名緩存
varnishadm "ban req.http.host == example.com"
# 檢查配置語法
varnishd -C -f /etc/varnish/default.vcl
# 測試模式運行
varnishd -d -f /etc/varnish/default.vcl
varnishstat -1 | grep -E 'MN.cache_(hit|miss)'
varnishstat -1 | grep BACKEND
| 錯誤代碼 | 含義 | 解決方案 |
|---|---|---|
| 503 | 后端不可用 | 檢查后端服務狀態 |
| 500 | VCL編譯錯誤 | 使用varnishd -C檢查語法 |
| 400 | 無效請求 | 檢查客戶端請求格式 |
backend web1 {
.host = "192.168.1.10";
.port = "80";
.probe = {
.url = "/health";
.interval = 5s;
.timeout = 1s;
.window = 5;
.threshold = 3;
}
}
backend web2 {
.host = "192.168.1.11";
.port = "80";
.probe = healthcheck;
}
sub vcl_recv {
# 根據URL路徑分配后端
if (req.url ~ "^/api/") {
set req.backend_hint = web1;
} else {
set req.backend_hint = web2;
}
}
sub vcl_backend_response {
# 啟用ESI處理
if (beresp.http.content-type ~ "text/html") {
set beresp.do_esi = true;
}
}
acl purge {
"localhost";
"192.168.1.0"/24;
}
sub vcl_recv {
if (req.method == "PURGE") {
if (!client.ip ~ purge) {
return(synth(405,"Method not allowed"));
}
return (purge);
}
}
sub vcl_recv {
# 強制HTTPS
if (req.http.X-Forwarded-Proto !~ "(?i)https") {
return (synth(750, ""));
}
}
sub vcl_synth {
if (resp.status == 750) {
set resp.status = 301;
set resp.http.Location = "https://" + req.http.Host + req.url;
return(deliver);
}
}
Varnish作為高性能HTTP加速器,能顯著提升網站響應速度和并發處理能力。本文詳細介紹了從安裝到配置的全過程,包括: 1. 多種安裝方法比較 2. 基礎配置模板 3. 性能優化技巧 4. 常見問題解決方案
建議生產環境部署前進行充分的壓力測試,根據實際業務特點調整緩存策略。更多高級功能可參考官方文檔。 “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。