V2 : help to set up a Yourls instance

1. My Caddy version (caddy version):

v2.0.0-beta.18 h1:N8CR8nO+n9Sk16CBx6ysVZWIZv0hgtU1J84st1bFWgI=

2. How I run Caddy:

a. System environment:

Ubuntu 18.04 on a public cloud instance from OVH.

b. Command:

I’m not sure what I’m supposed to put here…

c. Service/unit/compose file:

Standard caddy 2 service :

[Unit]
Description=Caddy Web Server
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
User=caddy
Group=caddy
ExecStart=/usr/local/bin/caddy run --config /etc/caddy/Caddyfile --adapter caddyfile --resume --environ
ExecReload=/usr/local/bin/caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile or JSON config:

{
    email email@email.com
}

(logs) {
    log {
        output file /var/log/caddy/access.log
    }    
}

(yourls) {
    php_fastcgi unix//run/php/php7.4-fpm-caddy.sock
    file_server 
    encode zstd gzip
    import logs

}

liens.macg.io {
    root * /var/www/yourls
    import yourls
}

liens2.macg.io {
    root * /var/www/yourls2
    import yourls
}

3. The problem I’m having:

I can’t manage to set up the url shortener service Yourls using Caddy 2.

Using the above configuration, I can access the admin interface fine, but the shorter links are all redirected to the administration rather than to their correct destination.

4. Error messages and/or full log output:

Access logs with the above configuration :

