0

我正在尝试将两个(Openresty)Lua Web 应用程序作为来自 NGINX 的虚拟主机提供服务,它们都需要自己独特的lua_package_path,但很难正确配置。

# Failing example.conf

http {
  lua_package_path = "/path/to/app/?.lua;;"

  server{
    listen       80;
    server_name  example.org
  }
}

http {    
  lua_package_path = "/path/to/dev_app/?.lua;;"

  server{
    listen       80;
    server_name  dev.example.org
  }
}
  1. 如果您定义http两次(每个主机一次),您将收到此错误:[emerg] "http" directive is duplicate in example.conf

  2. 如果你定义块lua_package_path内部server,你会收到这个错误:[emerg] "lua_package_path" directive is not allowed here in example.conf

  3. 如果您lua_package_path在一个块中定义两次http(无论如何这没有任何意义),您将收到此错误:[emerg] "lua_package_path" directive is duplicate in example.conf

为多个(Openresty)Lua 应用程序提供服务的最佳实践是什么,它们lua_package_path是同一 IP 和端口上的虚拟主机?

4

2 回答 2

3

几个月前我遇到了这个问题。我不建议不要在同一台服务器上使用调试和发布项目。例如,您为(调试和发布)键启动一个 nginx 应用程序可能会导致意外行为。但是,尽管如此,您可以设置:

  1. package.path = './mylib/?.lua;' .. package.path在 lua 脚本中。
  2. 您可以设置自己的local DEBUG = false状态并在应用程序内部进行管理。
  3. 显然,使用另一台机器进行调试。海事组织,最好的解决方案。
  4. 执行不同的my.release.luamy.debug.lua文件:
http {
          lua_package_path "./lua/?.lua;/etc/nginx/lua/?.lua;;";



        server{
            listen       80;
            server_name  dev.example.org;
              lua_code_cache off;


        location / {
                default_type text/html;
                content_by_lua_file './lua/my.debug.lua';
              }
        }
        server{
            listen       80;
            server_name  example.org

        location / {
                default_type text/html;
                content_by_lua_file './lua/my.release.lua';
              }
          }
        }
于 2016-09-22T10:09:33.810 回答
0

修复了它lua_package_path从 NGINX 配置中删除的问题(因为 OpenResty 包已经负责加载包)并将我指向我content_by_lua_file的应用程序的绝对完整路径:/var/www/app/app.lua

# example.conf

http {

  server{
    listen       80;
    server_name  example.org

    location / {
      content_by_lua_file '/var/www/app/app.lua';
    }
  }

  server{
    listen       80;
    server_name  dev.example.org

    location / {
      content_by_lua_file '/var/www/app_dev/app.lua';
    }

  }
}

之后,我将其包含在app.lua文件的顶部:

-- app.lua

-- Get the current path of app.lua
local function script_path()
   local str = debug.getinfo(2, "S").source:sub(2)
   return str:match("(.*/)")
end

-- Add the current path to the package path
package.path = script_path() .. '?.lua;' .. package.path

-- Load the config.lua package
local config = require("config")

-- Use the config
config.env()['redis']['host']

...

这使我可以config.lua从与我相同的目录中读取app.lua

-- config.lua

module('config', package.seeall)

function env()
  return {
    env="development",
    redis={
      host="127.0.0.1",
      port="6379"
    }
  }
end

使用它,我现在可以使用多个具有自己的包路径的虚拟主机。

@Vyacheslav 感谢您提供指向package.path = './mylib/?.lua;' .. package.path!那真的很有帮助!不幸的是,它也一直使用 NGINX conf 根目录而不是我的应用程序根目录。即使在路径前面加上.了。

于 2016-09-22T11:23:27.897 回答