Static brotli/gzip and reverse proxy config question

Thanks @francislavoie for your advices, it works perfect !

I share the config here for others:

{
    experimental_http3
}

# See https://github.com/caddyserver/caddy/issues/2665

(precompressed) {
  @{args.0}{args.1} {
    header Accept-Encoding *{args.2}*
    path *.{args.0}
    file {path}.{args.1}
  }
  handle @{args.0}{args.1} {
    rewrite {path}.{args.1}
    header Content-Encoding {args.2}
    header Content-Type {args.3}
    header Cache-Control max-age={args.4},public
    header Vary "Accept-Encoding"
  }
}
(uncompressed) {
  @{args.0}{args.1} {
    path *.{args.0}
    file {path}.{args.1}
  }
  handle @{args.0}{args.1} {
    header Cache-Control max-age={args.4},public
    header Vary "Accept-Encoding"
  }
}

my-nextjs-site.example.com {

    log {
        output file my-nextjs-site.example.com.log {
            roll_size 5MiB
            roll_keep 8
            roll_keep_for 72h
        }
        format console
    }


    handle_path /_next/static/* {

        # Rewrite to next static

        root * /path/to/my-nextjs-site.example.com/.next/static

        # Precompress static assets

        import precompressed html br br text/html 300
        import precompressed html gz gzip text/html 300
        import uncompressed html - - - 300
        import precompressed js br br application/javascript 2628000
        import precompressed js gz gzip application/javascript 2628000
        import uncompressed js - - - 2628000
        import precompressed map br br application/json 2628000
        import precompressed map gz gzip application/json 2628000
        import uncompressed map - - - 2628000
        import precompressed css br br "text/css; charset=utf-8" 2628000
        import precompressed css gz gzip "text/css; charset=utf-8" 2628000
        import uncompressed css - - - 2628000

        # Add cache control 

        @static {
            file
            path *.ico *.gif *.jpg *.jpeg *.png *.svg *.woff *.map
        }

        header @static Cache-Control max-age=2628000

        file_server
    }

    # Reverse proxy the nextjs app for server side rendering

    handle {
        encode zstd gzip
        reverse_proxy 127.0.0.1:3001
    }
}
1 Like