diego-treitos / nginx_cache_multipurge

31 stars 12 forks source link

Example 1: Mimic the commercial cache_purge doesn't work #6

Open iv1310 opened 4 years ago

iv1310 commented 4 years ago

When trying to implement cache with example 1, the server always returns response 200 without the content, and when trying to purge the content, the server always returns response 403. And after checking example 1, cmp_run_if variable not found in the location /, was it intentional?

My environment:

Debian 10 Nginx 1.17.10

Please help.

diego-treitos commented 4 years ago

The variable cmp_run_if should be available in the location if the map is in the same location than in the examples. I could confirm this in tests.

Regarding the example, after some tests I think it might be incorrect and that the proxy_pass directive has to go after the content_by_lua call.

midorinet commented 4 years ago

I have the same issue with Nginx 1.18.0

After moving proxy_pass after content_by_lua the content load succesfully. However executing curl -XPURGE http://ip-address/path is passing the PURGE request to my backend

Please find my full config

nginx.conf

map $request_method $cmp_run_if {
    default 0;
    PURGE 1;
}

proxy_cache_path  /data/nginx/cache levels=1:2 keys_zone=my-awesome-cache:8m;

vhosts

location / {
  proxy_cache my-awesome-cache;
  proxy_cache_key $scheme$host$uri$is_args$args;

  set $cmp_cache_key $scheme$host$uri$is_args$args;
  set $cmp_cache_path "/data/nginx/cache";
  content_by_lua_file /etc/nginx/lua/cache_multipurge.lua;
  proxy_pass http://small_light_server;
}

I also trying to remove all proxy_pass and proxy_key related just to check. And it seems I found error

2020/08/25 06:50:36 [error] 9324#9324: *118 lua entry thread aborted: runtime error: /usr/local/luajit/share/lua/5.1/md5.lua:20: attempt to call field 'sum' (a nil value)
stack traceback:
coroutine 0:
        /usr/local/luajit/share/lua/5.1/md5.lua: in function 'sumhexa'
        /etc/nginx/lua/cache_multipurge.lua:75: in function 'purge_one'
        /etc/nginx/lua/cache_multipurge.lua:94: in main chunk, client: 192.168.0.10, server: my.domain.com, request: "PURGE /assets/fonts/font.woff2 HTTP/1.1", host: "my.domain.com"
haarp commented 3 months ago

The problem is content_by_lua_file. openresty documentation says:

Do not use this directive and other content handler directives in the same location. For example, this directive and the proxy_pass directive should not be used in the same location.

I'm strongly assuming content_by_lua_block and content_by_lua_file are the same in this regard.

It works if you use a different call, such as rewrite_by_lua:

location /foo {
    # should come before proxy_pass
    rewrite_by_lua_file /opt/cache_multipurge.lua;

    proxy_pass http://mirror;
}