OpenResty是一款基于Nginx的高性能負載均衡服務器容器,簡單來說是Nginx+Lua。結合了Lua語言來對Nginx進行擴展,使得在Nginx上具有web容器功能。
# OpenResty運行環境搭建
首先是在CentOS 7.6上的安裝過程:
```
cd /opt
```
安裝編譯所需要的環境:
```
yum install readline-devel pcre-devel openssl-devel gcc
```
去OpenResty的官網下載安裝包:
地址:[http://openresty.org/cn/download.html](http://openresty.org/cn/download.html)

復制下載地址:
```
wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
```
解壓文件:
```
tar -xvzf openresty-1.15.8.1.tar.gz
cd openresty-1.15.8.1
```
安裝OpenResty并設置安裝目錄為/opt/openresty,如果不做設置,默認會安裝至/usr/local/openresty:
```
./configure --with-cc-opt="-I/usr/local/include" --with-ld-opt="-L/usr/local/lib" --prefix=/opt/openresty
make
make install
```
至此,OpenResty安裝完成,可以嘗試啟動:
```
/opt/openresty/nginx/sbin/nginx -c /opt/openresty/nginx/conf/nginx.conf -p /opt/openresty/nginx/
```
可以查看端口占用:
```
netstat -tnlp
```

從圖中可以看出nginx已經在監聽80端口,用瀏覽器訪問服務器的80端口,如圖:

OpenResty已經成功啟動
在修改相關配置文件后,需先停止服務,再做啟動:
```
/opt/openresty/nginx/sbin/nginx -s stop
/opt/openresty/nginx/sbin/nginx -c /opt/openresty/nginx/conf/nginx.conf -p /opt/openresty/nginx/
```
# nginx+lua 開發環境配置:
#### 1. 編輯nginx.conf配置文件
```
vi /opt/openresty/nginx/conf/nginx.conf
```
#### 2. 在http部分添加如下配置
```
#lua模塊路徑,多個之間”;”分隔,其中”;;”表示默認搜索路徑,默認到/usr/servers/nginx下找??
lua_package_path "/opt/openresty/lualib/?.lua;;"; #lua?模塊
lua_package_cpath "/opt/openresty/lualib/?.so;;"; #c?模塊
```
#### 3. 為了方便開發在/opt/openresty/nginx/conf目錄下創建一個lua.conf
```
#lua.conf
server {
listen 80;
server_name _;
}
```
#### 4 在nginx.conf中的http部分添加include lua.conf包含此文件片段
```
include lua.conf;
```
#### 5. 測試是否正常
```
/opt/openresty/nginx/sbin/nginx -t
```
如果如下圖所示,表示配置添加成功

# Hello world
#### 1.在lua.conf中server部分添加如下配置
```
location /lua {
default_type 'text/html';
content_by_lua 'ngx.say("hello world")';
}
```
#### 2. 測試配置是否正確
```
/opt/openresty/nginx/sbin/nginx -t
```
#### 3. 重啟nginx
```
/opt/openresty/nginx/sbin/nginx -s stop
/opt/openresty/nginx/sbin/nginx -c /opt/openresty/nginx/conf/nginx.conf -p /opt/openresty/nginx/
```
#### 4. 訪問域名http://xxx.xxx.xxx/lua(自己的機器根據實際情況換ip),可以看到如下內容
hello world
#### 5. lua代碼文件
我們把lua代碼放在nginx配置中會隨著lua的代碼的增加導致配置文件太長不好維護,因此我們應該把lua代碼移到外部文件中存儲。
```
vi /opt/openresty/nginx/conf/lua/test.lua
```
我這里把代碼放在nginx/config/lua中
修改lua.conf,在http下增加
```
content_by_lua_file conf/lua/test.lua; #相對于nginx安裝目錄
```
現在lua.conf整體為:
```
#lua.conf
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua; #相對于nginx安裝目錄
}
}
```
此處conf/lua/test.lua也可以使用絕對路徑/usr/servers/nginx/conf/lua/test.lua。
#### 6. lua_code_cache
默認情況下lua\_code\_cache? 是開啟的,即緩存lua代碼,即每次lua代碼變更必須reload nginx才生效,如果在開發階段可以通過lua\_code\_cache? off;關閉緩存,這樣調試時每次修改lua代碼不需要reload nginx;但是正式環境一定記得開啟緩存。
```
#lua.conf
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file conf/lua/test.lua; #相對于nginx安裝目錄
}
}
```
開啟后reload nginx會看到如下報警
nginx: [alert] lua_code_cache is off; this will hurt performance in /opt/openresty/nginx/conf/lua.conf:7
#### 7. 錯誤日志
如果運行過程中出現錯誤,請不要忘記查看錯誤日志。
```
tail -f /opt/openresty/nginx/logs/error.log
```
到此,基本環境搭建完畢。
<br />
# nginx+lua項目構建
以后我們的nginx lua開發文件會越來越多,我們應該把其項目化,已方便開發。項目目錄結構如下所示:
OpenResty
--openResty.conf???? ---該項目的nginx 配置文件
--lua????????????? ---我們自己的lua代碼
----test.lua
--ualib??????????? ---lua依賴庫/第三方依賴
----*.lua
----*.so
其中我們把lualib也放到項目中的好處就是以后部署的時候可以一起部署,防止有的服務器忘記復制依賴而造成缺少依賴的情況。
我們將項目放到到/usr/openResty目錄下。
nginx.conf配置文件修改includ的conf文件,修改為我們項目中的conf,同時修改引入lualib的地址
```
#lua模塊路徑,多個之間”;”分隔,其中”;;”表示默認搜索路徑,默認到/usr/servers/nginx下找??
lua_package_path "/usr/openResty/lualib/?.lua;;"; #lua?模塊
lua_package_cpath "/usr/openResty/lualib/?.so;;"; #c?模塊
include /usr/openResty/openResty.conf;
```
通過絕對路徑包含我們的lua依賴庫和nginx項目配置文件。
/usr/openResty/openResty.conf的內容如下:
```
server {
listen 80;
server_name _;
location /lua {
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/openResty/lua/test.lua;
}
}
```
lua文件我們使用絕對路徑/usr/openResty/lua/test.lua
到此,整個openResty就可以扔到github上了。
github:git@github.com:meteor1993/openResty.git