darkweak / souin

An HTTP cache system, RFC compliant, compatible with @tyktechnologies, @traefik, @caddyserver, @go-chi, @bnkamalesh, @beego, @devfeel, @labstack, @gofiber, @go-goyave, @go-kratos, @gin-gonic, @roadrunner-server, @zalando, @zeromicro, @nginx and @apache
https://docs.souin.io
MIT License
721 stars 56 forks source link

Redirects Hangs: How to exclude redirects from Caching #491

Closed skrlance closed 7 months ago

skrlance commented 7 months ago

Souin cache is currently speeding up my website with Caddy server. After I saw that it caches redirects also and all redirects like 301 and 302 doesn't works, I have configured it in the following way:

    @match path /*/
    cache @match {
        ttl 14400s
        default_cache_control "public, max-age=86400"
    }
    @match1 path /deal/*/
    cache @match1 {
        ttl 14400s
        default_cache_control "public, max-age=86400"
    }

By this way, many other redirects are working excluding the mentioned paths. However, still on those match and match1 paths no 301 or 302 redirects works. So, I tried the following way in the global option:

    regex {
    exclude expression {http.status_code} == 301
    exclude expression {http.status_code} == 302
    }

However, the above code also didn't prevented the redirects from being cached.

So, I tried every other way on the server option like:

    @redirects {
    expression {http.status_code} == 301
    expression {http.status_code} == 302
    }
    cache exclude @redirects

But, still nothing is preventing redirects from being cached. What to do?

Please help me in this!

darkweak commented 7 months ago

Hello @skrlance thank you for your feedback. The regex directive is defined as the following Regexp that the URI should match to override the key generation so that's a regex on the path, not on anything in the request. AFAIK you cannot exclude the 301/302 http status codes from being cached because the RFC allows it.

skrlance commented 7 months ago

There is something more I need to say like you know almost 40% of web is powered by WordPress. There are lots of people like me ditching NGINX and running WordPress using Caddy 2 server. So, these days cache is main agenda in WordPress in case of performance. For that purpose, most people using Caddy server for WordPress will use Souin Cache also.

In this view point, I don't see you kept any example configuration of Souin Cache for WordPress website. So, this is why this problem has arise.

Can you please help all WordPress users keeping example configuration of Souin Cache in Caddy server for WordPress websites?

Thanks Kindly

darkweak commented 7 months ago

Let me a couple of hour to work on an example and add it to the https://docs.souin.io/docs/use-cases/ 🙂

skrlance commented 7 months ago

Keep on this location also:

https://github.com/darkweak/souin/tree/master/plugins/caddy/examples

Thanks

darkweak commented 7 months ago

@skrlance https://docs.souin.io/docs/uses-cases/wordpress/

skrlance commented 7 months ago

Where have you excluded the following things from cache?

    header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
    path_regexp "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(index)?.xml|[a-z0-9-]+-sitemap([0-9]+)?.xml)"
    method POST
    expression {query} != ''

Regards

darkweak commented 7 months ago

@skrlance I updated the documentation with the following Caddyfile

{
    default_sni {$SERVER_NAME}
    order cache before rewrite
    cache {
        api {
            souin
        }
    }
}

{$SERVER_NAME} {
+    @authorized-cache {
+        not header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
+        not path_regexp "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(index)?.xml|[a-z0-9-]+-sitemap([0-9]+)?.xml)"
+        not method POST
+        not expression {query} != ''
+    }
+    cache @authorized-cache
    root * /var/www/html
    encode zstd gzip

    php_fastcgi wordpress:9000
    file_server

    log {
        output file /var/log/caddy.log
    }

    header / {
        X-Frame-Options "SAMEORIGIN"
        X-Content-Type-Options "nosniff"
    }

}
skrlance commented 7 months ago

Oh I see! This is how it works! Thank you very much!!

skrlance commented 7 months ago

You can mark this issue resolved! But still if you find someway to exclude the redirects especially 301 and 302, please let us know!

darkweak commented 7 months ago

@skrlance If your upstream can return a header Cache-Control: no-cache it could do the trick.

skrlance commented 7 months ago

@skrlance If your upstream can return a header Cache-Control: no-cache it could do the trick.

Thanks! I will do that. Please mark this issue resolved!!