Is there any way to reduce this configuration

example.com {
root * /home/xyz/web/
        @brCSS {
                header Accept-Encoding *br*
                file {
                        try_files {path}.br
                }
                path /*.css
        }

        handle @brCSS {
                header Content-Encoding br
                header Content-Type text/css
                rewrite {path}.br
        }

        @brHTM {
                header Accept-Encoding *br*
                file {
                        try_files {path}.br
                }
                path /*.htm
        }

        handle @brHTM {
                header Content-Encoding br
                header Content-Type text/html
                rewrite {path}.br
        }

        @brHTML {
                header Accept-Encoding *br*
                file {
                        try_files {path}.br
                }
                path /*.html
        }

        handle @brHTML {
                header Content-Encoding br
                header Content-Type text/html
                rewrite {path}.br
        }

        @brJS {
                header Accept-Encoding *br*
                file {
                        try_files {path}.br
                }
                path /*.js
        }

        handle @brJS {
                header Content-Encoding br
                header Content-Type application/javascript
                rewrite {path}.br
        }

        @brJSON {
                header Accept-Encoding *br*
                file {
                        try_files {path}.br
                }
                path /*.json
        }

        handle @brJSON {
                header Content-Encoding br
                header Content-Type application/json
                rewrite {path}.br
        }

        @brSVG {
                header Accept-Encoding *br*
                file {
                        try_files {path}.br
                }
                path /*.svg
        }

        handle @brSVG {
                header Content-Encoding br
                header Content-Type image/svg+xml
                rewrite {path}.br
        }

        @brTXT {
                header Accept-Encoding *br*
                file {
                        try_files {path}.br
                }
                path /*.txt
        }

        handle @brTXT {
                header Content-Encoding br
                header Content-Type text/plain
                rewrite {path}.br
        }

        @gzipCSS {
                header Accept-Encoding *gzip*
                file {
                        try_files {path}.gz
                }
                path /*.css
        }

        handle @gzipCSS {
                header Content-Encoding gzip
                header Content-Type text/css
                rewrite {path}.gz
        }

        @gzipHTM {
                header Accept-Encoding *gzip*
                file {
                        try_files {path}.gz
                }
                path /*.htm
        }

        handle @gzipHTM {
                header Content-Encoding gzip
                header Content-Type text/html
                rewrite {path}.gz
        }
                @gzipHTML {
                header Accept-Encoding *gzip*
                file {
                        try_files {path}.gz
                }
                path /*.html
        }

        handle @gzipHTML {
                header Content-Encoding gzip
                header Content-Type text/html
                rewrite {path}.gz
        }

        @gzipJS {
                header Accept-Encoding *gzip*
                file {
                        try_files {path}.gz
                }
                path /*.js
        }

        handle @gzipJS {
                header Content-Encoding gzip
                header Content-Type application/javascript
                rewrite {path}.gz
        }

        @gzipJSON {
                header Accept-Encoding *gzip*
                file {
                        try_files {path}.gz
                }
                path /*.json
        }

        handle @gzipJSON {
                header Content-Encoding gzip
                header Content-Type application/json
                rewrite {path}.gz
        }

        @gzipSVG {
                header Accept-Encoding *gzip*
                file {
                        try_files {path}.gz
                }
                path /*.svg
        }

        handle @gzipSVG {
                header Content-Encoding gzip
                header Content-Type image/svg+xml
                rewrite {path}.gz
        }

        @gzipTXT {
                header Accept-Encoding *gzip*
                file {
                        try_files {path}.gz
                }
                path /*.txt
        }

        handle @gzipTXT {
                header Content-Encoding gzip
                header Content-Type text/plain
                rewrite {path}.gz
        }

        header *.css Content-Type text/css
        header *.htm Content-Type text/html
        header *.html Content-Type text/html
        header *.js Content-Type application/javascript
        header *.json Content-Type application/json
        header *.svg Content-Type image/svg+xml
        header *.txt Content-Type text/plain

        @cIndex {
                not path /index.html
                not path /api/*
        }
        header @cIndex Cache-Control "public, max-age=31536000, immutable"

        header -Server
        header -X-Powered-By

        @spa {
                file {
                        try_files {path} /index.html
                }
                not path /api/*
        }
        handle @spa {
                header Cache-Control no-cache
                rewrite {http.matchers.file.relative}
        }

        file_server
        reverse_proxy /api/* 127.0.0.1:3000 {
                transport http {
                        versions h2c 2
                }
        }
}


If you have a question, please fill out the help topic template as per the forum rules.

thanks for reply, actually here i have to keep writing for each extension that if match rewrite to *.br or *.gz if available, i just want to know is there any alternative or shorter way to do this?
thanks

See the file_server directive’s precompressed option: file_server (Caddyfile directive) — Caddy Documentation

Or use snippets to deduplicate config: Caddyfile Concepts — Caddy Documentation

thanks, precompressed is amazing, now my new config is

example.com {
	root * /home/xyz/web/

	@cIndex {
		not path /index.html /assets/no-cache/* /api/*
	}
	header @cIndex Cache-Control "public, max-age=31536000, immutable"

	header -Server
	header -X-Powered-By

	@spa {
		not path /api/*
		not file
	}
	handle @spa {
		header Cache-Control no-cache
		rewrite * /index.html
	}

	file_server {
		precompressed br gzip zstd
	}

	reverse_proxy /api/* 127.0.0.1:3000 {
		transport http {
			versions h2c 2
		}
	}
}

thanks,
is this updated config is good to go?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.