1. 程式人生 > >Nginx+Lua 開發的 hello world 案例 詳解

Nginx+Lua 開發的 hello world 案例 詳解

編輯 Nginx 配置檔案# cd /opt/modules/openresty/nginx/conf# cp nginx.conf nginx.conf.example    # 備份 nginx.conf 檔案# vi nginx.confworker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;# 在 http 部分新增,如下兩個配置,將載入 Lua 相關庫lua_package_path "/opt/modules/openresty/lualib/?.lua;;";
    lua_package_cpath "/opt/modules/openresty/lualib/?.so;;";    sendfile        on;    keepalive_timeout  65;    server {        listen       8081;        server_name  localhost;        location / {            root   html;            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html;
        location = /50x.html {            root   html;        }    }}建立 lua.conf# cd /opt/modules/openresty/nginx/conf# vi lua.confserver {    listen       80;    server_name  _;}在 nginx.conf 的 http 部分新增 :# cd /opt/modules/openresty/nginx/conf# vi nginx.confworker_processes  1;events {    worker_connections  1024;
}http {    include       mime.types;    default_type  application/octet-stream;# 在 http 部分新增,如下兩個配置,將載入 Lua 相關庫lua_package_path "/opt/modules/openresty/lualib/?.lua;;";    lua_package_cpath "/opt/modules/openresty/lualib/?.so;;";    # 載入 Lua 配置先關檔案    include lua.conf;    sendfile        on;    keepalive_timeout  65;    server {        listen       8081;        server_name  localhost;        location / {            root   html;            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }}驗證配置是否正確 :# cd /opt/modules/openresty/nginx# ./sbin/nginx -tnginx: the configuration file /opt/modules/openresty/nginx/conf/nginx.conf syntax is oknginx: configuration file /opt/modules/openresty/nginx/conf/nginx.conf test is successful在 lua.conf 的 server 部分 /lua 請求路徑的攔截配置 :# cd /opt/modules/openresty/nginx/conf# vi lua.confserver {    listen       80;    server_name  _; # 新增 /lua 路徑的配置location /lua {        default_type 'text/html';        content_by_lua 'ngx.say("hello world")';    }}# 檢測修改的配置是否正常# cd /opt/modules/openresty/nginx# ./sbin/nginx -tnginx: the configuration file /opt/modules/openresty/nginx/conf/nginx.conf syntax is oknginx: configuration file /opt/modules/openresty/nginx/conf/nginx.conf test is successful啟動 Nginx 或者 重新 Nginx 載入配置啟動 Nginx/opt/modules/openresty/nginx/sbin/nginx# 重新 Nginx 載入配置# /opt/modules/openresty/nginx/sbin/nginx -s reload建立 Lua 指令碼測試檔案 test.lua# cd /opt/modules/openresty/nginx/conf# mkdir lua# vi lua/test.luangx.say("hello world");修改 lua.conf/opt/modules/openresty/nginx/conf# vi lua.confserver {    listen       80;    server_name  _;    location /lua {        default_type 'text/html';# content_by_lua 'ngx.say("hello world")';  去掉,引入 Lua 指令碼檔案        content_by_lua_file conf/lua/test.lua;    }}# 檢測修改的配置是否正常# cd /opt/modules/openresty/nginx# ./sbin/nginx -tnginx: the configuration file /opt/modules/openresty/nginx/conf/nginx.conf syntax is oknginx: configuration file /opt/modules/openresty/nginx/conf/nginx.conf test is successful# ./sbin/nginx -s reload # 重新載入配置檢視異常日誌tail -f /opt/modules/openresty/nginx/logs/error.log2018/05/10 20:04:56 [emerg] 23754#0: bind() to 0.0.0.0:80 failed (98: Address already in use)2018/05/10 20:04:56 [emerg] 23754#0: bind() to 0.0.0.0:80 failed (98: Address already in use)2018/05/10 20:04:56 [emerg] 23754#0: bind() to 0.0.0.0:80 failed (98: Address already in use)2018/05/10 20:04:56 [emerg] 23754#0: bind() to 0.0.0.0:80 failed (98: Address already in use)2018/05/10 20:04:56 [emerg] 23754#0: bind() to 0.0.0.0:80 failed (98: Address already in use)2018/05/10 20:04:56 [emerg] 23754#0: still could not bind()2018/05/10 20:46:16 [notice] 23890#0: signal process started2018/05/10 20:48:25 [notice] 23902#0: signal process started2018/05/10 20:48:29 [error] 23903#0: *1 open() "/opt/modules/openresty/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.242.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "ci-server", referrer: "http://ci-server/lua"2018/05/10 20:56:22 [notice] 23925#0: signal process started