github仓库
https://github.com/woodchen-ink/webp_server_go
主要修改点
非文件图片支持302重定向到源资源.
举例
远程资源情况下
源资源: https://cf-r2.czl.net/status.html
webp-server-go站点: https://webp.czl.net/r2/status.html
访问webp链接会重定向到源资源
本地资源情况下
源资源: 本地映射到容器的/v
目录
webp-server-go链接: https://webp.czl.net/v/status.html
访问webp链接, 会直接返回资源
部署方法
docker compose
services:
webp:
container_name: webp-server-go
image: woodchen/webp-server-go
ports:
- 3333:3333
restart: always
volumes:
- ./config.json:/etc/config.json
- ./exhaust:/opt/exhaust
- ./metadata:/opt/metadata
- /opt/1panel/apps/openresty/openresty/www/sites/xxx.net/index:/opt/v
可选环境变量
environment:
- MALLOC_ARENA_MAX=1
示例config.json
{
"HOST": "0.0.0.0",
"PORT": "3333",
"QUALITY": "100",
"EXHAUST_PATH": "./exhaust",
"IMG_MAP": {
"/oracle": "https://xxx.ap-singapore-1.xxxer-xxx.com/n/axxxxg/b/bucket-202xxx41/o",
"/b2": "https://cf-xxx.xxx.net",
"/r2": "https://cf-yyy.yyy.net",
"/v": "./v"
},
"ALLOWED_TYPES": ["*"],
"CONVERT_TYPES": ["webp"],
"STRIP_METADATA": true,
"ENABLE_EXTRA_PARAMS": true,
"EXTRA_PARAMS_CROP_INTERESTING": "InterestingAttention",
"READ_BUFFER_SIZE": 4096,
"CONCURRENCY": 262144,
"DISABLE_KEEPALIVE": false,
"CACHE_TTL": 259200,
"MAX_CACHE_SIZE": 20480
}
具体配置参数含义和可配置值可以参考: https://docs.webp.sh/usage/configuration/#full-configuration
使用nginx反代
可以单独反代一下favicon.ico
location = /favicon.ico { #这里是你需要直接访问的链接后缀
alias /www/sites/webp.czl.net/index/favicon.ico;#这里是你文件的详细目录
}
location ^~ / {
proxy_pass http://localhost:3333;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
add_header X-Cache $upstream_cache_status;
add_header Cache-Control no-cache;
proxy_ssl_server_name off;
add_header Strict-Transport-Security "max-age=31536000";
}
使用即可.
注意的点
- 如果转换后的webp文件比原图还要大, 那么会使用原图. 主要是看返回标头是否有
x-compression-rate
, 有的话就说明已经经过webp-server-go
的处理了 - 源站最好支持下
etag
标签 - 在高并发状况下, 可以考虑使用上面的可选环境变量来优化下内存占用
- 如果每天访问量有20w以上, 可以考虑自行优化下, go的劣势在于内存回收时会阻塞, 有能力的话使用rust重构会更好.
- 使用CDN的时候会影响客户端判断, 但是如果不用CDN缓存, 又不太合适. 我也没有一个很好的想法.
评论区