Caddy 2 如何设置CORS

Caddy 2 去掉了专门的CORS插件,所以需要额外的去重写header,以便支持Access-Control-Allow-Origin "*"

    route {
        try_files {path} {path}/ /index.php?{query}
        php_fastcgi unix//tmp/php-cgi.sock {
            header_down Access-Control-Allow-Origin "*"
        }
    }

header_down表示,从上游的服务器(PHP、node等)取回数据后,重写header。同理,header_up则是把header传给/重写给上游服务器(如用户的浏览器类型、referer等)。

Caddy 2的Caddyfile改变很大,顺便附上一个参考配置

https://test.bra.do {
    encode zstd gzip
    log {
        output file /home/wwwlogs/test.bra.do.log {
        	roll_size 1gb
		    roll_keep 5
		    roll_keep_for 4320h
        }
    }

    tls /etc/ssl/caddy/_.bra.do.crt /etc/ssl/caddy/Naizhao.key

    header / {
        X-Content-Type-Options nosniff
        X-Frame-Options "SAMEORIGIN"
        X-XSS-Protection "1; mode=block"
        X-Robots-Tag none
        X-Download-Options noopen
        X-Permitted-Cross-Domain-Policies none
        Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
    }

    root * /wwwroot/do/bra/test

    route {
        try_files {path} {path}/ /index.php?{query}
        php_fastcgi unix//tmp/php-cgi.sock
    }

    file_server
}