OpenResty环境搭建
http://openresty.org/cn/download.html
安装前准备,必须安装perl、libpcre、libssl库。
# 从系统路径中查看必备库是否已经安装 $ sudo ldconfig -v # 安装必备库 $ sudo apt install libpcre3-dev libssl-dev perl make build-essential curl libreadline-dev libncurses5-dev
下载并解压OpenResty后进入其目录
$ wget https://openresty.org/download/ngx_openresty-1.13.6.1.tar.gz $ tar -zxvf ngx_openresty-1.13.6.1.tar.gz $ mv openresty-1.13.6.1 openresty $ cd openresty $ ./configure
默认会被安装到/usr/local/openresty目录下
# 编译并安装 $ sudo make && make install $ cd /usr/local/openresty
启动Nginx
$ sudo /usr/local/openresty/nginx/sbin/nginx $ ps -ef | grep nginx $ service nginx status
Nginx启动若出现
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind()
说明80端口并占用,查看80端口被占用的端口并重启。原因在于nginx先监听了ipv4的80端口之后又监听了ipv6的80端口,于是就重复占用了。
$ sudo netstat -ntlp | grep 80 $ sudo killall -9 nginx
重新编辑Nginx配置文件
$ sudo vim /etc/nginx/conf/nginx.conf listen 80; listen [::]:80 ipv6only=on default_server;
使用curl工具或在浏览器访问默认80端口
$ curl 127.0.0.1
浏览器输入http://127.0.0.1/
将Nginx工具配置到当前用户的系统环境变量中
$ sudo vim ~/.bashrc export PATH=$PATH:/usr/local/openresty/nginx/sbin $ source ~./bashrc $ cd ~ $ nginx -s reload nginx: [alert] kill(12267, 1) failed (1: Operation not permitted)
开发文档
https://www.nginx.com/resources/wiki/modules/lua/
ubuntu 安装 vcode 或 sublime text 编辑器
content_by_lua
$ vim /usr/local/openresty/nginx/conf/nginx.conf location /test { default_type text/html; content_by_lua 'ngx.say("hello openresty")'; } # 重启Nginx $ /usr/local/openresty/nginx/sbin/nginx -s reload # 浏览器访问 127.0.0.1/test
content_by_lua_file
$ vim nginx.conf location /test { content_by_lua_file 'html/test.lua'; } $ vim ../html/test.lua ngx.say("hello lua") $ sudo /usr/local/nginx/sbin/nginx -s reload $ curl 127.0.0.1/test hello lua
为避免每次修改都需要重启Nginx,可在Nginx的server选项中配置lua_code_cache选项。
$ vim nginx.conf server{ lua_code_cache off; location /test{ content_by_lua_file 'html/test.lua'; } } $ sudo /usr/local/openresty/nginx/sbin/nginx -s reload nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:48
注意lua_code_cache off;是会引擎Nginx的性能的,在生产环境中是需要将其开启的。
小节
在OpenResty中开发是分为两步的,第一步是修改Nginx配置,第二步是使用Lua开发自己的脚本。
OpenResty入门
参考资料
- OpenResty最佳实践
- Nginx Lua
创建工作目录
OpenResty安装之后就有配置文件及相关目录,为了工作目录和安装目录互不干扰,另外创建OpenResty工作目录,并另写配置。
$ mkdir -p ~/openresty/test/logs ~/openresty/test/conf $ vim ~/openresty/test/conf/nginx.conf # 设置Nginx worker工作进程数量,即CPU核数。 worker_processes 1; # 设置错误日志文件路径 error_log logs/error.log; # 配置Nginx服务器与用户的网络连接 events{ # 设置每个工作进程的最大连接数 worker_connections 10224; } http{ # 虚拟机主机块定义 server{ # 监听端口 listen 8001; # 配置请求的路由 location /{ default_type text/html; content_by_lua_block{ ngx.say("hello world"); } } } } $ nginx -p ~/openresty/test $ curl 127.0.0.1:8001 hello world $ vim nginx.conf location /test{ content_by_lua_file "lua/test.lua"; } $ cd .. && mkdir lua && cd lua $ vim test.lua local args = ngx.req.get_uri_args() local salt = args.salt if not salt then ngx.exit(ngx.HTTP_BAD_REQUEST) end local md5str = ngx.md5(ngx.time()..salt) ngx.say(md5str) $ sudo /usr/local/openresty/nginx/sbin/nginx -s reload $ curl -i 127.0.0.1/test?salt=lua HTTP/1.1 200 OK Server: openresty/1.13.6.2 Date: Sun, 27 Jan 2019 10:07:17 GMT Content-Type: application/octet-stream Transfer-Encoding: chunked Connection: keep-alive b55b77f75e46b96b11778ca7edfe8d55
若代码中出现错误则需要直接查看Nginx的错误日志进行查看
$ vim nginx/logs/error.log 2019/01/27 17:37:15 [error] 15764#0: *6 failed to load external Lua file "/usr/local/openresty/nginx/test.lua": cannot open /usr/local/openresty/nginx/test.lua: No such file or...
Windows系统下查看Nginx进程
λ tasklist /fi "imagename eq nginx.exe" 映像名称 PID 会话名 会话# 内存使用 ========================= ======== ================ =========== ============ nginx.exe 9072 Console 1 7,840 K nginx.exe 7692 Console 1 12,304 K nginx.exe 8120 Console 1 7,840 K nginx.exe 4552 Console 1 12,188 K nginx.exe 9588 Console 1 7,828 K nginx.exe 6256 Console 1 12,216 K nginx.exe 7308 Console 1 7,828 K nginx.exe 10192 Console 1 12,212 K λ taskkill /im nginx.exe /f 成功: 已终止进程 "nginx.exe",其 PID 为 9072。
ngx lua API
参考资料
- NGINX API for Lua