Is it possible to use self hosted analytics with Caddy?

Hello I’m curious if it’s possible to use programs like Shynet (https://github.com/milesmcc/shynet) with Caddy. I was able to use it pretty easily with Nginx but I haven’t been able to find out exactly how it can be done with Caddy.

From Shynet
chrome_VGZAQSeevv

In Nginx I was using it like so

sub_filter '</head>' '<script src="https://domain.tld/ingress/4fee8707-ffc6-41b1-a25a-46748d4c8f04/script.js"></script></head>';
sub_filter_once on;

Does Caddy have a directive that can be used similarly?

You can use the replace-response plugin.

2 Likes

Thanks for the swift response, I’ll give it a shot!

1 Like

So i’ve got the plugin set up now I’m just not sure about how the syntax should go. The examples use words like “Foo” so its a bit unclear how i’m to input something with characters like <> or quotations. I tried it like this which is definitely wrong and is throwing errors. I added

order replace after encode

to my global options

domain.tld {
   root * /home/gallery
   encode gzip
   replace { 
   </head> <script defer src="https://domain.tld/ingress/0a88af5c-75af-492c-8c05-ca226c769289/script.js"></script></head>
   }
   file_server
   log {
   output file /data/blog.log
   }
   tls {
   dns cloudflare 123456789
   }
   header {
        Strict-Transport-Security max-age=31536000;
        X-Content-Type-Options nosniff;
        X-XSS-Protection "1; mode=block;"
        X-Robots-Tag none;
        X-Frame-Options SAMEORIGIN;
        Referrer-Policy no-referrer;
  }
}

Best place to reference re: quoting/escaping in the Caddyfile is the Caddyfile Concepts documentation:

In your case I’d suggest backticks are the simplest option (the last example in that section as of this writing).

directive `"foo bar"`
2 Likes

Still throwing errors with backticks. I tried it like this

replace { 
   '</head>' '<script defer src="https://domain.tld/ingress/0a88af5c-75af-492c-8c05-ca226c769289/script.js"></script></head>'
   }

error log says

{"level":"info","ts":1626049959.4685297,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
run: adapting config using caddyfile: parsing caddyfile tokens for 'replace': /etc/caddy/Caddyfile:222 - Error during parsing: Wrong argument count or unexpected line ending after 'defer'
{"level":"info","ts":1626049960.4398599,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
run: adapting config using caddyfile: parsing caddyfile tokens for 'replace': /etc/caddy/Caddyfile:222 - Error during parsing: Wrong argument count or unexpected line ending after 'defer'
{"level":"info","ts":1626049961.4446237,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
run: adapting config using caddyfile: parsing caddyfile tokens for 'replace': /etc/caddy/Caddyfile:222 - Error during parsing: Wrong argument count or unexpected line ending after 'defer'
{"level":"info","ts":1626049962.4865465,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
run: adapting config using caddyfile: parsing caddyfile tokens for 'replace': /etc/caddy/Caddyfile:222 - Error during parsing: Wrong argument count or unexpected line ending after 'defer'

Those don’t look like backticks (`) to me. They look like single apostrophes (’).

The backtick on the US keyboard layout is at the top-left - it’s the tilde key (~) without pressing the shift modifier.

2 Likes

Hahaha oh man I’m a dumb dumb. Thanks :man_facepalming:

Can confirm it’s working now. Cheers guys!

2 Likes

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