使用OpenResty替换nginx

什么是OpenResty? 有什么作用?

OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

简而言之OpenResty就是整合了Nginx和lua的框架, 并内置了lua;

主要流程

先用nginx -V查看版本和配置

nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-stream --with-stream_ssl_module --with-http_ssl_module --with-http_v2_module --with-threads

其中configure arguments: 就是nginx的配置信息,这里一会儿配置openresty时要用到

下载openresty

# https://openresty.org/en/download.html
wget https://openresty.org/download/openresty-1.17.8.2.tar.gz

安装

# 解压
tar -zxvf openresty-1.17.8.2.tar.gz
# 进入解压目录
cd openresty-1.17.8.2
# 上面nginx的配置参数后面添加--with-luajit
./configure --prefix=/usr/local/nginx --with-stream --with-stream_ssl_module --with-http_ssl_module --with-http_v2_module --with-threads --with-luajit
# 安装
gmake && gmake install

迁移nginx配置文件

mv /usr/local/nginx/conf/*.conf /usr/local/openresty/nginx/conf
mv /usr/local/nginx/conf/vhost /usr/local/openresty/nginx/conf

停止原nginx

nginx -s reload

替换原nginx

# 备份原nginx
mv /usr/local/nginx /usr/local/nginx.bak
# 替换nginx
cp /usr/local/openresty/bin/openresty /usr/local/bin/nginx

这时查看在查看版本时多了openresty的标识

nginx -v

结果

nginx version: openresty/1.19.3.1

启动并测试是否安装成功

# 启动
/usr/local/bin/nginx
# 添加location测试
location /lua {
    # 测试lua模块是否可用
    lua_code_cache off;  # 关闭缓存
    default_type "text/plain";
    content_by_lua 'ngx.say("hello, openresty")';
}

参考资料

https://openresty.org
https://blog.csdn.net/Linuxprobe18/article/details/103493982

posted @ 2021-03-18 19:54  ranblogs  阅读(1971)  评论(0)    收藏  举报