Convert munin's nginx or apache config to Caddy


(Avinash H Duduskar) #1

I’m trying to convert munin’s nginx or apache config using fastcgi to Caddy

This is what I came up with but it doesn’t work -

munin.wrtpoona.in {

                rewrite /.* /usr/share/munin/cgi/munin-cgi-html/$1
                rewrite {
        if {file} is *.html
        to {path} {path}/ /usr/share/munin/cgi/munin-cgi-html/$1
}
                rewrite /static/.* /etc/munin/static/$1
                rewrite {
        if {file} is favicon.ico
        to {path} {path}/ /etc/munin/static/favicon.ico
}
                rewrite /munin-cgi/.* /usr/share/munin/cgi/$1
                tls strykar@hotmail.com
                log stdout
                errors
                root /usr/share/munin
                fastcgi / unix:/var/run/fcgiwrap.sock {
                                env SCRIPT_FILENAME /run/munin/fcgi-graph.sock
        }
}

munin.wrtpoona.in/munin/static/ {
                root /etc/munin/static/
}

munin.wrtpoona.in/munin {
                root /usr/share/munin
                fastcgi / unix:/var/run/fcgiwrap.sock {
                env SCRIPT_FILENAME /run/munin/fcgi-html.sock
                }
}

Here are working nginx and apache configs that I am trying to convert -
Apache:

    <VirtualHost *:80>
       ServerName localhost
       ServerAdmin  root@localhost

       DocumentRoot /srv/http/munin

       ErrorLog /var/log/httpd/munin-error.log
       CustomLog /var/log/httpd/munin-access.log combined


       # Rewrites
       RewriteEngine On

       # Static content in /static
       RewriteRule ^/favicon.ico /etc/munin/static/favicon.ico [L] 
       RewriteRule ^/static/(.*) /etc/munin/static/$1          [L] 

       # HTML
       RewriteCond %{REQUEST_URI} .html$ [or]
       RewriteCond %{REQUEST_URI} =/
       RewriteRule ^/(.*)          /usr/share/munin/cgi/munin-cgi-html/$1 [L] 

       # Images
       RewriteRule ^/munin-cgi(.*) /usr/share/munin/cgi/$1 [L] 

       <Directory "/etc/munin/static">
           Require all granted
       </Directory>

       # Ensure we can run (fast)cgi scripts
       <Directory "/usr/share/munin/cgi">
           Require all granted
           Options +ExecCGI
           <IfModule mod_fcgid.c>
               SetHandler fcgid-script
           </IfModule>
           <IfModule !mod_fcgid.c>
               SetHandler cgi-script
           </IfModule>
       </Directory>
    </VirtualHost>

Nginx:

server {
    server_name yourhost.example.com;
    listen 80;
    access_log /var/log/nginx/munin-access.log;
    error_log /var/log/nginx/munin-error.log info;
    location ^~ /munin-cgi/munin-cgi-graph/ {
        fastcgi_split_path_info ^(/munin-cgi/munin-cgi-graph)(.*);
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass unix:/run/munin/fcgi-graph.sock;
        include fastcgi_params;
    }
    location /munin/static/ {
        alias /etc/munin/static/;
    }
    location /munin/ {
        fastcgi_split_path_info ^(/munin)(.*);
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass unix:/run/munin/fcgi-html.sock;
        include fastcgi_params;
    }
}

Static web config is working when configured like Convert munin's nginx config to Caddy but that post didn’t get much traction.

FastCGI works with another app (Smokeping) and Caddy, but I’m unable to get it to work with munin -

smokeping.example.com {
        log stdout
        errors
        tls john@example.com
        root /srv/http/smokeping
        fastcgi / unix:/var/run/fcgiwrap.sock {
                env SCRIPT_FILENAME /srv/http/smokeping/smokeping.fcgi.dist
        }
}
smokeping.example.com/js {
        root /srv/http/smokeping/js
}
smokeping.example.com/css {
        root /srv/http/smokeping/css
}
smokeping.example.com/cache {
        root /var/cache/smokeping
}

I could use some help :confused:


(Matthew Fay) #2

Hi @Strykar,

Sorry that nobody’s managed to offer an answer here for a week, but it’s a pretty big task!

I haven’t gone through and checked all the conversions myself, but I did note some potential problems.

Firstly, the regex replacement for rewrites doesn’t use the $1 style of substitution.

Instead, it uses the placeholder system - emphasis mine:

destinations… is one or more space-separated paths to rewrite to, with support for request placeholders as well as numbered regular expression captures such as {1}, {2}, etc. Rewrite will check each destination in order and rewrite to the first destination that exists. Each one is checked as a file or, if ends with /, as a directory. The last destination will act as default if no other destination exists.

https://caddyserver.com/docs/rewrite

So you may see a better result using {1}, {2}, etc in your regex rewrites.

Secondly, rewriting if {file} is *.html won’t work because A) you don’t have a capture group and B) comparisons like is, not, has etc aren’t regex comparisons and won’t produce capture data anyway. You need to use the regexp subdirective to do this kind of check.

Those are the things I could see going over it pretty quickly, hopefully this helps you refactor your Caddyfile a bit and get some results.


(Nicolas) #3

We use Munin with Caddy and here’s what we have in our Caddyfile :

(munin) {
    basicauth / login password
    log /var/log/caddy/munin.access.log
    errors /var/log/caddy/munin.error.log
    gzip
}

munin.domain.com {
    root /var/www/munin 
    import munin
    fastcgi /munin-cgi/munin-cgi-graph/ /var/run/munin/fcgi-graph.sock {
        split ^(/munin-cgi/munin-cgi-graph)(.*)
    }
    fastcgi /munin/ /var/run/munin/fcgi-html.sock {
         split ^(/munin)(.*)
    }
}

munin.domain.com/static {
    root /etc/munin/static
    import munin
}

It works fine… except for the dynazoom part of Munin. We tried several ideas on the fastcgi part, but could not make it work. Since it’s not a high priority for us, we have left things as they were, but if someone has an idea, I’ll take it ! :slight_smile: