htaccess-for-nginx
htaccess-for-nginx copied to clipboard
Runtime error when using with OpenResty
I'm getting an error using htaccess.lua with openresty/1.21.4.1 under the following circumstances:
-
I'm using the latest
htaccess.luafrom this repository. I've placed it in/usr/local/openresty/nginx/scripts. -
I have a directory,
/usr/local/openresty/nginx/html/protected, which I'm trying to protect using.htaccess. -
The
.htaccessfile (/usr/local/openresty/nginx/html/protected/.htaccess) contents are:
AuthType Basic
AuthName "Restricted Content"
AuthUserFile .htpasswd
Require valid-user
- The nginx configuration (relevant portions) are:
http {
...
lua_shared_dict htaccess 4m;
...
}
server {
...
rewrite_by_lua_file /usr/local/openresty/nginx/scripts/htaccess.lua;
...
location / {
root html;
index index.html index.htm;
}
...
}
When the rewrite_by_lua_file line is commented out, I can access content in /protected, as expected. When not commented out, access to /protected/index.html leads to a Lua runtime error:
2023/05/30 09:15:43 [error] 18532#18532: *17 lua entry thread aborted: runtime error: /usr/local/openresty/nginx/scripts/htaccess.lua:119: attempt to concatenate global 'doc_root' (a nil value)
stack traceback:
coroutine 0:
/usr/local/openresty/nginx/scripts/htaccess.lua: in function 'ensure_doc_root'
/usr/local/openresty/nginx/scripts/htaccess.lua:145: in function 'get_file_contents'
/usr/local/openresty/nginx/scripts/htaccess.lua:507: in function 'parse_htaccess_directive'
/usr/local/openresty/nginx/scripts/htaccess.lua:1012: in main chunk, client: 146.90.194.232, server: foo.bar.com, request: "GET /protected/index.html HTTP/2.0", host: "foo.bar.com"
Any idea what could be the cause of the problem? It seems like in
https://github.com/e404/htaccess-for-nginx/blob/b81c1f1a4573839021284652c75e2a42d008d72a/htaccess.lua#L114-L118
there's a reference to doc_root, which is not defined globally and is local to in_doc_root.