{"level":"info","ts":1584686119.7500317,"logger":"http.log.access.log0","msg":"handled request","request":{"method":"GET","uri":"/oTJuT","proto":"HTTP/2.0","remote_addr":"86.253.229.114:53180","host":"liens.macg.io","headers":{"Accept-Encoding":["gzip, deflate, br"],"Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; experimentation_subject_id=ImIyZjFmM2Y4LTU3OWItNDg2Yy1hYmU2LTFkZjQzNGMyNTE2MSI%3D--dbd7f97ae060fe4bfe8aa5ca427457726aeb76f4"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"],"Accept-Language":["fr-fr"]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"liens.macg.io"}},"common_log":"86.253.229.114 - - [20/Mar/2020:07:35:19 +0100] \"GET /oTJuT HTTP/2.0\" 302 0","latency":0.006397828,"size":0,"status":302,"resp_headers":{"Server":["Caddy"],"Status":["302 Found"],"Location":["https://liens.macg.io/admin"],"Content-Type":["text/html; charset=UTF-8"]}}
{"level":"info","ts":1584686119.7500317,"logger":"http.log.access.log0","msg":"handled request","request":{"method":"GET","uri":"/oTJuT","proto":"HTTP/2.0","remote_addr":"86.253.229.114:53180","host":"liens.macg.io","headers":{"Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; experimentation_subject_id=ImIyZjFmM2Y4LTU3OWItNDg2Yy1hYmU2LTFkZjQzNGMyNTE2MSI%3D--dbd7f97ae060fe4bfe8aa5ca427457726aeb76f4"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"],"Accept-Language":["fr-fr"],"Accept-Encoding":["gzip, deflate, br"]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"liens.macg.io"}},"common_log":"86.253.229.114 - - [20/Mar/2020:07:35:19 +0100] \"GET /oTJuT HTTP/2.0\" 302 0","latency":0.006397828,"size":0,"status":302,"resp_headers":{"Server":["Caddy"],"Status":["302 Found"],"Location":["https://liens.macg.io/admin"],"Content-Type":["text/html; charset=UTF-8"]}}
{"level":"info","ts":1584686119.7500317,"logger":"http.log.access.log0","msg":"handled request","request":{"method":"GET","uri":"/oTJuT","proto":"HTTP/2.0","remote_addr":"86.253.229.114:53180","host":"liens.macg.io","headers":{"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"],"Accept-Language":["fr-fr"],"Accept-Encoding":["gzip, deflate, br"],"Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; experimentation_subject_id=ImIyZjFmM2Y4LTU3OWItNDg2Yy1hYmU2LTFkZjQzNGMyNTE2MSI%3D--dbd7f97ae060fe4bfe8aa5ca427457726aeb76f4"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"liens.macg.io"}},"common_log":"86.253.229.114 - - [20/Mar/2020:07:35:19 +0100] \"GET /oTJuT HTTP/2.0\" 302 0","latency":0.006397828,"size":0,"status":302,"resp_headers":{"Server":["Caddy"],"Status":["302 Found"],"Location":["https://liens.macg.io/admin"],"Content-Type":["text/html; charset=UTF-8"]}}
{"level":"info","ts":1584686119.7908714,"logger":"http.log.access.log0","msg":"handled request","request":{"method":"GET","uri":"/admin","proto":"HTTP/2.0","remote_addr":"86.253.229.114:53180","host":"liens.macg.io","headers":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"],"Accept-Language":["fr-fr"],"Accept-Encoding":["gzip, deflate, br"],"Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; experimentation_subject_id=ImIyZjFmM2Y4LTU3OWItNDg2Yy1hYmU2LTFkZjQzNGMyNTE2MSI%3D--dbd7f97ae060fe4bfe8aa5ca427457726aeb76f4"]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"liens.macg.io"}},"common_log":"86.253.229.114 - - [20/Mar/2020:07:35:19 +0100] \"GET /admin HTTP/2.0\" 308 0","latency":0.000216081,"size":0,"status":308,"resp_headers":{"Server":["Caddy"],"Location":["/admin/"],"Content-Type":[]}}
{"level":"info","ts":1584686119.7908714,"logger":"http.log.access.log0","msg":"handled request","request":{"method":"GET","uri":"/admin","proto":"HTTP/2.0","remote_addr":"86.253.229.114:53180","host":"liens.macg.io","headers":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"],"Accept-Language":["fr-fr"],"Accept-Encoding":["gzip, deflate, br"],"Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; experimentation_subject_id=ImIyZjFmM2Y4LTU3OWItNDg2Yy1hYmU2LTFkZjQzNGMyNTE2MSI%3D--dbd7f97ae060fe4bfe8aa5ca427457726aeb76f4"]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"liens.macg.io"}},"common_log":"86.253.229.114 - - [20/Mar/2020:07:35:19 +0100] \"GET /admin HTTP/2.0\" 308 0","latency":0.000216081,"size":0,"status":308,"resp_headers":{"Content-Type":[],"Server":["Caddy"],"Location":["/admin/"]}}
{"level":"info","ts":1584686119.7908714,"logger":"http.log.access.log0","msg":"handled request","request":{"method":"GET","uri":"/admin","proto":"HTTP/2.0","remote_addr":"86.253.229.114:53180","host":"liens.macg.io","headers":{"Accept-Language":["fr-fr"],"Accept-Encoding":["gzip, deflate, br"],"Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; experimentation_subject_id=ImIyZjFmM2Y4LTU3OWItNDg2Yy1hYmU2LTFkZjQzNGMyNTE2MSI%3D--dbd7f97ae060fe4bfe8aa5ca427457726aeb76f4"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"liens.macg.io"}},"common_log":"86.253.229.114 - - [20/Mar/2020:07:35:19 +0100] \"GET /admin HTTP/2.0\" 308 0","latency":0.000216081,"size":0,"status":308,"resp_headers":{"Content-Type":[],"Server":["Caddy"],"Location":["/admin/"]}}
{"level":"info","ts":1584686119.8275764,"logger":"http.log.access.log0","msg":"handled request","request":{"method":"GET","uri":"/admin/","proto":"HTTP/2.0","remote_addr":"86.253.229.114:53180","host":"liens.macg.io","headers":{"Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; experimentation_subject_id=ImIyZjFmM2Y4LTU3OWItNDg2Yy1hYmU2LTFkZjQzNGMyNTE2MSI%3D--dbd7f97ae060fe4bfe8aa5ca427457726aeb76f4"],"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"],"Accept-Language":["fr-fr"],"Accept-Encoding":["gzip, deflate, br"]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"liens.macg.io"}},"common_log":"86.253.229.114 - - [20/Mar/2020:07:35:19 +0100] \"GET /admin/ HTTP/2.0\" 200 7267","latency":0.01506835,"size":7267,"status":200,"resp_headers":{"Content-Type":["text/html; charset=utf-8"],"Expires":["Thu, 23 Mar 1972 07:00:00 GMT"],"Last-Modified":["Fri, 20 Mar 2020 06:35:19 GMT"],"Content-Encoding":["gzip"],"Vary":["Accept-Encoding"],"Server":["Caddy"],"Pragma":["no-cache"],"Set-Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; expires=Fri, 27-Mar-2020 06:35:19 GMT; Max-Age=604800; path=/; domain=liens.macg.io; secure; HttpOnly"],"Cache-Control":["no-cache, must-revalidate, max-age=0"]}}
{"level":"info","ts":1584686119.8275764,"logger":"http.log.access.log0","msg":"handled request","request":{"method":"GET","uri":"/admin/","proto":"HTTP/2.0","remote_addr":"86.253.229.114:53180","host":"liens.macg.io","headers":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"],"Accept-Language":["fr-fr"],"Accept-Encoding":["gzip, deflate, br"],"Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; experimentation_subject_id=ImIyZjFmM2Y4LTU3OWItNDg2Yy1hYmU2LTFkZjQzNGMyNTE2MSI%3D--dbd7f97ae060fe4bfe8aa5ca427457726aeb76f4"]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"liens.macg.io"}},"common_log":"86.253.229.114 - - [20/Mar/2020:07:35:19 +0100] \"GET /admin/ HTTP/2.0\" 200 7267","latency":0.01506835,"size":7267,"status":200,"resp_headers":{"Set-Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; expires=Fri, 27-Mar-2020 06:35:19 GMT; Max-Age=604800; path=/; domain=liens.macg.io; secure; HttpOnly"],"Cache-Control":["no-cache, must-revalidate, max-age=0"],"Last-Modified":["Fri, 20 Mar 2020 06:35:19 GMT"],"Content-Encoding":["gzip"],"Vary":["Accept-Encoding"],"Server":["Caddy"],"Pragma":["no-cache"],"Content-Type":["text/html; charset=utf-8"],"Expires":["Thu, 23 Mar 1972 07:00:00 GMT"]}}
{"level":"info","ts":1584686119.8275764,"logger":"http.log.access.log0","msg":"handled request","request":{"method":"GET","uri":"/admin/","proto":"HTTP/2.0","remote_addr":"86.253.229.114:53180","host":"liens.macg.io","headers":{"Accept":["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"],"User-Agent":["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"],"Accept-Language":["fr-fr"],"Accept-Encoding":["gzip, deflate, br"],"Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; experimentation_subject_id=ImIyZjFmM2Y4LTU3OWItNDg2Yy1hYmU2LTFkZjQzNGMyNTE2MSI%3D--dbd7f97ae060fe4bfe8aa5ca427457726aeb76f4"]},"tls":{"resumed":true,"version":772,"ciphersuite":4865,"proto":"h2","proto_mutual":true,"server_name":"liens.macg.io"}},"common_log":"86.253.229.114 - - [20/Mar/2020:07:35:19 +0100] \"GET /admin/ HTTP/2.0\" 200 7267","latency":0.01506835,"size":7267,"status":200,"resp_headers":{"Content-Encoding":["gzip"],"Vary":["Accept-Encoding"],"Server":["Caddy"],"Pragma":["no-cache"],"Content-Type":["text/html; charset=utf-8"],"Expires":["Thu, 23 Mar 1972 07:00:00 GMT"],"Last-Modified":["Fri, 20 Mar 2020 06:35:19 GMT"],"Set-Cookie":["yourls_20e6eb59858c3b25aed1109e5dd21ab1=abd9927d0100164f4263b438fff52fda; expires=Fri, 27-Mar-2020 06:35:19 GMT; Max-Age=604800; path=/; domain=liens.macg.io; secure; HttpOnly"],"Cache-Control":["no-cache, must-revalidate, max-age=0"]}}

5. What I already tried:

Here’s what I had for Caddy 1 :

(yourls) {
    log /var/log/caddy/yourls.access.log
    errors /var/log/caddy/yourls.error.log
    
    fastcgi / /var/run/php/php7.0-fpm.sock php 
    rewrite / {
        r (.*)
        to {uri} {uri}/ /yourls-loader.php?{query}
    }

    gzip

}

liens.macg.io {
    import yourls
    root /var/www/yourls
}

liens2.macg.io {
    import yourls
    root /var/www/yourls2
}

It worked perfectly then.

I tried to reproduce the rewrite from Caddy 1 which worked fine. Since it’s not possible to just reproduce it on Caddy 2, I guess I don’t know how to do this.

Here’s what I tried first :

(yourls) {
    php_fastcgi unix//run/php/php7.4-fpm-caddy.sock
    file_server 
    encode zstd gzip
    import logs
    try_files {path} {path}/ /yourls-loader.php?{query}&p={path}
}

Now, the links are correctly expanded, but the admin is inaccessible. It loops itself on a series of redirects and the admin is never displayed.

So I added this :

(yourls) {
    php_fastcgi unix//run/php/php7.4-fpm-caddy.sock
    file_server 
    encode zstd gzip
    import logs
    rewrite /admin/* /index.php?{query}&p={path}
    try_files {path} {path}/ /yourls-loader.php?{query}&p={path}
}

It was the same result.

6. Links to relevant resources:

Here’s the official recommended nginx configuration for Yourls : https://github.com/YOURLS/YOURLS/wiki/Nginx-configuration

Thanks for your help !

Looks like yourls doesn’t use index.php as an entrypoint from what I gather.

See https://caddyserver.com/docs/caddyfile/directives/php_fastcgi#expanded-form

You can try using this expanded syntax instead of php_fastcgi and modify the try_files directive to fit your needs

1 Like

Thanks for your help, but I don’t understand what I’m supposed to do.

I tried this :

(yourls) {

    # Add trailing slash for directory requests
    @canonicalPath {
        file {
            try_files {path}/yourls-loader.php
        }
        not {
            path */
        }
    }
    redir @canonicalPath {path}/ 308

    # If the requested file does not exist, try index files
    try_files {path} {path}/index.php index.php

    # Proxy PHP files to the FastCGI responder
    @phpFiles {
        path *.php
    }
    reverse_proxy @phpFiles unix//run/php/php7.4-fpm-caddy.sock {
        transport fastcgi {
            split .php
        }
    }

    file_server 
    encode zstd gzip
    import logs
}

