搭建OpenResty(Nginx+Lua)

时间:2023-11-21 14:06:20

这篇文章是一个多月前写的,当时之所以搭建这个是为了最大程度上发挥Nginx的高并发效率(主要是结合lua脚本),参考的话,主要参考张开涛先生写的跟开涛学Nginx+lua系列文章,地址为:https://jinnianshilongnian.iteye.com/blog/2190344

当时本人按照张开涛写的一步一步搭建,当然了也发现一些小问题,所以在此将其发表出去,另外强调一点,开发人员无论是平时编写代码或者是调研新技术或者实践,最好也写写文档总结一下。

我写文档的主要目的,一来让自己思路更加清晰,二来为博文积累素材,三来这是一个秘密。

下面进入正题吧

1.创建目录/usr/servers

mkdir -p /usr/servers
cd /usr/servers/

2.安装依赖(不同的系统环境需要以不同的方式安装依赖,具体可以参考该地址: //openresty.org/#Installation)

apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl  

3.下载ngx_openresty-1.7.7.2.tar.gz并解压

wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
tar -xzvf ngx_openresty-1.7.7.2.tar.gz

ngx_openresty-1.7.7.2/bundle目录里存放着nginx核心和很多第三方模块,比如有我们需要的Lua和LuaJIT

4.安装LuaJIT

cd bundle/LuaJIT-2.1-20150120/
make clean && make && make install
ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit

5.下载ngx_cache_purge模块,该模块用于清理nginx缓存

cd /usr/servers/ngx_openresty-1.7.7.2/bundle
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar -xvf 2.3.tar.gz

6.下载nginx_upstream_check_module模块,该模块用于ustream健康检查

cd /usr/servers/ngx_openresty-1.7.7.2/bundle
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -xvf v0.3.0.tar.gz

7.安装ngx_openresty

cd /usr/servers/ngx_openresty-1.7.7.2
./configure --prefix=/usr/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2
make && make install

--with***                安装一些内置/集成的模块

--with-http_realip_module  取用户真实ip模块

-with-pcre               Perl兼容的达式模块

--with-luajit              集成luajit模块

--add-module            添加自定义的第三方模块,如此次的ngx_che_purge

8.启动nginx

/usr/servers/nginx/sbin/nginx

9.配置nginx+lua开发环境

(1) 编辑nginx.conf配置文件

vim /usr/servers/nginx/conf/nginx.conf 

(2)在http部分添加如下配置

#lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
lua_package_path "/usr/servers/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/servers/lualib/?.so;;"; #c模块

搭建OpenResty(Nginx+Lua)

其实之所以编写example.conf,是考虑Nginx+Lua开发文件会越来越多,如果全部挤在nginx文件下的话,不利于管理和维护。

/usr/example/example.conf配置文件如下 :

server {
listen 80;
server_name _; location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/example/lua/test.lua;
}
}

test.lua内容如下:

ngx.say("hello world");

重启Nginx在浏览器输入:http://IP/lua,出现hello world表示成功

例如:

搭建OpenResty(Nginx+Lua)

最后完整的nginx.conf文件如下:

#user  nobody;
worker_processes 2; error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http { include mime.types;
default_type application/octet-stream;
#lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
lua_package_path "/usr/servers/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/servers/lualib/?.so;;"; #c模块
include /usr/example/example.conf;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}