Making JSON with Caddyfile V1 to use JSON with V2

I was hopping to using Caddy v1 to output a JSON file, which should theoretically compatible with V2 , so I can just run to Caddy v2 with little work.

1. Caddy version (caddy version):

root@caddy:/usr/local/www # caddy -version
v1.0.4
root@caddy:/usr/local/www #

2. How I run Caddy:

package from repository. have try using getcaddy.com as well.

a. System environment:

FreeNAS 11.2-U7

b. Command:

root@caddy:/usr/local/www # caddy -caddyfile-to-json
^C  root@caddy:/usr/local/www #

c. Service/unit/compose file:



d. My complete Caddyfile or JSON config:

root@caddy:/usr/local/www # cat Caddyfile
mytest.website.com {

        root   /usr/local/www/nextcloud
        log    /var/log/nextcloud_access.log
        errors /var/log/nextcloud_errors.log

        fastcgi / 127.0.0.1:9000 php {
                env PATH /bin
                env modHeadersAvailable true
                env front_controller_active true
                connect_timeout 60s
                read_timeout 3600s
                send_timeout 300s
        }

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

        header /core/fonts {
                Cache-Control                           "max-age=604800"
        }

        # checks for images
        rewrite {
                ext .png .html .ttf .ico .jpg .jpeg .css .js .woff .woff2 .svg .gif .map
                r ^/index.php/.*$
                to /{1} /index.php?{query}
        }

        rewrite {
                r ^/\.well-known/host-meta$
                to /public.php?service=host-meta&{query}
        }
        rewrite {
                r ^/\.well-known/host-meta\.json$
                to /public.php?service=host-meta-json&{query}
        }
        rewrite {
                r ^/\.well-known/webfinger$
                to /public.php?service=webfinger&{query}
        }

        rewrite {
                r ^/index.php/.*$
                to /index.php?{query}
        }

        rewrite / {
                if {path} not_starts_with /remote.php
                if {path} not_starts_with /public.php
                ext .png .html .ttf .ico .jpg .jpeg .css .js .woff .woff2 .svg .gif .map .html .ttf
                r ^/(.*)$
                to /{1} /index.php{uri}
        }

        rewrite / {
                if {path} not /core/img/favicon.ico
                if {path} not /core/img/manifest.json
                if {path} not_starts_with /remote.php
                if {path} not_starts_with /public.php
                if {path} not_starts_with /cron.php
                if {path} not_starts_with /core/ajax/update.php
                if {path} not_starts_with /status.php
                if {path} not_starts_with /ocs/v1.php
                if {path} not_starts_with /ocs/v2.php
                if {path} not /robots.txt
                if {path} not_starts_with /updater/
                if {path} not_starts_with /ocs-provider/
                if {path} not_starts_with /ocm-provider/
                if {path} not_starts_with /.well-known/
                to /index.php{uri}
        }

        # client support (e.g. os x calendar / contacts)
        redir /.well-known/carddav /remote.php/carddav 301
        redir /.well-known/caldav /remote.php/caldav 301

        # remove trailing / as it causes errors with php-fpm
        rewrite {
                r ^/remote.php/(webdav|caldav|carddav|dav)(\/?)(\/?)$
                to /remote.php/{1}
        }

        rewrite {
                r ^/remote.php/(webdav|caldav|carddav|dav)/(.+?)(\/?)(\/?)$
                to /remote.php/{1}/{2}
        }

        rewrite {
                r ^/public.php/(dav|webdav|caldav|carddav)(\/?)(\/?)$
                to /public.php/{1}
        }

        rewrite {
                r ^/public.php/(dav|webdav|caldav|carddav)/(.+)(\/?)(\/?)$
                to /public.php/{1}/{2}
        }

        # .htaccess / data / config / ... shouldn't be accessible from outside
        status 404 {
                /.htaccess
                /data
                /config
                /db_structure
                /.xml
                /README
                /3rdparty
                /lib
                /templates
                /occ
                /console.php
        }

}
root@caddy:/usr/local/www #

3. The problem I’m having:

result not expeced
It will stuck for very long time

4. Error messages and/or full log output:

