Wood Chen

Modified webp-server-go: 302 Redirects and Direct Serving for Non-Image Resources

0 comments111 views247 words

This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文

github Repository

https://github.com/woodchen-ink/webp_server_go

Main Changes

Non-image files support 302 redirects to the source resource.

Examples

Remote Resources

Source resource: https://cf-r2.czl.net/status.html webp-server-go site: https://webp.czl.net/r2/status.html

Visiting the webp URL redirects to the source resourceimage

Local Resources

Source resource: a local directory mapped to /v in the container webp-server-go URL: https://webp.czl.net/v/status.html

Visiting the webp URL returns the resource directlyimage

Deployment

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

Optional environment variable

environment:
      - MALLOC_ARENA_MAX=1

Example 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
}

For configuration parameter descriptions and supported values, see: https://docs.webp.sh/usage/configuration/#full-configuration

Using nginx as a Reverse Proxy

You can handle favicon.ico separately

location = /favicon.ico { #This is the URL suffix you want to access directly
    alias /www/sites/webp.czl.net/index/favicon.ico;#This is the full path to your file
}
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"; 
}

You're ready to use it.

Things to Note

  1. If the converted webp file is larger than the original image, the original will be used. Check the response headers for x-compression-rate; its presence means the resource has been processed by webp-server-go.
  2. Ideally, the origin server should support etag.
  3. Under high concurrency, consider using the optional environment variable above to reduce memory usage.
  4. If daily traffic exceeds 20w visits, consider optimizing it yourself. One drawback of go is that garbage collection can block execution. If you have the skills, rewriting it in rust would be better.
  5. Using a CDN affects client detection, but not using CDN caching isn't ideal either. I don't have a good solution for this yet.

Last updated 2025-03-23

Related posts

Comments 0