Nginx替换为Caddy2

最近把用了十几年的反向代理Nginx替换为了caddy.
首先我的这个需求可能不适合你,毕竟替换的学习成本比较高.
但是caddy的灵活性更适合我,nginx的灵活性和重新编译都太麻烦了。
本站使用的caddyfile的几个example可以快速入门caddy:

root@VM-zlNr9iaPx8gT:~# cat /etc/caddy/Caddyfile 
````file
{ # for metrics
    servers {
        metrics
    }
}

forum.magentochina.org:443 { # reverse proxy for discourse
        reverse_proxy {
                to 127.0.0.1:8080
                transport http {
                        keepalive 30m
                }
        }
        encode zstd gzip
        tls shuai.zend@qq.com
        log {
        output file /var/log/forum.access.log {
                roll_size 1gb
                roll_keep 5
                roll_keep_for 720h
                }
        format json
        level DEBUG
        }
}
doc.magentochina.org:443 {
        root * /var/www/doc.magentochina.org/_site
        file_server
        encode gzip zstd
        tls shuai.zend@qq.com
}


www.magentochina.org:443, magentochina.org:443 { # for wordpress
        root * /var/www/magentochina.org
        encode gzip zstd
        @cache {
                not header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
                not path_regexp "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(index)?.xml|[a-z0-9-]+-sitemap([0-9]+)?.xml)"
                not method POST
                not expression {query} != ''
         }

     route @cache {
        try_files /wp-content/cache/page_enhanced/{host}{uri}/_index_ssl.html /wp-content/cache/page_enhanced/{host}{uri}/_index.html {path} {path}/index.php?{query}
     }

        php_fastcgi unix//run/php/php8.1-fpm.sock
        log {
        output file /var/log/forum.access.log {
                roll_size 1gb
                roll_keep 5
                roll_keep_for 720h
                }
        format json
        level DEBUG
        }
        file_server
        tls shuai.zend@qq.com
}

如果你是想在 caddy下运行magento2 , 那么可以参考这个配置文件

{
    default_sni {$SERVER_NAME}
}

{$SERVER_NAME} {    
    import /etc/{$TLS_MODE} 
    import /etc/basic_auth.conf
    root * /srv/site/pub
    encode zstd gzip

    @blocked {
        path /media/customer/* /media/downloadable/* /media/import/* /media/custom_options/* /errors/*
    }
    respond @blocked 403

    @notfound {
        path_regexp reg_notfound \/\..*$|\/errors\/.*\.xml$|theme_customization\/.*\.xml
    }
    respond @notfound 404

    @staticPath path_regexp reg_static ^/static/(version\d*/)?(.*)$
    handle @staticPath {
        @static file /static/{re.reg_static.2}
        rewrite @static /static/{re.reg_static.2}

        @dynamic not file /static/{re.reg_static.2}
        rewrite @dynamic /static.php?resource={re.reg_static.2}
    }

    file_server
    php_fastcgi fpm:9000

    log {
        output file /var/log/caddy.log
    }

    header /media X-Frame-Options "SAMEORIGIN"
    header /static X-Frame-Options "SAMEORIGIN"
    header / X-Content-Type-Options "nosniff"

}