在Debian上啟用壓縮緩存可以顯著提高文件傳輸和網絡訪問的速度。以下是一些常見的方法來啟用壓縮緩存:
squid
代理服務器squid
是一個高性能的緩存代理服務器,支持壓縮緩存。
squid
sudo apt update
sudo apt install squid
squid
編輯 /etc/squid/squid.conf
文件,添加或修改以下配置:
http_access allow all
acl localnet src 192.168.1.0/24 # 根據你的網絡配置修改
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
http_port 3128 accel defaultsite=my_cache_host
cache_mem 64 MB
maximum_object_size 4 MB
refresh_pattern . 1440 90% 10080 override-expire ignore-reload ignore-no-cache ignore-private
squid
sudo systemctl start squid
sudo systemctl enable squid
nginx
作為反向代理和緩存服務器nginx
也可以配置為緩存服務器,并支持壓縮。
nginx
sudo apt update
sudo apt install nginx
nginx
編輯 /etc/nginx/nginx.conf
或在 /etc/nginx/conf.d/
目錄下創建一個新的配置文件,例如 default.conf
:
http {
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_server;
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
add_header X-Proxy-Cache $upstream_cache_status;
}
}
}
nginx
sudo systemctl start nginx
sudo systemctl enable nginx
varnish
緩存服務器varnish
是一個高性能的 HTTP 加速器,也支持壓縮。
varnish
sudo apt update
sudo apt install varnish
varnish
編輯 /etc/varnish/default.vcl
文件,添加或修改以下配置:
vcl 4.0;
backend default {
.host = "127.0.0.1";
.port = "80";
}
sub vcl_recv {
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}
}
sub vcl_backend_response {
if (bereq.http.Accept-Encoding) {
if (beresp.http.content-encoding ~ "gzip") {
set beresp.do_gzip = true;
} elsif (beresp.http.content-encoding ~ "deflate") {
set beresp.do_gzip = true;
}
}
}
varnish
sudo systemctl start varnish
sudo systemctl enable varnish
以上方法都可以在Debian上啟用壓縮緩存,具體選擇哪種方法取決于你的需求和環境。squid
適用于傳統的代理服務器場景,nginx
適用于需要同時處理靜態和動態內容的場景,而 varnish
則適用于高性能的HTTP加速場景。