博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx+Lua 开发的 hello world 案例 详解
阅读量:2393 次
发布时间:2019-05-10

本文共 3838 字,大约阅读时间需要 12 分钟。

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

转载地址:http://kogab.baihongyu.com/

你可能感兴趣的文章
hdu1754 I Hate It
查看>>
hdu 1166 敌兵布阵(求区间的和,单节点更新)
查看>>
hiho一下 第四十四周 题目1 : 博弈游戏·Nim游戏
查看>>
poj2299 Ultra-QuickSort(线段树计数问题)
查看>>
hdu4565 So Easy!(矩阵快速幂)
查看>>
poj2528 Mayor's posters(线段树,离散化)
查看>>
线段树多lazy-tag(两个)
查看>>
hdu4578(三个更新操作,三个求值操作)
查看>>
并查集(初级)小结
查看>>
Treap
查看>>
相似图片搜索——感知哈希算法
查看>>
编译原理 词法分析
查看>>
计算机系统结构 计算机系统结构的基本概念
查看>>
计算机系统结构 计算机指令集结构
查看>>
计算机系统结构 输入/输出系统
查看>>
信息安全技术及应用 常规加密技术
查看>>
02-线性结构1 两个有序链表序列的合并
查看>>
HDU 1080 DP LCS
查看>>
HDU 3308 线段树+区间合并
查看>>
ASP.NET 入手页面控件及事件触发
查看>>