wrong template returned when using multiple server blocks
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
yes, good catch, I think they are sharing the same cache key. This needs to be fixed somehow.
@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.
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 Any news?
@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.
Fixed in v2.0.0. @bungle Thank you!
@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?
How about adding a new var like '$template_namespace' and use this value as cache_key's prefix?
oh I have missed that, let me check it, and thank you for reporting.