Nginx:如何将Python脚本的结果发送到客户端?

Nginx:如何将Python脚本的结果发送到客户端?,python,nginx,system-calls,nginx-location,nginx-config,Python,Nginx,System Calls,Nginx Location,Nginx Config,我有一个简单的python脚本:从给定的十六进制颜色生成一个x图标,然后返回一个有效的字节流(BytesIO) 我想得到这样的东西(请不要笑,我正在使用Nginx大约两天): location~^/icons/(?[a-fA-F0-9]{6})\.ico${ 使用'color'参数发送200(./favicon.py color)#系统调用到'favicon.py'。 } 有可能吗?以下配置应该可以完成这项工作: location ~^/icons/(?<colour>[a-fA-

我有一个简单的python脚本:从给定的十六进制颜色生成一个
x图标
,然后返回一个有效的字节流(
BytesIO

我想得到这样的东西(请不要笑,我正在使用Nginx大约两天):

location~^/icons/(?[a-fA-F0-9]{6})\.ico${
使用'color'参数发送200(./favicon.py color)#系统调用到'favicon.py'。
}

有可能吗?

以下配置应该可以完成这项工作:

location ~^/icons/(?<colour>[a-fA-F0-9]{6})\.ico$ {
    content_by_lua '
        local command = "./favicon.py colour"
        local handle = io.popen(command)
        local content = handle:read("*a")
        handle:close()
        ngx.print(content)
    ';
}
location~^/icons/(?[a-fA-F0-9]{6})\.ico${
内容由
本地命令=“./favicon.py color”
本地句柄=io.popen(命令)
本地内容=句柄:读取(“*a”)
句柄:close()
ngx.print(内容)
';
}
基本上,它使用Lua来执行和提供内容


注意:您的nginx应该使用lua模块进行编译,以使此解决方案正常工作

如果我在nginx中将
root
设置为
/some/path
/favicon.py
将扩展为
/some/path/favicon.py
,对吗?同时我在“^/icons/(?[a-fA-F0-9]中也发现了错误
缺失)
,但在我的配置中一切都是正确的:
位置^/icons/(?[a-fA-F0-9]{6})\.ico$
.1.不确定,我想它将使用nginx进程的运行目录,但它可能是定义的
根目录
或其他什么。您可以使用完整路径。2.我刚从您那里复制了位置,使用您需要的位置,而不是我编写的位置
location ~^/icons/(?<colour>[a-fA-F0-9]{6})\.ico$ {
    content_by_lua '
        local command = "./favicon.py colour"
        local handle = io.popen(command)
        local content = handle:read("*a")
        handle:close()
        ngx.print(content)
    ';
}