root@caddy:/var/log # ls -al
total 170
drwxr-xr-x   2 root  wheel       30 Apr 28 03:01 .
drwxr-xr-x  24 root  wheel       24 May  7 07:13 ..
-rw-------   1 root  wheel       62 Apr 20 12:25 auth.log
-rw-------   1 root  wheel    73029 May  7 07:22 cron
-rw-------   1 root  wheel     7094 Apr 23 06:00 cron.0.bz2
-rw-------   1 root  wheel       62 Apr 20 12:25 debug.log
-rw-r--r--   1 root  wheel       62 Apr 20 12:25 devd.log
-rw-------   1 root  wheel    24699 Apr 28 03:01 dmesg.today
-rw-------   1 root  wheel    21941 Apr 27 03:01 dmesg.yesterday
-rw-r--r--   1 root  wheel       62 Apr 20 12:25 lpd-errs
-rw-r-----   1 root  wheel     5411 Apr 28 03:09 maillog
-rw-r-----   1 root  wheel     1152 Apr 28 00:00 maillog.0.bz2
-rw-r-----   1 root  wheel      937 Apr 27 00:00 maillog.1.bz2
-rw-r-----   1 root  wheel      831 Apr 23 00:00 maillog.2.bz2
-rw-r-----   1 root  wheel      633 Apr 22 00:00 maillog.3.bz2
-rw-r-----   1 root  wheel      130 Apr 21 00:00 maillog.4.bz2
-rw-r--r--   1 root  wheel     6419 May  7 07:13 messages
-rw-------   1 root  wheel       55 Apr 21 03:02 mount.today
-rw-r--r--   1 root  wheel        0 Apr 26 18:30 nextcloud_access.log
-rw-r--r--   1 root  wheel        0 Apr 26 18:30 nextcloud_errors.log
-rw-r-----   1 root  network     62 Apr 20 12:25 ppp.log
-rw-------   1 root  wheel       62 Apr 20 12:25 security
-rw-r-----   1 root  wheel        0 Apr 27 14:00 sendmail.st
-rw-r-----   1 root  wheel        0 Apr 27 14:00 sendmail.st.0
-rw-r-----   1 root  wheel        0 Apr 20 14:00 sendmail.st.1
-rw-------   1 root  wheel     3304 Apr 21 03:02 setuid.today
-rw-------   1 root  wheel      315 Apr 20 12:46 userlog
-rw-r--r--   1 root  wheel        0 Apr 20 12:25 utx.lastlogin
-rw-r--r--   1 root  wheel       87 May  7 07:13 utx.log
-rw-------   1 root  wheel       62 Apr 20 12:25 xferlog
root@caddy:/var/log #
May  7 07:13:07 caddy /usr/sbin/cron[57140]: (CRON) WARNING (madvise() failed)
May  7 07:15:14 caddy /usr/sbin/cron[57306]: (root) CMD (/usr/libexec/atrun)
May  7 07:20:02 caddy /usr/sbin/cron[57738]: (root) CMD (/usr/libexec/atrun)
May  7 07:22:00 caddy /usr/sbin/cron[57944]: (operator) CMD (/usr/libexec/save-entropy)
root@caddy:/var/log #
root@caddy:/var/log # cat messages
May  7 07:13:07 caddy syslogd: kernel boot file is /boot/kernel/kernel
root@caddy:/var/log #

5. What I already tried:

 caddy -caddyfile-to-json 
 caddy -caddyfile-to-json -conf Caddyfile
 caddy -caddyfile-to-json caddy.json -conf Caddyfile

6. Links to relevant resources:

What is that? :thinking:

Caddy 1 does not support JSON for configuration, you can only use the Caddyfile with Caddy 1.

