香港服务器 Cloudflare 优化配置指南

香港服务器 Cloudflare 优化配置指南

发布时间:2024-11-19 10:02:54

1. 网络优化配置

dns配置

yaml

# dns记录设置
类型: a记录
名称: @
内容: [香港服务器ip]
代理状态: 已代理(橙色云朵)

# 建议增加子域名记录
类型: a记录
名称: hk
内容: [香港服务器ip]
代理状态: 已代理
ttl: auto

网络优化设置

yaml

# ssl/tls设置
ssl/tls模式: full (strict)
最小tls版本: 1.2
tls 1.3: 开启
hsts: 开启
自动https重写: 开启

# speed优化
auto minify:
- html: 开启
- css: 开启
- javascript: 开启
brotli压缩: 开启
早期提示: 开启
rocket loader: 按需开启

2. 香港节点特定缓存策略

page rules配置

yaml

# 静态资源缓存(cdn主要作用)
url匹配: example.com/static/*
规则:
- cache level: cache everything
- edge cache ttl: 7 days
- browser cache ttl: 7 days
- origin cache control: 开启

# api请求优化(考虑香港到大陆延迟)
url匹配: example.com/api/*
规则:
- cache level: standard
- edge cache ttl: 30 minutes
- browser cache ttl: 5 minutes
- origin cache control: 开启

# 动态内容(适合香港服务器特点)
url匹配: example.com/dynamic/*
规则:
- cache level: standard
- edge cache ttl: 1 hour
- browser cache ttl: 30 minutes

3. 香港服务器源站配置

nginx配置

nginx

# 主配置
http {
# 基础优化
client_max_body_size 100m;
client_body_buffer_size 128k;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;

# 开启gzip
gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml;
gzip_vary on;

# 缓存相关
proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

server {
listen 80;
server_name example.com;

# 静态文件处理
location /static/ {
root /var/www/html;
expires 7d;
add_header cache-control "public, no-transform";
add_header x-server-location "hk";
}

# api处理
location /api/ {
proxy_cache my_cache;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_valid 200 30m;
proxy_cache_valid 404 1m;
add_header x-cache-status $upstream_cache_status;
add_header x-server-location "hk";
}
}
}

apache配置

apache

# 开启必要模块
loadmodule headers_module modules/mod_headers.so
loadmodule expires_module modules/mod_expires.so
loadmodule cache_module modules/mod_cache.so

# 缓存配置

expiresactive on

# 图片文件
expiresbytype image/jpg "access plus 1 year"
expiresbytype image/jpeg "access plus 1 year"
expiresbytype image/png "access plus 1 year"
expiresbytype image/gif "access plus 1 year"

# css, javascript
expiresbytype text/css "access plus 1 month"
expiresbytype application/javascript "access plus 1 month"

# 默认缓存
expiresdefault "access plus 2 days"

4. 针对中国访客的优化

workers脚本

javascript

addeventlistener('fetch', event => {
event.respondwith(handlerequest(event.request))
})

async function handlerequest(request) {
// 检测用户ip来源
const userip = request.headers.get('cf-connecting-ip')
const country = request.headers.get('cf-ipcountry')

// 中国用户特殊处理
if (country === 'cn') {
// 使用更激进的缓存策略
const cache = caches.default
let response = await cache.match(request)

if (response) {
return response
}

response = await fetch(request)
const headers = new headers(response.headers)

// 针对中国用户设置更长的缓存时间
headers.set('cache-control', 'public, max-age=7200')

response = new response(response.body, {
status: response.status,
headers
})

event.waituntil(cache.put(request, response.clone()))
return response
}

return fetch(request)
}

5. 监控和维护

性能监控

yaml

# 需要监控的指标
- 请求响应时间
- 缓存命中率
- 带宽使用情况
- 错误率
- 源站负载

# 告警设置
响应时间 > 1s: 告警
错误率 > 1%: 告警
缓存命中率 < 70%: 告警

定期维护任务

bash

# 每周缓存清理(低峰期执行)
curl -x post "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache"
-h "authorization: bearer {api_token}"
-h "content-type: application/json"
--data '{"purge_everything":true}'

# 日志分析(每日)
analysis_script.sh --region=hk --date=$(date -d "yesterday" +%y%m%d)