编程技术分享平台

网站首页 > 技术教程 正文

openresty代替nginx并使用lua扩展功能

xnh888 2024-10-09 21:55:09 技术教程 75 ℃ 0 评论

openresty是什么

openresty也是web服务器,是基于nginx开发出来,但其内置了lua扩展功能,能让你编写lua脚本对其进行扩展。

openresty与nginx怎么选择

两者都是web服务器,如果没有特殊需求两个选择哪个都可以,但如果你要对web服务器进行扩展功能,可以选择openresty。

虽然nginx也有lua扩展但其不是免费的,只有nginx plus才有,或者你自己为nginx编译lua扩展也可以。

虽然你可以自己为nginx编译lua扩展,但相对来说比较麻烦且以后升级维护也比较困难,这样还不如直接选择openresty。

安装openresty

我们下面只说centos下的安装方法,其它平台安装方法请参照官网文档:https://openresty.org/en/installation.html

  1. 添加软件仓库源
wget https://openresty.org/package/centos/openresty.repo -O /etc/yum.repos.d/openresty.repo
  1. 安装软件
yum install openresty

3、打开配置文件

vim /usr/local/openresty/nginx/conf/nginx.conf

4、修改配置文件内容如下

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            default_type text/html;
            content_by_lua_block {
               ngx.say("<h1>Hello Openresty</h1>")
            }
        }
    }
}

5、启动openresty

systemctl restart openresty

6、访问http://127.0.0.1显示如下信息

Hello Openresty

结语

openresty除兼容nginx外能让你用lua脚本对其进行功能扩展,这里只是一个入门示例,如果要想详细了解其功能使用,请参考官网文档。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表