CentOs7 源码编译安装OpenResty

Nginx 采用一个 master 进程管理多个 worker 进程(master-worker)模式,基本的事件处理都在 woker 中,master 负责一些全局初始化,以及对 worker 的管理。在OpenResty中,每个 woker 使用一个 LuaVM,当请求被分配到 woker 时,将在这个 LuaVM 里创建一个 coroutine(协程)。协程之间数据隔离,每个协程具有独立的全局变量_G。OpenResty致力于将服务器应用完全运行与nginx中,充分利用nginx事件模型进行非阻塞I/O通信。其对MySQL、redis、Memcached的I\O通信操作也是非阻塞的,可以轻松应对10K以上的超高连接并发。

OpenResty 是 Nginx 与 Lua 的结合;

下载  http://openresty.org/cn/download.html

安装前的准备

必须将这些库 perl 5.6.1+, libpcre, libssl安装在您的电脑之中

yum install pcre-devel openssl-devel gcc curl -y

 安装

tar -zxvf openresty-1.15.8.2.tar.gz
cd openresty-1.15.8.2
./configure
make
make install

 默认程序会被安装到/usr/local/openresty目录。

配置

修改nginx.conf配置文件

cd /usr/local/openresty/nginx/conf

vim nginx.conf


worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }
}

添加环境变量

echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin" >> /etc/profile
source /etc/profile

然后启动openresty,启动命令和nginx一致。

nginx -c /usr/local/openresty/nginx/conf/nginx.conf

启动后查看一下服务

ps -ef | grep nginx

访问 Web 服务

第二种lua配置方案

添加lua.conf配置文件

server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }

修改nginx.conf配置文件

cd /usr/local/openresty/nginx/conf

vim nginx.conf

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    #lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找  
    lua_package_path "/usr/local/openresty/lualib/?.lua;;";  #lua模块
    lua_package_cpath "/usr/local/openresty/lualib/?.so;;";  #c模块 
    include lua.conf; #lua.conf和nginx.conf在同一目录下
}

添加环境变量

echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin" >> /etc/profile
source /etc/profile

然后启动openresty,启动命令和nginx一致。

nginx -c /usr/local/openresty/nginx/conf/nginx.conf

启动后查看一下服务

ps -ef | grep nginx

访问 Web 服务

配置lua代码文件

我们把lua代码放在nginx配置中会随着lua的代码的增加导致配置文件太长不好维护,因此我们应该把lua代码移到外部文件中存储。

在conf文件夹下创建lua文件夹,专门用来存放lua文件

mkdir /usr/local/openresty/nginx/conf/lua

创建test.lua文件

cd /usr/local/openresty/nginx/conf/lua
vim test.lua
ngx.say("test lua");

修改conf/lua.conf文件

vim /usr/local/openresty/nginx/conf/lua.conf
server {
        listen 8080;
        location / {
            default_type text/html;
            lua_code_cache off; #关闭lua代码缓存,调试时关闭,正式环境开启
            content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
        }
    }

关闭缓存后会看到如下报警(忽略不管)

nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/lua.conf:5

重启 Web 服务

nginx  -s reload

.测试性能

安装ab命令

yum -y install httpd-tools

压力测试

  • -c:每次并发数为10个
  • -n:共发送50000个请求
ab -c10 -n50000 http://localhost:8080/ 

测试报详解

Server Software:        openresty/1.15.8.2   #服务器软件
Server Hostname:        localhost   #域名
Server Port:            8080   #端口

Document Path:          /    #文件路径
Document Length:        20 bytes #页面字节数

Concurrency Level:      10  #请求并发数
Time taken for tests:   3.392 seconds #总访问时间
Complete requests:      50000  #请求成功数
Failed requests:        0   #请求失败数
Write errors:           0
Total transferred:      8400000 bytes   #请求总数据大小(包括header头信息)
HTML transferred:       1000000 bytes   #html页面实际总字节数
Requests per second:    14738.52 [#/sec] (mean)   #每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量
Time per request:       0.678 [ms] (mean)  #用户平均请求等待时间 
Time per request:       0.068 [ms] (mean, across all concurrent requests)    # 服务器平均处理时间,也就是服务器吞吐量的倒数
Transfer rate:          2418.04 [Kbytes/sec] received   #每秒获取的数据长度

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     0    1   0.2      1       9
Waiting:        0    0   0.2      0       9
Total:          0    1   0.2      1       9

Percentage of the requests served within a certain time (ms)
  50%      1   #50%用户请求在1ms内返回
  66%      1   #66%用户请求在1ms内返回
  75%      1   #75%用户请求在1ms内返回
  80%      1   #80%用户请求在1ms内返回
  90%      1   #90%用户请求在1ms内返回
  95%      1   #95%用户请求在1ms内返回
  98%      1   #98%用户请求在1ms内返回
  99%      1   #99%用户请求在1ms内返回
 100%      9 (longest request)

https://www.cnblogs.com/wushuaishuai/p/9315611.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值