如何将MySQL用于Nginx的动态文档根目录?
我一直在试图找到一种方法,首先捕获环境变量HOSTNAME,然后使用MySQL查询获取并返回到Nginx conf,即vhost的文档根。我们目前在Apache中使用它们作为动态文档根,但正在迁移到Nginx 示例nginx.conf(可能看起来像这样): 。。。。。如何将MySQL用于Nginx的动态文档根目录?,mysql,nginx,lua,openresty,Mysql,Nginx,Lua,Openresty,我一直在试图找到一种方法,首先捕获环境变量HOSTNAME,然后使用MySQL查询获取并返回到Nginx conf,即vhost的文档根。我们目前在Apache中使用它们作为动态文档根,但正在迁移到Nginx 示例nginx.conf(可能看起来像这样): 。。。。。 我正在探索使用Lua,但一直无法弄清楚如何将主机名和mysql查询作为变量捕获并返回结果。首先,使用数据库进行基本路由听起来不是一个好主意——我建议将结果缓存在内存中,并可能定期从数据库中刷新它们 第二,基本的Nginx配置文件到
我正在探索使用Lua,但一直无法弄清楚如何将主机名和mysql查询作为变量捕获并返回结果。首先,使用数据库进行基本路由听起来不是一个好主意——我建议将结果缓存在内存中,并可能定期从数据库中刷新它们 第二,基本的Nginx配置文件到目前为止还不能满足您的需要-为了获得更高级的功能,您需要在其上使用脚本语言(如Lua)。这允许您读取环境变量。我在这里写到了如何做到这一点:
让Lua在Nginx上工作的通常方法是使用Openresty,这是Nginx的一个版本,预装了几个模块,包括Lua模块。您可以将
luarestymysql
添加到混合中,然后直接从lua执行您想要的任何操作。我从未使用过LuaRestyMySQL,所以我无法为您编写代码。如果你打算使用Mysql,你必须研究它的文档。在您使用Redis时,不妨看看它,它可能比Mysql更合适。谢谢您的帮助。这对我来说不起作用,但经过大量的工作,我终于找到了一些有效的方法。这是给别人的,如果他们需要的话
原来$http_主机已经在nginx中全局定义了-所以这是固定的
set $httphost $http_host; # create and initialize var
set $docroot "";
# begin LUA scripting
rewrite_by_lua '
-- make sure http host is defined
if not ngx.var.httphost then
ngx.log(ngx.ERR,"ERROR - no httphost defined")
return
end
-- begin mysql
local mysql = require "resty.mysql"
local db, err = mysql:new()
db:set_timeout(1000) -- 1 sec
local ok, err, errno, sqlstate = db:connect
{
host = "127.0.0.1",
port = 3306,
database = "db",
user = "user",
password = "password",
max_packet_size = 1024 * 1024
}
if not ok then
ngx.log(ngx.ERR,"MySQL failed to connect: ", err, ": ", errno, " ", sqlstate)
return
end
-- prevent injection attack
local hname = ngx.unescape_uri(client)
local quoted_name = ngx.quote_sql_str(hname)
local sql = "select docroot from users where customer =" .. quoted_name
result,err,errno,sqlstate = db:query(sql,1)
if not result then
ngx.log(ngx.ERR,"MySQL bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
if not result[1].docroot then
ngx.log(ngx.ERR,"MySQL ERROR - no docroot was returned")
return
end
ngx.var.docroot = result[1].docroot
';
# now we can set the docroot for this host
root /var/www/$docroot;
set $httphost $http_host; # create and initialize var
set $docroot "";
# begin LUA scripting
rewrite_by_lua '
-- make sure http host is defined
if not ngx.var.httphost then
ngx.log(ngx.ERR,"ERROR - no httphost defined")
return
end
-- begin mysql
local mysql = require "resty.mysql"
local db, err = mysql:new()
db:set_timeout(1000) -- 1 sec
local ok, err, errno, sqlstate = db:connect
{
host = "127.0.0.1",
port = 3306,
database = "db",
user = "user",
password = "password",
max_packet_size = 1024 * 1024
}
if not ok then
ngx.log(ngx.ERR,"MySQL failed to connect: ", err, ": ", errno, " ", sqlstate)
return
end
-- prevent injection attack
local hname = ngx.unescape_uri(client)
local quoted_name = ngx.quote_sql_str(hname)
local sql = "select docroot from users where customer =" .. quoted_name
result,err,errno,sqlstate = db:query(sql,1)
if not result then
ngx.log(ngx.ERR,"MySQL bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
if not result[1].docroot then
ngx.log(ngx.ERR,"MySQL ERROR - no docroot was returned")
return
end
ngx.var.docroot = result[1].docroot
';
# now we can set the docroot for this host
root /var/www/$docroot;