2013-08-04 57 views
10

我現在正在用nginx編程Lua。我寫我的Lua文件並將其放在/usr/local/nginx/lua的目錄中。然後在nginx.conf我寫了一個位置,如Lua的nginx包路徑

location /lua { 
    lua_need_request_body on; 
    content_by_lua_file lua/test.lua; 
} 

當我通過Nginx的訪問這個位置上,Lua的腳本將被執行。

在一個Lua文件中,人們通常可以包括你自己的Lua模塊,並指示搜索路徑

common_path = '../include/?.lua;' 
package.path = common_path .. package.path 

在普通的Lua編程,相對路徑是相對於我的Lua文件。

但是對於nginx,相對路徑是相對於我啓動Nginx的目錄。

贊,我在/usr/local/nginx並執行sbin/nginx,那麼在Lua package.path將是/usr/local/include

如果我在/usr/local/nginx/sbin並執行./nginx,那麼在Lua package.path將是/usr/local/nginx/include

我認爲我啓動nginx服務器的目錄不應該受到限制, 但我不知道如何解決這個問題。

+0

你使用HttpLuaModule嗎?如果是這樣,也許你需要設置lua_package_path和/或lua_package_cpath [見文檔](http://wiki.nginx.org/HttpLuaModule#lua_package_path) – Mali

+0

是的,我已經看到了api,但路徑和cpath與'/',我懷疑是否有其他方法可以使路徑與nginx目錄關聯 – freedoo

回答

13

您想要修改Lua package.path以在您擁有源代碼的目錄中搜索。對你來說,那是lua/

你可以用httpd塊中的lua_package_path指令來做到這一點(文檔說你可以把它放在頂層,但是當我嘗試它不起作用時)。

所以你:

http { 
    #the scripts in lua/ need to refer to each other 
    #but the server runs in lua/.. 

    lua_package_path "./lua/?.lua;;"; 

    ... 
} 

現在你的Lua腳本可以找到對方,即使服務器運行一個目錄了。

+0

值得注意的是,';;'附加了默認的package.path,而不是拼寫錯誤。 – Randall