But is was exactly the same as with the try_files I tried earlier. The links were redirected, but the admin is not accessible. What should I do ?

I don’t want to go back to Caddy 1, but at least it worked perfectly there.

I searched Yourls and found the settings in Nginx:

  # Rewrites
  location / {
    try_files $uri $uri/ /yourls-loader.php$is_args$args;
  }

Caddy 2 also has try_files:

Therefore:

try_files {path} {path}/ /yourls-loader.php

or

try_files {path} {path}/ /yourls-loader.php?{query}

Similar to the setting in Caddyfile of Caddy v1

try_files {uri} {uri}/ /yourls-loader.php?{query}

Add this to the (yourls) of Caddyfile and see if it works as expected.

Yes, that’s what I tried, as explained in the first message… :slight_smile:

The links definitely need this try_files line, but the admin stops working completely when I use it.

You add &p={path} without understanding. What happens if delete it?

Note: If you test the transfer, the cache will remain. Use Incognito Window if you are using Chrome:
https://support.google.com/chrome/answer/95464

I tried both, and same result.

Capture d’écran 2020-03-23 à 10.01.29

Caddy logs confirm it keeps redirecting to the same URL on a loop.

Maybe it’s a bug in Caddy 2.
I’ve found that it ignores settings like try_files and references index.php.
Settings provided by @francislavoie are also ignored.


