Nginx+Lua 开发的 hello world 案例 详解

4 篇文章 1 订阅
编辑 Nginx 配置文件
# cd /opt/modules/openresty/nginx/conf
# cp nginx.conf nginx.conf.example    # 备份 nginx.conf 文件
# vi nginx.conf

worker_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.conf
server {
    listen       80;
    server_name  _;
}

在 nginx.conf 的 http 部分添加 :
# cd /opt/modules/openresty/nginx/conf
# vi  nginx.conf

worker_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 -t
nginx: the configuration file /opt/modules/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/modules/openresty/nginx/conf/nginx.conf test is successful

在 lua.conf 的 server 部分 /lua 请求路径的拦截配置 :
# cd  /opt/modules/openresty/nginx/conf
# vi  lua.conf
server {
    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 -t
nginx: the configuration file /opt/modules/openresty/nginx/conf/nginx.conf syntax is ok
nginx: 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.lua
ngx.say("hello world");

修改 lua.conf
/opt/modules/openresty/nginx/conf
# vi lua.conf
server {
    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 -t
nginx: the configuration file /opt/modules/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/modules/openresty/nginx/conf/nginx.conf test is successful

# ./sbin/nginx -s reload     # 重新加载配置

查看异常日志
tail -f /opt/modules/openresty/nginx/logs/error.log
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: 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 started
2018/05/10 20:48:25 [notice] 23902#0: signal process started
2018/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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值