Convert nginx config to Caddy

Hi,

I have the following nginx config and it does the following things;

  • let you access files directly in /css, /fonts, /img, /js with a cache header

  • rewrite the rest to fastcgi with fastcgi_params (index.php?..)

    daemon off;

    events {
    worker_connections 4096;
    }

    http {
    include mime.types;

      client_body_temp_path /tmp/nginx/client_body;
      proxy_temp_path /tmp/nginx/proxy;
      fastcgi_temp_path /tmp/nginx/fastcgi;
      uwsgi_temp_path /tmp/nginx/uwsgi;
      scgi_temp_path /tmp/nginx/scgi;
    
      client_body_buffer_size  1k;
      client_header_buffer_size 1k;
      client_max_body_size 1k;
      large_client_header_buffers 2 1k;    daemon off;
    

    events {
    worker_connections 4096;
    }

    http {
    include mime.types;

    client_body_temp_path /tmp/nginx/client_body;
    proxy_temp_path /tmp/nginx/proxy;
    fastcgi_temp_path /tmp/nginx/fastcgi;
    uwsgi_temp_path /tmp/nginx/uwsgi;
    scgi_temp_path /tmp/nginx/scgi;

    client_body_buffer_size 1k;
    client_header_buffer_size 1k;
    client_max_body_size 1k;
    large_client_header_buffers 2 1k;

    limit_conn_zone $binary_remote_addr zone=addr:5m;
    limit_conn addr 10;

    access_log /tmp/nginx/logs/access.log;

    server {
    listen 8080;
    root /www;
    index index.php;

      server_tokens off;
    
      location ~ ^/(css|fonts|img|js)/  {
          #expires 30d;
      }
    
      location / {
          rewrite ^ /index.php break;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          include fastcgi_params;
          fastcgi_param HTTP_PROXY "";
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_pass php-fpm;
      }
    

    }

    upstream php-fpm {
    server php:8080;
    } }

      limit_conn_zone $binary_remote_addr zone=addr:5m;
      limit_conn addr 10;
    
      access_log /tmp/nginx/logs/access.log;
    
      server {
          listen 8080;
          root /www;
          index index.php;
    
          server_tokens off;
    
          location ~ ^/(css|fonts|img|js)/  {
              #expires 30d;
          }
    
          location / {
              rewrite ^ /index.php break;
              fastcgi_split_path_info ^(.+\.php)(/.+)$;
              include fastcgi_params;
              fastcgi_param HTTP_PROXY "";
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              fastcgi_pass php-fpm;
          }
      }
    
      upstream php-fpm {
          server php:8080;
      }
    

    }

Any idea how I would best inplement this in Caddy? Important thing here is that everything is redirected to fastcgi if not in /css,/fonts, etc.

So far I have:

gzip
minify
/css/,/fonts/,/img/,/js/ {
        root /assets/
        expires {
                match .js$ 1d
                match .css$ 1d
                match .eaf$ 1m
        }
}
fastcgi / php-fpm:8080 php

Thanks for showing what you’ve got so far!

Remember that the first line of your Caddyfile is always the address of the site to serve. You’ve got a couple of different sites here it looks like…

Then the first word of each line inside a site description is a directive.

So this is probably more along the lines of what you want:

your-site.com {
    gzip
    minify
    fastcgi / php-fpm:8080 php
}

your-site.com/css {
    gzip
    minify
    fastcgi / php-fpm:8080 php
    root /assets/css
    expires ...
}
...

Or something like that, anyway.

Hi Matt,

Indeed, as you can see on the github issuelist, my config is this so far;

http://hazcod.com/ {
    log stdout
    errors stderr

    header / {
        #"Strict-Transport-Security" "max-age=63072000; includeSubdomains; preload"
        "X-Frame-Options" "DENY"
        "X-Content-Type-Options" "nosniff"
        "X-XSS-Protection" "1; mode=block"
        "Access-Control-Allow-Origin" "{host}"
        "Access-Control-Allow-Methods" "GET, POST, OPTIONS"
        -Server
    }

    gzip
    minify

    rewrite / /index.php{uri}

    fastcgi / php:8080 php {
        env HTTP_PROXY ""
        env SCRIPT_FILENAME /www/index.php
    }
}

http://hazcod.com/js/ {
    header / -Server

    gzip
    minify

    root /assets/js/
    expires {
        match .js$ 1d
    }
}

http://hazcod.com/css/ {
    header / -Server

    gzip
    minify

    root /assets/css/
    expires {
        match .css$ 1d
    }
}

http://hazcod.com/img/ {
    header / -Server

    gzip
    minify

    root /assets/img/
    expires {
        match .png$ 1d
    }
}

http://hazcod.com/fonts/ {
    header / -Server

    gzip
    minify

    root /assets/fonts/
    expires {
        match .tiff$ 1w
    }
}

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