Edit: There was a Glitch link here but it was removed


php_fastcgi of Caddy 2 has the following specifications different from Caddy 1:

  • Run the specified PHP file if it exists
  • If not, run index.php

Yourls seems to be able to customize index.php

So, generate the following index.php:

<?php
$uri = $_SERVER['REQUEST_URI'];

if ( ( $uri != "/" ) &&
     ( $uri != "/index.php" ) )
{
    require "yourls-loader.php";

    exit;
}

// Describe index.php to be output after this

Delete rewrite and try_files from Caddyfile. What about this?

Yourls already has an index.php file :

<?php
/**
 * index.php from Redirect Index
 * https://github.com/tomslominski/yourls-redirect-index
 *
 * Make sure to copy this into the base directory of your YOURLS installation!
 */

// Start YOURLS engine
require_once( dirname(__FILE__) . '/includes/load-yourls.php' );

// Write URL to variable
$redir_url = yourls_get_option( 'redirindex_url' );

// Perform the redirect
if( ( yourls_is_active_plugin( 'yourls-redirect-index/plugin.php' ) || yourls_is_active_plugin( 'redirect-index/plugin.php' ) ) && !empty( $redir_url ) ) {
        Header( "Location: $redir_url" );
} else {
        echo '<h2>' . yourls__( 'YOURLS Redirect Index', 'redirindex' ) . '</h2>';
        echo '<p>' . yourls__( 'Have you enabled the plugin and set the redirect URL in the YOURLS admin panel? If you\'re having trouble, check out the <a href="https://github.com/tomslomi$
}

?>

If I delete the rewrite and try_files, the admin works again, but not the redirects for the links.

With admin working, put this above // Start YOURLS engine in index.php

$uri = $_SERVER['REQUEST_URI'];

if ( ( $uri != "/" ) &&
     ( $uri != "/index.php" ) )
{
    require "yourls-loader.php";

    exit;
}
1 Like

OK, by adding that and deleting the rewrite and try_files, it works indeed !

Thanks for your help, but it’s not perfect either, I will have to manually edit this file at each YOURLS update. If it’s indeed a bug in Caddy 2, I hope it will be fixed.

I might add this to a Github issue, but I’d probably leave it to those who can judge this,
as they will see it.

Could you post your final Caddyfile? I’d like to see exactly what it looked like in the end.

I think this might be the setup you need:

# Add trailing slash for directory requests
@canonicalPath {
	file {
		try_files {path}/index.php
	}
	not {
		path */
	}
}
redir @canonicalPath {path}/ 308

# If the requested file does not exist, try index files
try_files {path} {path}/index.php yourls-loader.php

# Proxy PHP files to the FastCGI responder
@phpFiles {
	path *.php
}
reverse_proxy @phpFiles unix//run/php/php7.4-fpm-caddy.sock {
	transport fastcgi {
		split .php
	}
}

