[nginx] nginx + spawn-fcgi + wsapi/lua(搭建lua的web dev平台)

来源:互联网 发布:yum y什么意思 编辑:程序博客网 时间:2024/06/05 16:18
nginx, web server
spawn-fcgi, 一个 fastcgi wrapper
wsapi.fcgi, WSAPI是针对lua的web标准,wsapi.fcgi就是把fcgi请求变成wsapi协议。

FreeBSD 8.1

安装 wsapi-fcgi
/usr/ports/www/fcgi
# pkg_add -r fcgi-devkit

# luarocks install wsapi-fcgi

安装 spawn-fcgi
/usr/ports/www/spawn-fcgi
# pkg_add -r spawn-fcgi

启动 spawn-fcgi
spawn-fcgi -F 1 -s /var/run/nginx-fcgi.sock -P /var/run/nginx-fcgi.pid -U nobody -- /usr/local/bin/wsapi.fcgi 

安装nginx
/usr/ports/devel/pcre
# pkg_add -r pcre

下载 nginx-0.8.54
# ./configure && gmake install

---------------- nginx.conf ----------------
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen 8080;
        server_name localhost;

        location / {
            fastcgi_pass unix:/var/run/nginx-fcgi.sock;
            fastcgi_index hello.lua;
            fastcgi_param SCRIPT_FILENAME /home/kasicass/sandbox/kepler/wsapi$fastcgi_script_name;
        }
    }
}
-----------------------------------------------

启动 nginx
# /usr/local/nginx/sbin/nginx

WSAPI Hello World Program
------------------ hello.lua ---------------------
#!/usr/bin/env wsapi.cgi

module(..., package.seeall)

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
---------------------------------------------------

然后通过浏览器访问:
http://127.0.0.1/hello.lua

碰到的问题
spawn-fcgi 中的 "-U nobody" 参数,让 spawn-fcgi 以 nobody 权限启动。否则 nginx worker (nobody权限) 无法正确 connect()。
http://serverfault.com/questions/101983/nginx-php-fastcgi-fails-how-to-debug

nginx.conf 配置中 "fastcgi_index hello.lua;" 让 127.0.0.1/ 的访问变成 127.0.0.1/hello.lua

参考资料
老外的一篇 lua + nginx + fastcgi 的配置资料
http://davelahn.com/lua-nginx-fastcgi-in-debian

nginx + fastcgi + spawn-fcgi + mysql
http://www.akii.org/ubuntu-9-10-install-nginx-fastcgi-spawn-fcgi-mysql-diary.html

nginx + fastcgi
http://huichen.org/2010/03/12/configure-fastcgi-for-nginx/
0 0