通过 FastCGI 在 ngninx 上运行 WSAPI

通过 FastCGI 在 ngninx 上运行 WSAPI

使用通过 luarocks 安装的带有 wsapi 1.6-1 和 wsapi-fcgi 1.6-1 的 Lua 5.2,我有以下 nginx 配置:

server {
  listen       127.0.0.1:8080;
  server_name  localhost;

  location / {
    root   /home/petsagouris/code/wsapitest/;
    index  index.html index.htm run.lua;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
  # Lua WSAPI
  # taken from http://mascarenhas.github.io/2009/10/24/wsapi-nginx-fcgi.html
  location ~ ^(.+\.lua)(.*)$ {
    root html;
    fastcgi_pass 127.0.0.1:9100;
    fastcgi_index run.lua;
    fastcgi_split_path_info ^(.+\.lua)(.*)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

启动器脚本是这样的:

#!/usr/bin/env lua
# filename: run.lua

local fastcgi = require"wsapi.fastcgi"
local app = require"app"
fastcgi.run(app.run)

实际的应用程序是这样的:

#!/usr/bin/env wsapi.cgi
# filename: app.lua
local coroutine = require "coroutine"

local M= {}
_ENV = M

function run(wsapi_env)
  local headers = { ["Content-type"] = "text/html" }

  local function hello_text()
    coroutine.yield("<html><body>")
    coroutine.yield("<p>Hello Wsapi!</p>")
    coroutine.yield("<p>PATH_INFO: " .. wsapi_env.PATH_INFO .. "</p>")
    coroutine.yield("<p>SCRIPT_NAME: " .. wsapi_env.SCRIPT_NAME .. "</p>")
    coroutine.yield("</body></html>")
  end

  return 200, headers, coroutine.wrap(hello_text)
end

return M

它们两个目前都是可执行的(我是否只需要可执行,或者我必须保持两者一样?)并且当我执行或时,run.lua我可以从命令行获得正确的响应$ ./run.lua$ ./app.lua

当我访问时,localhost:8080我收到“502 Bad Gateway”响应和以下日志行:

2013/11/19 09:02:51 [error] 31359#0: *26 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9100", host: "localhost:8080"

我觉得很奇怪,我可以通过命令行让脚本正常工作,但从服务器访问时却不起作用。有谁能帮我解决这个问题?

答案1

我不熟悉 WSAPI,但您是否尝试过使用 spawn-fcgi 运行 run.lua(run.lua 必须是可执行的):

/usr/bin/spawn-fcgi -f run.lua -a 127.0.0.1 -p 9100 -P /var/run/fcgi.pid

相关内容