【问题标题】:NGINX virtual hosts with seperate lua_package_path variables具有单独 lua_package_path 变量的 NGINX 虚拟主机
【发布时间】:2017-01-30 17:17:40
【问题描述】:

我正在尝试将两个 (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. 如果您在server 块内定义lua_package_path,您将收到此错误:[emerg] "lua_package_path" directive is not allowed here in example.conf

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

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

【问题讨论】:

    标签: nginx configuration lua virtualhost openresty


    【解决方案1】:

    修复了它从 NGINX 配置中删除 lua_package_path(因为 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']
    
    ...
    

    这使我可以从与我的app.lua 相同的目录中读取config.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 根目录而不是我的应用程序根目录。即使在路径前面加上.

    【讨论】:

      【解决方案2】:

      几个月前我遇到了这个问题。 我不建议不要在同一台服务器上使用调试和发布项目。例如,您为(调试和发布)键启动一个 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';
                    }
                }
              }
      

      【讨论】:

      • 感谢您的指点,真的很有帮助!我完全同意在实时环境中分离生产和开发服务器。虽然在这种情况下,我的笔记本电脑上运行了 12 个虚拟主机,但我不想为此运行 12 个独立的虚拟服务器(或 NGINX 进程)。
      猜你喜欢
      • 2014-11-12
      • 2016-08-29
      • 2017-01-21
      • 1970-01-01
      • 2016-05-12
      • 2013-09-18
      • 1970-01-01
      • 2016-09-19
      • 2020-09-29
      相关资源
      最近更新 更多