[nginx to caddy] help setting up some rules

1. The problem I’m having:

I am trying to convert these nginx rules to caddy

# BEGIN Converter for Media
set $ext_avif ".avif";
if ($http_accept !~* "image/avif") {
    set $ext_avif "";
}

set $ext_webp ".webp";
if ($http_accept !~* "image/webp") {
    set $ext_webp "";
}

location ~ /wp-content/(?<path>.+)\.(?<ext>jpe?g|png|gif|webp)$ {
    add_header Vary Accept;
    expires 365d;
    try_files
        /wp-content/uploads-webpc/$path.$ext$ext_avif
        /wp-content/uploads-webpc/$path.$ext$ext_webp
        $uri =404;
}
# END Converter for Media

I wrote this:

wordpress.website.com {
    root * /home/deploy/wordpress.website.com
    encode gzip
    file_server
    php_fastcgi unix//run/php/php8.2-fpm.sock

    @disallowed {
        path /xmlrpc.php
        path *.sql
        path /wp-content/uploads/*.php
    }

    # Protect WooCommerce upload folder from being accessed directly.
    # You may want to change this config if you are using "X-Accel-Redirect/X-Sendfile" or "Force Downloads" method for downloadable products.
    # Place this config towards the end of the "route" block in your Caddyfile.
    @woocommerce_uploads {
	    path_regexp wp-content/uploads/woocommerce_uploads/.*
    }
    handle @woocommerce_uploads {
	    header {
		    X-Accel-Redirect ""
	    }
	    respond 403 "Access denied"
    }

    log {
	output file /var/log/caddy/wordpress.website.com.log {
		roll_size 1gb
		roll_keep 5
		roll_keep_for 720h
		format console
	}
    }


    rewrite @disallowed '/index.php'

#################################
#    route /wp-content/*.(jpg|jpeg|png|gif|webp) {
#        @ext_avif {
#            not {
#                header Accept *image/avif*
#            }
#        }
#        @ext_webp {
#            not {
#                header Accept *image/webp*
#            }
#        }
#        header Vary Accept
#
#        rewrite * /wp-content/uploads-webpc/{path}.{ext}{ext_avif}{ext_webp}
#
#        handle @ext_avif {
#            respond Content-Type image/avif
#            rewrite * {http.request.uri.path}
#            file_server {
#		root /home/deploy/wordpress.website.com/wp-content
#            }
#        }
#
#        handle @ext_webp {
#            respond Content-Type image/webp
#            rewrite * {http.request.uri.path}
#            file_server {
#		root /home/deploy/wordpress.website.com/wp-content
#            }
#        }
#
#        handle {
#            rewrite * {http.request.uri.path}
#            file_server {
#		root /home/deploy/wordpress.website.com/wp-content
#            }
#        }
#    }

@acceptsWebp {
	header Accept *image/webp*
	path_regexp webp ^(.+)\.(jpg|jpeg|png)$
}
handle @acceptsWebp {
	@hasWebp file {re.webp.1}.webp
	rewrite @hasWebp {re.webp.1}.webp
}

####################################
}

but apparently those are not doing what nginx is intending to do.

2. Error messages and/or full log output:

3. Caddy version:

v2.6.4

4. How I installed and ran Caddy:

5. Links to relevant resources:

Hmm, that’s funky. Maybe this:

@content path_regexp content /wp-content/(.+)\.(jpe?g|png|gif|webp)$
handle @content {
	@accept-avif header Accept *image/avif*
	vars @accept-avif ext-avif ".avif"
	vars ext-avif ""

	@accept-webp header Accept *image/webp*
	vars @accept-webp ext-webp ".webp"
	vars ext-webp ""

	header +Vary Accept
	header Cache-Control max-age=31536000

	try_files /wp-content/uploads-webpc/{re.content.1}.{re.content.2}{vars.ext-avif} /wp-content/uploads-webpc/{re.content.1}.{re.content.2}{vars.ext-webp} {path} =404
}

Francis, thanks a lot for this.
This solved it.

1 Like

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