1 Like
root@caddy:/ # caddy -version
v1.0.4
root@caddy:/ # caddy --help
Usage of caddy:
  -agree
        Agree to the CA's Subscriber Agreement
  -ca string
        URL to certificate authority's ACME server directory (default "https://acme-v02.api.letsencrypt.org/directory")
  -caddyfile-to-json
        From Caddyfile stdin to JSON stdout
  -catimeout duration
        Default ACME CA HTTP timeout (default 30s)
  -conf string
        Caddyfile to load (default "Caddyfile")
  -cpu string
        CPU cap (default "100%")
  -default-sni string
        If a ClientHello ServerName is empty, use this ServerName to choose a TLS certificate
  -disable-http-challenge
        Disable the ACME HTTP challenge
  -disable-tls-alpn-challenge
        Disable the ACME TLS-ALPN challenge
  -disabled-metrics string
        Comma-separated list of telemetry metrics to disable
  -email string
        Default ACME CA account email address
  -env
        Enable to print environment variables
  -envfile string
        Path to file with environment variables to load in KEY=VALUE format
  -ginkgo.debug
        If set, ginkgo will emit node output to files when running in parallel.
  -ginkgo.dryRun
        If set, ginkgo will walk the test hierarchy without actually running anything.  Best paired with -v.
  -ginkgo.failFast
        If set, ginkgo will stop running a test suite after a failure occurs.
  -ginkgo.failOnPending
        If set, ginkgo will mark the test suite as failed if any specs are pending.
  -ginkgo.flakeAttempts int
        Make up to this many attempts to run each spec. Please note that if any of the attempts succeed, the suite will not be failed. But any failures will still be recorded. (default 1)
  -ginkgo.focus string
        If set, ginkgo will only run specs that match this regular expression.
  -ginkgo.noColor
        If set, suppress color output in default reporter.
  -ginkgo.noisyPendings
        If set, default reporter will shout about pending tests. (default true)
  -ginkgo.noisySkippings
        If set, default reporter will shout about skipping tests. (default true)
  -ginkgo.parallel.node int
        This worker node's (one-indexed) node number.  For running specs in parallel. (default 1)
  -ginkgo.parallel.streamhost string
        The address for the server that the running nodes should stream data to.
  -ginkgo.parallel.synchost string
        The address for the server that will synchronize the running nodes.
  -ginkgo.parallel.total int
        The total number of worker nodes.  For running specs in parallel. (default 1)
  -ginkgo.progress
        If set, ginkgo will emit progress information as each spec runs to the GinkgoWriter.
  -ginkgo.randomizeAllSpecs
        If set, ginkgo will randomize all specs together.  By default, ginkgo only randomizes the top level Describe, Context and When groups.
  -ginkgo.regexScansFilePath
        If set, ginkgo regex matching also will look at the file path (code location).
  -ginkgo.seed int
        The seed used to randomize the spec suite. (default 1588819056)
  -ginkgo.skip string
        If set, ginkgo will only run specs that do not match this regular expression.
  -ginkgo.skipMeasurements
        If set, ginkgo will skip any measurement specs.
  -ginkgo.slowSpecThreshold float
        (in seconds) Specs that take longer to run than this threshold are flagged as slow by the default reporter. (default 5)
  -ginkgo.succinct
        If set, default reporter prints out a very succinct report
  -ginkgo.trace
        If set, default reporter prints out the full stack trace when a failure occurs
  -ginkgo.v
        If set, default reporter print out all specs as they begin.
  -grace duration
        Maximum duration of graceful shutdown (default 5s)
  -host string
        Default host
  -http-port int
        Default port to use for HTTP (default 80)
  -http2
        Use HTTP/2 (default true)
  -https-port int
        Default port to use for HTTPS (default 443)
  -json-to-caddyfile
        From JSON stdin to Caddyfile stdout
  -log string
        Process log file
  -log-roll-compress
        Gzip-compress rolled process log files (default true)
  -log-roll-mb int
        Roll process log when it reaches this many megabytes (0 to disable rolling) (default 100)
  -log-timestamps
        Enable timestamps for the process log (default true)
  -pidfile string
        Path to write pid file
  -plugins
        List installed plugins
  -port string
        Default port (default "2015")
  -quic
        Use experimental QUIC
  -quiet
        Quiet mode (no initialization output)
  -revoke string
        Hostname for which to revoke the certificate
  -root string
        Root path of default site (default ".")
  -type string
        Type of server to run (default "http")
  -validate
        Parse the Caddyfile but do not start the server
  -version
        Show version
root@caddy:/ #

Line 7 and line 8 , From Caddyfile stdin to JSON stdout

I guess the fuction of “to-JSON” didn’t really implemented ?

p/s: I really dig deep to get what I need

Minor FYI - you’ve linked to someone else’s forked Caddy repo.

The equivalent in the v1 branch of the official Caddy repository is here:

Ohhhh. I forgot about that flag…

What I said still stands though, you can’t use v1 to get a v2 config.

I thought JSON file should technically universal the same.

At one point I was going to use nginx adapter in V2, change it to JSON, and use it on V1 with --json-to-caddyfile flag

JSON is just a format.

The layout and content of the things you store in JSON is everything to how the server is configured. Caddy v1 doesn’t use JSON at its core, and if it did, it would be vastly different to Caddy v2’s structure.

This would most definitely not produce a working config.

To be clear, certain control structures in nginx are simply not possible to achieve in v1 Caddyfile. Even if it could somehow produce an appropriate translation, it would be ignorant of the many hacks and tricks required to make Caddy v1 operate with similar behaviours to the usual nginx methods. Not to mention the behaviours that must simply be tackled from a completely different angle, logically speaking.

Caddy v2 is much more robust and powerful, which is why it is feasible to create an nginx config adapter for v2 specifically.

1 Like

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