编辑 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
访问 http://ci-server/lua
创建 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