Ubuntu下OpenResty 搭建高性能服务端_ubuntu openresty luajit

  • 数据不一致
    • 例如没有主从架构导致不同服务器数据不一致
  • 遇到存储瓶颈
    • 磁盘或内存遇到天花板

解决数据不一致比较好的办法是采用主从或分布式集中存储,而遇到存储瓶颈就需要进行按业务键进行分片,将数据分散到多台服务器。
在这里插入图片描述接入网关
接入网关又叫接入层,即接收流量的入口,在入口处做如下事情:
在这里插入图片描述

OpenResty环境搭建

安装前准备,必须安装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

OpenResty入门

创建工作目录

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");
            }
        }


![img](https://img-blog.csdnimg.cn/img_convert/209c5af3010d80b00669bb2fe92e02ae.png)
![img](https://img-blog.csdnimg.cn/img_convert/f2d1148359c584b08564db6111babada.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**


https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
        }


[外链图片转存中...(img-3rLoIGN3-1725796751643)]
[外链图片转存中...(img-iRSaKBs7-1725796751644)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**


https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值