我正在使用OpenResty Nginx,并在Nginx conf中有一段Lua脚本。 (下面的样本,但为简洁而截断)。这在nginx.conf文件中正常工作。
rewrite_by_lua '
-- Load Memcached module
local memcached = require "resty.memcached"
local memc, err = memcached:new()
if not memc then
ngx.log(ngx.ERR, "Failed to instantiate memc: ", err)
return
end
'
但是,如果我将此脚本解压缩到文件memcached.lua
然后将其包含在带有rewrite_by_lua_file
的nginx配置中,我将无法重新启动nginx并收到有关语法错误的通知,例如:
unexpected "," in /etc/nginx/sites-enabled/memcached.lua
unknown directive "--" in /etc/nginx/sites-enabled/memcached.lua
如果我完全删除了ngx.log行,语法错误就会消失。
为什么语法在nginx conf中有效,但是当我将其提取到单独的文件时却没有?我在某处读过字节码,但不确定这是否相关?
这是.lua文件的完整输出。如果它通过rewrite_by_lua
cat /etc/nginx/sites-enabled/memcached.lua
-- Define Memcached key
local memcached_key = "saas_" .. ngx.var.host
-- Load Memcached module
local memcached = require "resty.memcached"
local memc, err = memcached:new()
if not memc then
ngx.log(ngx.ERR, "Failed to instantiate memc: ")
end
-- Set Memcached timeout
memc:set_timeout(1000)
-- Connect to Memcached daemon
local ok, err = memc:connect("127.0.0.1", 11211)
if not ok then
ngx.log(ngx.ALERT, "Failed to connect to Memcached: ", err)
end
-- Query for branch name in Memcached
local branch, flags, err = memc:get(memcached_key)
if err then
ngx.log(ngx.NOTICE, "Failed to get account from Memcached: ", err)
end
-- If branch not found in Memcached, ask MySQL instead
if not branch then
-- Load MySQL module
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
ngx.log(ngx.ALERT, "Failed to instantiate MySQL: ", err)
return
end
-- Set MySQL timeout
db:set_timeout(1000) -- 1 sec
-- Connect to MySQL daemon
local ok, err, errno, sqlstate = db:connect{
path = "/var/run/mysqld/mysqld.sock",
database = "dbname",
user = "dbuser",
password = "dbpass" }
if not ok then
ngx.log(ngx.ALERT, "Failed to connect to DB: ", err, ": ", errno, " ", sqlstate)
return
end
-- Define subdomain string by extracting it from the http hostname
local subdomain = string.match(ngx.var.host, "([%w-]+).")
local quoted_subdomain = ngx.quote_sql_str(subdomain)
-- Execute an SQL query to get the branch name based on the subdomain
local sql = "SELECT branch FROM saas_account WHERE account = " .. quoted_subdomain
local res, err, errno, sqlstate = db:query(sql, 1)
if not res then
ngx.log(ngx.ALERT, "Bad result from DB: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
-- Check we got a result
if not res[1] then
ngx.log(ngx.NOTICE, "No docroot was returned for subdomain: ", subdomain)
ngx.var.branch = "chaletops"
-- Assign the result from MySQL to the Nginx variable
else
ngx.var.branch = res[1].branch
-- Store the result from MySQL back in to Memcached with a lifetime of 1 hour (3600 seconds)
local ok, err = memc:set(memcached_key, ngx.var.branch, 3600)
if not ok then
ngx.log(ngx.WARN, "Failed to set branch in Memcached: ", err)
-- return
end
end
-- Save MySQL KeepAlive connection
local ok, err = db:set_keepalive(10000, 100)
if not ok then
ngx.log(ngx.ALERT, "Cannot set MySQL keepalive: ", err)
return
end
ngx.log(ngx.NOTICE, "Loading branch from MySQL: ", ngx.var.branch)
-- Else we did get a result from Memcached
else
-- Assign the result from Memcached to the Nginx variable
ngx.var.branch = branch
ngx.log(ngx.NOTICE, "Loading branch from Memcached: ", ngx.var.branch)
end
-- Save Memcached KeepAlive connection
local ok, err = memc:set_keepalive(10000, 100)
if not ok then
ngx.log(ngx.ALERT, "Cannot set Memcached keepalive: ", err)
-- return
end
TL:DR - 将.lua文件放在Nginx配置目录之外。
在被要求发布确切的错误消息后,我不得不重新创建问题。对于参考和Google搜索者,错误消息如下:
2015/12/17 10:04:02 [emerg] 6903#0: unexpected "," in /etc/nginx/sites-enabled/memcached.lua:18
这次我从我的nginx.conf中复制了Lua代码并将其放在一个名为memcached.lua
的文件中,并将其保存在我的项目目录中。然后我将以下include添加到我的nginx.conf中代替Lua脚本:
rewrite_by_lua_file /path/to/my/project/memcached.lua;
立即开始工作!
以前我通过将它从我的项目目录中的符号链接到/etc/nginx/sites-enabled/memcached.lua
来包含它(假设它实际上与包含它的.conf一起生活更好)然后只使用了rewrite_by_lua_file memcached.lua;
因为它是相对的.conf文件。
所以我最初想知道是不是因为rewrite_by_lua_file
函数不能遵循符号链接或其他东西,但后来我突然意识到它与Lua include语句无关,但显然(现在!)因为符号链接在/etc/nginx/sites-enabled
中,因此Nginx自动尝试加载文件,就好像它是一个普通的Nginx配置文件一样。
这是因为Nginx(在Ubuntu / Linux Mint上)配置为使用路径/etc/nginx/sites-enabled/*
导入所有配置文件到它的主配置。
我认为我犯这个简单错误的原因是,在更高版本的Ubuntu上使用Apache时,会发生类似的过程,但是对于Apache(我更习惯),你有一个/etc/nginx/sites-enabled/*.conf
的包含路径 - 即它特别需要.conf文件扩展名,这就是我错误地认为Nginx不会尝试包含我的.lua文件的原因。