我正在使用 Lua 运行 NGINX 服务器(通过 OpenResty)。
我想实现一个非常简单的事情:
/
访问
startpage
/foo
,则从文件夹/html/foo
我尝试了很多方法,但似乎没有任何效果。我无法通过
ngx.var.document_root = "/startpage"
更改根文件夹 - 我收到错误。
然后我尝试了这个脚本,但我没有对网址进行内部重写,而是重定向到“/startpage”,因此用户可以在浏览器地址栏中看到“/startpage”,这是我不想要的。
-- Check if the script has already been executed - without this check, this script is called recursively
if rewritten ~= true then
rewritten = true
-- Root ("/") was called, set correct startpage
if ngx.var.uri == "/" then
ngx.req.set_uri("/startpage")
-- Otherwise, set correct path to the called resource
else
ngx.req.set_uri("/html"..ngx.var.uri)
end
end
如果有人能指出我正确的方向,我将非常感激。
具体目标是什么?
如果用户调用root
/
:
/startpage/user
/startpage/guest
Else(用户调用任何其他 URL):
/foo
:从 /html/foo
/foo/bar/baz
:从 /html/foo/bar/baz
身份验证逻辑全部正常工作,因此我知道用户是否已登录。我只需要设置动态提供文件的根文件夹。
你不需要 Lua 来实现这一点,
location
指令就足够了:
location = / {
alias ./startpage/;
try_files index.html =404;
}
location / {
root ./html/;
}