lua-resty-template icon indicating copy to clipboard operation
lua-resty-template copied to clipboard

wrong template returned when using multiple server blocks

Open craveica opened this issue 8 years ago • 9 comments

server {
    server_name a;
    set $template_root /etc/nginx/sites/a;
    location / {
        content_by_lua_file '/etc/nginx/sites/a/index.lua';
    }
}

server {
    server_name b;
    set $template_root /etc/nginx/sites/b;
    location / {
        content_by_lua_file '/etc/nginx/sites/b/index.lua';
    }
}

index.lua:
local template = require "resty.template"
template.render("test.html")

/etc/nginx/sites/a/test.html:
a

/etc/nginx/sites/b/test.html
b

--------------------------------
$ curl https://b/ ; curl https://a/
b
a
$ curl https://b/ ; curl https://a/
b
b <-- wrong

craveica avatar Feb 23 '17 13:02 craveica

yes, good catch, I think they are sharing the same cache key. This needs to be fixed somehow.

bungle avatar Feb 23 '17 16:02 bungle

@bungle You're right. The cache key is set to 'test.html' for both. As a workaround, I have to pass my own key to template.render.

template.render("test.html", {}, ngx.var.template_root .. "test.html")

I still think this should be fixed.

craveica avatar Feb 24 '17 11:02 craveica

Yes, it should. Have to figure out what would be a better way to auto generate cache key without affecting too much cache access times.

bungle avatar Feb 24 '17 14:02 bungle

@bungle Any news?

craveica avatar Sep 30 '19 07:09 craveica

@craveica In general I think it is an issue with globals. I think we just need to add support for template.new(opts) which return object similar to require "resty.template" but so that it is its own instance and does not share info with the global require "resty.template". I don't think there is another way to solve it.

bungle avatar Sep 30 '19 10:09 bungle

Fixed in v2.0.0. @bungle Thank you!

craveica avatar Feb 22 '20 14:02 craveica

@bungle It looks like when including templates {(partial_template)}, it does not use the cache_key provided. For example, server b will use included templates from server a, if server a was hit first. The main templates are cached correctly using specific cache_keys, but the partial templates not. Can you verify that?

craveica avatar Sep 17 '20 09:09 craveica

How about adding a new var like '$template_namespace' and use this value as cache_key's prefix?

Rifle-Rex avatar Mar 25 '21 09:03 Rifle-Rex

oh I have missed that, let me check it, and thank you for reporting.

bungle avatar Apr 20 '21 19:04 bungle