caddycaddyfile

How to set specific headers for a file using caddy


I'm trying to host game build files using caddy. I need those files to have specific headers, otherwise the frontend won't know how to handle them and the game won't play.

Here's my Caddyfile

racing.<my domain> {
        root * /home/<my home directory>/caddy

        # Serve .data.gz and .symbols.json.gz with the correct headers
        handle_path /Build/Build.data.gz {
                header Content-Encoding gzip
                header Content-Type application/gzip
                precompressed gzip
                file_server
        }

        # Serve .js.gz with the correct headers
        handle_path /Build/Build.framework.js.gz {
                header Content-Encoding gzip
                header Content-Type application/javascript
                precompressed gzip
                file_server
        }

        # Serve .wasm.gz with the correct headers
        handle_path /Build/Build.wasm.gz {
                header Content-Encoding gzip
                header Content-Type application/wasm
                precompressed gzip
                file_server
        }

        # Serve .js with the correct headers
        handle_path /Build/Build.loader.js {
                header Content-Type text/javascript
                precompressed gzip
                file_server
        }

        # Default handling for other files, e.g., frontend routes
        try_files {path} /index.html
        file_server
}

As you can see, I have tried to set specific headers for each file, but they have "text/html" content-type when they get on my browser.

I am new to Caddyfiles, and I feel like it is a syntax issue/misuse of certain tags.


Solution

  • Instead of using handle_path, I need to use header as defined in Caddy's documentation here https://caddyserver.com/docs/caddyfile/directives/header

    Here's the corrected version of my Caddyfile

    <my domain> {
            root * /home/<my username>/caddy
    
            header /Build/Build.data.gz {
                Content-Encoding gzip
                Content-Type application/gzip
            }
            header /Build/Build.framework.js.gz {
                Content-Encoding gzip
                Content-Type application/javascript
            }
            header /Build/Build.wasm.gz {
                Content-Encoding gzip
                Content-Type application/wasm
            }
            header /Build/Build.loader.js {
                Content-Type text/javascript
            }
    
            # Default handling for other files, e.g., frontend routes
            try_files {path} /index.html
            file_server
    }