0

我正在尝试创建一个有条件的 content_by_lua 脚本,其中的内容应该由 lua 仅在 turthy 条件下设置。

例子:

nginx.conf

location / {
        content_by_lua_file  /nginx/lua/nginx.lua;

        root   /nginx/www;
        index  index.html;

        location ~* \.(?:ico|css|js|gif|jpe?g|png|woff|ttf)$ {
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
    }

nginx.lua

if condition then
    ngx.header["Content-type"] = "text/html"
    ngx.say('<H1>Hello World.</H1>');
    ngx.exit(0)
else
    -- serve the original content (index.html)
end 

问题是 - nginx 下的 lua 脚本不支持同一路由中的 2 个内容指令,有没有我可以做的解决方法?

在条件为假时使用当前用法,我希望显示 index.html 但会收到一个空白页面

4

1 回答 1

1

您可以ngx.exec拨打内部电话。

nginx.conf

location / {
    content_by_lua_file  /nginx/lua/nginx.lua;

    root   /nginx/www;
    index  index.html;

    location ~* \.(?:ico|css|js|gif|jpe?g|png|woff|ttf)$ {
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }
}

location /default_index {
    root   /nginx/www;
    index  index.html;
}

nginx.lua

if condition then
    ngx.header["Content-type"] = "text/html"
    ngx.say('<H1>Hello World.</H1>');
    ngx.exit(0)
else
    -- serve the original content (index.html)
    ngx.exec("/default_index", ngx.var.args)
end 
于 2015-12-18T17:37:01.133 回答