The difference is the the try_files in the middle. This will still allow requests to admin/* and route those through admin/index.php.

Essentially the rule reads like this:

  • First check if the requested path exists as a directory on disk, and if it does has an index.php. If so, redirect to that directory with a trailing slash.
  • Next, check if the path exists on disk exactly. This is to handle any static assets like .js and .css files for example. If so, serves that (file_server is what serves that).
  • Next, check if the path is a directory with an index.php in it. If so, rewrites the path to the index.php in that directory (then reverse_proxy will serve this request because it matches path *.php).
  • Next, just rewrite anything else to yourls-loader.php (and also gets served through reverse_proxy for the same reason).
1 Like

Here it is right now :


(logs) {
    log {
        output file /var/log/caddy/access.log
    }    
}

# Configuration Liens
(yourls) {
    php_fastcgi unix//run/php/php7.4-fpm-caddy.sock
    file_server 
    encode zstd gzip
    import logs
}

liens.macg.io {
    root * /var/www/yourls
    import yourls
}

liens2.macg.io {
    root * /var/www/yourls2
    import yourls
}

I’m pretty sure I tried this setup this morning, but it was broking both the admin and the redirection of links.

What I wrote is different than what you wrote here:

Do my mind trying it again? Also don’t forget to rename or delete the index.php you added, to get it out of the way.

1 Like

OK you’re right, either I had not tried this setup, or tried it slightly differently. Anyway, you’re suggestion works, without editing the index.php file.

So, if you want to set up Caddy 2 for Yourls, here’s the correct Caddyfile you need :

domain.com {
    root * /var/www/yourls
    @canonicalPath {
        file {
            try_files {path}/index.php
        }
        not {
            path */
        }
    }
    redir @canonicalPath {path}/ 308

    # If the requested file does not exist, try index files
    try_files {path} {path}/index.php yourls-loader.php

    # Proxy PHP files to the FastCGI responder
    @phpFiles {
        path *.php
    }
    reverse_proxy @phpFiles unix//run/php/php7.4-fpm-caddy.sock {
        transport fastcgi {
            split .php
        }
    }

    file_server 
    encode zstd gzip
}

Thanks for your help. :+1:

EDIT : There is a simpler solution, if you’re using Caddy v2 beta 20 and above. Here’s what you need :

domain.com {
    root * /var/www/yourls
    route {
        try_files {path} {path}/index.php yourls-loader.php
        php_fastcgi unix//run/php/php7.4-fpm-caddy.sock
    }
    file_server 
    encode zstd gzip
}
3 Likes

I was just thinking about this a bit more… I have another idea that might be much simpler, I’m curious if you could give it a shot real quick to verify.

route {
    try_files {path} {path}/index.php yourls-loader.php
    php_fastcgi unix//run/php/php7.4-fpm-caddy.sock
}

So, the route directive defines a block that is guaranteed to be executed in the order the contents are listed. What I think should happen with this config is that this will effectively override the try_files behaviour built into php_fastcgi by defaulting to yourls-loader.php instead of a non-existent index.php.

If this works for you, we’ll likely add this sort of example to the documentation as it’s starting to seem like a common pattern.

2 Likes

This is because Glitch’s PHP uses a built-in server,
PHP behavior has priority over Caddy.
(If there is no file, the build-in server refers to index.php or index.html)
I should have done a Caddyfile investigation in another environment. :persevere: