sillygod / cdp-cache

a caddy 2 proxy cache plugin
MIT License
119 stars 17 forks source link

Does `match_path` support wildcard or regex ? #17

Closed chinajuanbob closed 4 years ago

chinajuanbob commented 4 years ago

When working with file_server browse of the caddy, I only want files (or the ones of certain types) to be cached, but not the directories.

Does match_path support it?

Thanks!

sillygod commented 4 years ago

Hi @chinajuanbob,

Maybe, you can use match_heaer to achieve your goal.

ex.

:9991  {

    reverse_proxy {
        to localhost:9995
    }

    http_cache {

        cache_type in_memory
        match_path /
        match_header Content-Type image/jpg image/png "text/plain; charset=utf-8"

    }

However, I found the match_header doesn't work correctly. I will do a fix for it soon.

sillygod commented 4 years ago

@chinajuanbob ,

I've created the latest release 0.3.4. You can use match_header to filter the content types you want to cache.

chinajuanbob commented 4 years ago

hit this:

:2020 {
    reverse_proxy {
        to localhost:2021
    }
    http_cache {
        cache_type in_memory 
        match_path /
        match_header Content-Type image/jpg image/png "text/plain; charset=utf-8"
    }
}
$ curl -I http://localhost:2021/uploadtest    
HTTP/1.1 404 Not Found
Server: Caddy
Date: Tue, 23 Jun 2020 10:29:50 GMT

$ curl -I http://localhost:2020/uploadtest    
HTTP/1.1 200 OK <------------------------ should be 404 here
Date: Tue, 23 Jun 2020 10:29:55 GMT
Server: Caddy
X-Cache-Status: skip
sillygod commented 4 years ago

Oops, I know the root cause! I will make a fix for this.

sillygod commented 4 years ago

Hi @chinajuanbob ,

Thanks for testing it. I've pushed a fix for the problem. the latest release is v0.3.6

chinajuanbob commented 4 years ago

The 404 issue is gone, however, hit this:

:2020 {
    reverse_proxy {
        to localhost:2021
    }
    http_cache {
        cache_type in_memory 
        match_path /
        match_header Content-Type image/jpeg
    }
}
$ curl -I http://localhost:2021/uploadtest.jpg
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 5
Content-Type: image/jpeg
Etag: "qcf73l5"
Last-Modified: Wed, 24 Jun 2020 07:50:09 GMT
Server: Caddy
Date: Wed, 24 Jun 2020 07:50:16 GMT

$ curl -I http://localhost:2020/uploadtest.jpg
HTTP/1.1 200 OK
Accept-Ranges: bytes
Accept-Ranges: bytes. <-------- duplicated
Content-Length: 5
Content-Length: 5
Content-Type: image/jpeg
Content-Type: image/jpeg
Date: Wed, 24 Jun 2020 07:50:09 GMT
Date: Wed, 24 Jun 2020 07:50:21 GMT
Etag: "qcf73l5"
Etag: "qcf73l5"
Last-Modified: Wed, 24 Jun 2020 07:50:09 GMT
Last-Modified: Wed, 24 Jun 2020 07:50:09 GMT
Server: Caddy
X-Cache-Status: miss <--------- keep 'miss' after request for multiple times 
sillygod commented 4 years ago

@chinajuanbob ,

~Do you know what curl -I does internally? If I just use curl [URL], it will work well.~ OK, this will issue a HTTP HEAD method.

There is some unexpected behavior when receiving the request with the head method. I push a fix about it. the latest release is v0.3.7. Thanks for finding this extra bugs.

sillygod commented 4 years ago

Hi @chinajuanbob ,

Did you encounter any problem with the latest release? If no, I will close this issue a few days later.

chinajuanbob commented 4 years ago

Thanks @sillygod for the quick fix! I just try the case again and the problem is gone. We could close it for now.