Nginx的nb之處就不說了,lua也是一個小巧的腳本語言,由標準C編寫而成,幾乎能夠運行在全部的平臺上,也很是強大,其餘特性請自行度娘。
nginx_lua_module是由淘寶的工程師清無(王曉哲)和春來(章亦春)所開發的nginx第三方模塊,它能將lua語言嵌入到nginx配置中,從而使用lua就極大加強了nginx的能力 http://wiki.nginx.org/HttpLuaModulehtml
下面說說mac下Nginx如何編譯集成nginx_lua_module模塊nginx
lua-nginx-module-0.10.5,LuaJIT,ngx_devel_kit-0.3.0,openssl,zlib
還有主要的nginx源碼,具體下載地址就搜索一下吧,版本隨時也會變化瀏覽器
PCRE是爲了重寫URL的rewrite
若是本機安裝了brew就比較方便了,直接 brew install PCRE 便可自動下載安裝。不然請下載源碼測試
./configure make make install
zlib是爲了gzip壓縮使用。Brew上好像沒有,因此仍是源碼安裝。lua
cd /Users/hecom/Downloads/zlib-1.2.8 ./configure make make install
下載源碼,解壓spa
cd /Users/hecom/Downloads/openssl-1.0.1t ./configure make make install
使用源碼安裝,http://luajit.org/download.html日誌
cd /Users/hecom/Downloads/ LuaJIT-2.0.4t ./configure make make install
lib和include的默認安裝位置在/usr/local/lib和usr/local/include文件夾下面,這個很重要,須要導入環境變量使用。code
個人本地路徑:/Users/hecom/Downloads/lua-nginx-module-0.10.5
注意版本號htm
個人本地路徑:/Users/hecom/Downloads/ngx_devel_kit-0.3.0
注意版本號blog
個人本地路徑:/Users/hecom/Downloads/nginx-1.11.1
注意版本號
export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.0/
若是nginx沒法啓動,報下面錯誤,請檢測是否執行該步驟。
[error] failed to initialize Lua VM in /usr/local/nginx/conf/nginx.conf:44
cd /Users/hecom/Downloads/nginx-1.11.1
./configure --prefix=/usr/local/nginx \ #nginx安裝位置 #--with-openssl=/Users/hecom/Downloads/openssl-1.0.1t \ #openssl的源碼路徑,若是不是本身編譯,則不須要這個參數。
--with-http_ssl_module \ #開啓ssl模塊 --with-http_stub_status_module \ --with-zlib=/Users/hecom/Downloads/zlib-1.2.8 \ #zlib源碼路徑 --add-module=/Users/hecom/Downloads/lua-nginx-module-0.10.5 \ #源碼路徑 --add-module=/Users/hecom/Downloads/ngx_devel_kit-0.3.0 #ngx_devel源碼路徑
make make install
上面命令請在root命令下執行,即在前面加上sudo, 各個源碼路徑請根據本身的位置修改。
本身編譯openssl上走了不少彎路,一直報錯:ld: symbol(s) not found for architecture x86_64,編譯openssl時加 ./Configure darwin64-x86_64-cc 也不行,最後沒有搞定,使用系統自帶的版本經過了
若是幸運不出錯的話,nginx變安裝成功了。輸入下面命令啓動nginx並測試,sudo /usr/local/nginx/sbin/nginx,而後在瀏覽器中輸入:http://localhost/,若是看到下面畫面恭喜你,成功了!
打開/usr/local/nginx/conf/nginx.conf文件,添加lua腳本。
location /hello { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; }
而後執行 sudo /usr/local/nginx/sbin/nginx –s reload在瀏覽器中輸入 http://localhost/hello,看到輸出 「hello,lua」不然根據錯誤日誌排查緣由