caddy配置文件Caddyfile示例

{
    # 这块是全局配置
    # http不要自动转跳https
    auto_https disable_redirects
    # 内部私有证书,不要自动安装到certs系统目录里
    skip_install_trust
    # 在线核对证书状态间隔时间,
    ocsp_interval 12h

    # 全局监听配置
    #servers {
    # http请求头最大字节大小
    # max_header_size 5MB
    # # tcp keepalive保活时间
    # keepalive_interval 30s
    #    listener_wrappers {
    #    # http自动转跳https
    #        http_redirect
    #        tls
    #    }
    #}
    # 全局日志
    log {
        level ERROR
        # based on hostname
        #      output file /var/log/caddy/{host}.log  
        format json
    }
}

# ------

# 举例使用 import block__handle_check_slash_end_args /test
(block__handle_check_slash_end_args) {
    #route {
    #      @noSlash {
    #          path_regexp [^/]$  # Match paths that do not end with a slash
    #      }
    #      redir @noSlash {http.request.scheme}://{http.request.host}{http.request.uri}/ 302
    # }

    @append_slash {
        path_regexp /({args[0]}|{args[1]}|{args[2]}|{args[3]}|{args[4]}|{args[5]}|{args[6]}|{args[7]}|{args[8]}|{args[9]})[^/]*$
    }

    redir @append_slash {path} {path}/ 302
}

# 举例使用 import block__sub_header
(block__sub_header) {
    header {
        -server
        +server IIS/10.0
        x-aspnet-version 9.0
        x-server-version 9.0
        frame-options SAMRORIGIN
        -'proxy-connection'
    }
}

# 举例使用 import block__logger_args /tmp/logs/caddylogs/xxx.log
(block__logger_args) {
    log {
        output file /home/datum/logs/caddylogs/{args[0]}.log {
            roll_size 1gb
            roll_uncompressed
        }
    }
}

# 举例使用 import block__cors
(block__cors) {
    route {
        @CheckOptions {
            method OPTIONS
        }
        # 跨域预请求响应
        respond @CheckOptions 200 {
            #header Access-Control-Allow-Headers * #这个有问题,不能是通配符
            #header Access-Control-Allow-Headers X-XSRF-TOKEN,XSRF-TOKEN,language,token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization
            header Access-Control-Allow-Methods GET,POST,PUT,OPTIONS
            header Access-Control-Allow-Origin {http.request.header.Origin}
            header Access-Control-Allow-Headers {http.request.header.Access-Control-Request-Headers}
            header Access-Control-Allow-Credentials true
            #respond 200
        }

        @notOptions {
            not method OPTIONS
        }

        # 默认常规请求方式响应(非options)
        header @notOptions {
            # Echo
            Access-Control-Allow-Methods GET, POST, PUT, OPTIONS
            Access-Control-Allow-Origin {http.request.header.Origin}
            Access-Control-Allow-Headers {http.request.header.Access-Control-Request-Headers}
            Access-Control-Allow-Credentials true
        }
    }
}

# 举例使用 import block__proxy_upstream_cors_headers_strip
(block__proxy_upstream_cors_headers_strip) {
    #请求上游重写替换请求头,Strip 
    header_up Access-Control-Allow-Origin ""
    header_up Access-Control-Allow-Methods ""
    header_up Access-Control-Allow-Headers ""

    # 响应给下游(浏览器)的响应头,maybe duplicated
    header_down Access-Control-Allow-Methods GET,POST,PUT,OPTIONS
    header_down Access-Control-Allow-Origin {http.request.header.Origin}
    header_down Access-Control-Allow-Headers {http.request.header.Access-Control-Request-Headers}
    header_down Access-Control-Allow-Credentials true
}

# 举例使用 import block__html_no_cache
(block__html_no_cache) {
    # Apply no-cache headers only to .html files
    @htmlFiles {
        path *.html
    }

    header @htmlFiles {
        Cache-Control "no-cache, no-store, must-revalidate, max-age=0"
        Pragma "no-cache"
        Expires "0"
    }
}

# 代码块,使用举例:import block__http2https
(block__http2https) {
    @CheckHttp {
        protocol http
    }
    redir @CheckHttp https://{hostport}{uri} 302
}

# 代码块,使用举例: import block__proxy_upstream_args * https://www.baidu.com
(block__proxy_upstream_args) {
    reverse_proxy {args[:]} {
        header_down -'proxy-connection'
        flush_interval 300ms
        transport http {
            dial_timeout 10s
            keepalive 10m
            keepalive_idle_conns 10240
        }
    }
}

# 代码块,使用举例: import block__proxy_upstream_start_with_host_args www.baidu.com  8.8.8.8
(block__proxy_upstream_start_with_host_args) {
    reverse_proxy {args[1:]} {
        header_up Host {args[0]}
        header_down -'proxy-connection'
        flush_interval 300ms
        transport http {
            dial_timeout 10s
            keepalive 10m
            keepalive_idle_conns 10240
        }
    }
}

# 举例使用 import block__hide_git
(block__handle_hide_git) {
    @denyGit {
        path (/.*)?/.git(/.*)?
    }
    respond @denyGit 404
}

# 举例使用 import block__compress
(block__compress) {
    encode zstd gzip
}

# 举例使用 import block__handle_strip_path_and_proxy_args test/* http://baidu.com
(block__handle_strip_path_and_proxy_args) {
    handle /{args[0]} {
        uri strip_prefix /{args[1]}

        reverse_proxy {args[2]} {
            header_down -'proxy-connection'
            flush_interval 300ms
            transport http {
                dial_timeout 10s
                keepalive 10m
                keepalive_idle_conns 10240
            }
        }
    }
}

# 举例使用 import block__handle_strip_path_and_file_server_vue3_history_mode_args /test/* /path/to/html/static/file/dir
(block__handle_strip_path_and_file_server_vue3_history_mode_args) {
    handle_path /{args[0]} {
        file_server {
            hide .git
            index index.html
        }
        root * {args[1]}

        try_files {path} /index.html
    }
    # endif
}

# 举例使用 import block__handle_strip_path_and_file_server_only_args /test/* /path/to/html/static/file/dir
(block__handle_strip_path_and_file_server_only_args) {
    handle_path /{args[0]} {
        file_server {
            index index.html
            hide .git
        }
        root * {args[1]}
    }
    # endif
}

# 公共
(block__common) {
    import block__sub_header
    #import block__logger
    #import block__cors
    import block__html_no_cache
    import block__handle_hide_git
    import block__compress
}

# ----------


import ./conf/*.conf
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值