Porting nginx config to Caddy

I would love not to have to install nginx to run writefreely. Writefreely production requires something like this:

server {
    listen 80;
    listen [::]:80;

    server_name example.com;

    gzip on;
    gzip_types
      application/javascript
      application/x-javascript
      application/json
      application/rss+xml
      application/xml
      image/svg+xml
      image/x-icon
      application/vnd.ms-fontobject
      application/font-sfnt
      text/css
      text/plain;
    gzip_min_length 256;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_vary on;

    location ~ ^/.well-known/(webfinger|nodeinfo|host-meta) {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://127.0.0.1:8080;
        proxy_redirect off;
    }

    location ~ ^/(css|img|js|fonts)/ {
        root /var/www/example.com/static;
        # Optionally cache these files in the browser:
        # expires 12M;
    }

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://127.0.0.1:8080;
        proxy_redirect off;
    }
}

How do you go about to porting that to a Caddyfile? TIA!

This is all super simple to translate. Going from top to bottom:

Site label example.com. Prefix with http:// if you want no HTTPS (this nginx config does not serve HTTPS).

Use gzip

Transparent proxies. There’s no regex to select for proxy from path, so you’ll need to enumerate them

proxy /.well-known/webfinger http://127.0.0.1:8080 {
  transparent
}
proxy /.well-known/nodeinfo http://127.0.0.1:8080 {
  transparent
}
proxy /.well-known/host-meta http://127.0.0.1:8080 {
  transparent
}

Set the site root, root /var/www/example.com/static

Catch-all transparent proxy

proxy / http://127.0.0.1:8080 {
  transparent
}
1 Like

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