将 Openresty 中的 content_by_lua_file 翻译成中文是“通过 Lua 文件提供内容”。

我试图在OpenResty中创建一个基本的hello word页面。如果我使用content_by_lua,它可以正常工作,但是当我尝试使用content_by_lua_file时,我得到了以下错误:

2015/01/22 13:52:35 [alert] 2183#0: lua_code_cache is off; this will hurt performance in /Users/lobster/documents/web_server/conf/nginx.conf:10
2015/01/22 13:52:38 [error] 2223#0: *4 failed to load external Lua file "/Users/lobster/documents/web_server/./lua/main.lua": cannot open /Users/lobster/documents/web_server/./lua/main.lua: Permission denied, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8080"

但这没有任何意义,因为我可以轻松更改 /Users/lobster/documents/web_server/lua/main.lua 文件。这是我的配置:

worker_processes  1;
error_log logs/error.log;

events {
    worker_connections 1024;
}
http {
    lua_package_path '/lua/main.lua;';
    server {
    lua_code_cache off;
        listen 8080;
        location / {
            default_type 'text/plain';
            content_by_lua_file ./lua/main.lua;
        }
    }
}

我从根目录开始启动nginx,这样nginx可以访问我的计算机上的任何文件。我做错了什么?

更新: 我使用了content_by_lua,并在其中使用了require来解决它。

点赞
用户7026131
用户7026131

我也遇到了这个问题,我通过在我的 nginx.conf 中添加:

user root root;

来解决它,因为我的 LUA 脚本文件的用户和组是 root。

此外,你也可以改变你的 LUA 脚本文件的所有者。

2016-10-16 09:04:02
用户6842080
用户6842080

通常情况下,nginx有两个进程,一个是主进程,另一个是工作进程。主进程由root用户运行,工作进程由nobody用户运行,因此您应该确保nobody用户可以读取/Users/lobster/documents/web_server/./lua/main.lua文件。

顺便说一句,rensike在nginx.conf中添加了user root root;,工作进程将由root用户运行,所以他用另一种方式解决了你的问题。

(注:markdown格式为英文格式)

2016-11-15 07:04:28