# webp-server-go 重构二开版 支持非图片资源302重定向和直接返回和内存优化等

**URL:** <https://www.sunai.net/t/topic/134>\
**Category:** IT\
**Tags:** webp, webp-server-go\
**Created:** [2024 年9 月 29 日 08:31 UTC](https://www.sunai.net/t/topic/134 "2024-09-29T08:31:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![wood](https://www.sunai.net/user_avatar/www.sunai.net/wood/32/1032_2.png) [@wood](https://www.sunai.net/u/wood)\
发布日期： [2024 年9 月 29 日 08:31 UTC](https://www.sunai.net/t/topic/134/1 "2024-09-29T08:31:10Z")

</div>

## github仓库

[https://github.com/woodchen-ink/webp\_server\_go](https://github.com/woodchen-ink/webp_server_go)

## 主要修改点

1. 如果文件非图片类型, 302重定向到源文件
2. 完全删除ETAG相关逻辑, 处理后的文件直接按目录显式存储到exhaust目录, 在图片处理完毕5分钟后, 清理远程源图像文件
3. 低于30KB的图片不处理, 直接存到`EXHAUST_PATH`, 下次从本地存储中提供访问
4. 优化日志可读性
5. 内存优化:

- 控制预取并发
- 流式处理大文件
- 内存使用监控

1. 根目录设置打招呼(没啥用)

2. 移除`IMG_PATH`的配置方式, 只保留`IMG_MAP`.(依旧可以配置本地路径)

3. 当前缀与`IMG_MAP`不匹配时, 直接返回错误(减少处理量)

4. 处理后的文件, 直接从exhaust目录提供访问,包含以下几个处理逻辑:

5. web引擎从fiber切换到gin (2024.10.28)

经过以上修改, 本项目跟原项目的根底逻辑已经变化了. 更类似于我之前写的两个项目:

[https://github.com/woodchen-ink/MirrorFetch](https://github.com/woodchen-ink/MirrorFetch)

[https://github.com/woodchen-ink/SimpleMirrorFetch](https://github.com/woodchen-ink/SimpleMirrorFetch)

### 举例

#### 远程资源情况下

源资源: [https://cf-r2.czl.net/status.html](https://cf-r2.czl.net/status.html)  
webp-server-go站点: [https://webp.czl.net/r2/status.html](https://webp.czl.net/r2/status.html)

访问webp链接会重定向到源资源

 ![image](https://i.czl.net/r2/original/1X/8b62a2a6cfc11c20f86d5d3c9edec69b9a95c881.png)

#### 本地资源情况下

源资源: 本地映射到容器的`/v`目录  
webp-server-go链接: [https://webp.czl.net/v/status.html](https://webp.czl.net/v/status.html)

访问webp链接, 会直接返回资源

 ![image](https://i.czl.net/r2/original/1X/0b25b80d8098e2dd1a6716c86ee30fa8400f116d.png)

## 部署方法

### docker compose

```yml
services:
    webp:
        container_name: webp-server-go
        image: woodchen/webp-server-go
        ports:
            - 3333:3333
        restart: always
        environment:
          - TZ=Asia/Shanghai
        volumes:
            - ./config.json:/etc/config.json
            - ./exhaust:/opt/exhaust
            - ./metadata:/opt/metadata
            - ./remote-raw:/opt/remote-raw
            - /opt/.../www/sites/xxx.net/uploads:/opt/v
           

```

### 示例config.json

```json
{
  "HOST": "0.0.0.0",
  "PORT": "3333",
  "QUALITY": "90",
  "EXHAUST_PATH": "./exhaust",
  "MetadataPath": "./metadata",
  "RemoteRawPath": "./remote-raw",
  "IMG_MAP": {
    "/oracle": "https://xxx.ap-singapore-1.xxxer-xxx.com/n/.../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",
  "ReadBufferSize": "1024*4",
  "CONCURRENCY": 256,
  "DISABLE_KEEPALIVE": true,
  "CACHE_TTL": 1440,
  "MAX_CACHE_SIZE": 10240
}

```

具体配置参数含义和可配置值可以参考: [Configuration | WebP Server Documentation](https://docs.webp.sh/usage/configuration/#full-configuration)

## 使用nginx反代

可以单独反代一下`favicon.ico`

```nginx
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"; 
}

```

使用即可.

## 注意的点

1. 如果转换后的webp文件比原图还要大, 那么会使用原图. 主要是看返回标头是否有`x-compression-rate`, 有的话就说明已经经过`webp-server-go`的处理了.日志里也有详细显示
2. 源站最好支持下`etag`标签
3. 如果每天访问量有20w以上, 可以考虑自行优化下, go的劣势在于内存回收时会阻塞, 有能力的话使用rust重构也许会更好.
