总结反思
如题,这是一个个人对openresty使用的反思贴,也是一个记录贴。说起来,在项目中使用openresty已经有不短的时间了,主要是做API网关和一些动态负载均衡的功能,转眼已经是2019年了,过完年回来,又要开始紧锣密鼓地推进工作进度了,其中一块功能又选择了使用openresty来做。
又回到了一个熟悉的步骤: 申请机器 -> openresty环境配置(参考官方文档) -> 搭建项目骨架(参考旧项目和openresty最佳实践) -> 从luarocks上下载需要的lua包 -> coding
在开发过程中,对自己有一股失望的情绪,觉得自己对openresty这个项目的理解和精通程度一直停留在了一个比较低的水平。反思了一下,觉得这个原因是多方面的。
- 对nginx的精通程度
- 对lua语法的精通程度
- 对openresty 包的精通程度
自己的问题:
所谓熟能生巧,首先是自己实践的不足,然后是自己没有很好的总结,导致每次使用都差不多需要从它的官方文档开始,即使是opm出了很久了,但是每次下载lua包,我还是会去luarocks,这一点,就是学习精神的懈怠的一窥吧。还有一个小问题,似乎我一直没有找到一个趁手的ide去开发openresty项目(vim/emacs党勿喷)
项目本身的复杂性:
就个人的感觉,openresty本身是一个nginx + lua packages组成的解决方案式的项目,并不能像学习一个新语言那样,快速无依赖地实现hello world。
总结来说,只有更好的总结,更多的实践, 更多的理解nginx/lua本身,才能又快又好地完成一个优秀的openresty项目.
学习笔记
希望这篇文,能成为一个合格的个人学习笔记记录吧
部署
centos下安装openresty:
1 | mkdir -p /data/openresty/opt |
第一行代码
init_demo.sh1
2
3
4
5
6
7
8
9
10
11
12mkdir -p /data/openresty/demo
cd /data/openresty/demo
mkdir logs
echo '/data/openresty/bin/openresty -p /data/openresty/demo -c nginx.conf' > run.sh
echo '/data/openresty/bin/openresty -p /data/openresty/demo -c nginx.conf -t' >test.sh
echo '/data/openresty/bin/openresty -p /data/openresty/demo -c nginx.conf -s reload' >restart.sh
echo 'ps -ef|grep /data/openresty/demo |grep -v grep |awk '{print$2}' | xargs kill' > stop.sh
touch nginx.conf
nginx.conf内容如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18worker_processes 1;
user root;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 127.0.0.1:9191;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("hello world")
}
}
}
}
测试:
1 | cd /data/openresty/demo |
tips:
1.在 1.5.8.1 版本之前, OpenResty 默认使用标准 Lua 5.1 解释器。所以对于老版本, 你需要显式地加入–with-luajit 编译选项(1.5.8.1 以后的版本已默认开启)来启用 LuaJIT 组件
2.openresty官网首页有全文搜索功能
3.openresty执行阶段概念说明:https://moonbingbing.gitbooks.io/openresty-best-practices/content/ngx_lua/phase.html
~ 待续
$todo 代码风格
$todo lua_package_path相关
参考文档:
openresty官方安装文档:
https://openresty.org/cn/installation.html
openresty 组件列表:
https://openresty.org/cn/components.html
luarocks安装文档:
https://openresty.org/cn/using-luarocks.html
openresty官网样例:
https://openresty.org/cn/samples.html
openresty最佳实践:https://moonbingbing.gitbooks.io/openresty-best-practices/content/