openresty

openresty --help--with需要加--without不需要加,内置模块nginx 同理[root@server1 ~]# lsmemcache-4.0.5.2 oniguruma-6.8.2-1.el7.x86_64.rpmmemcache-4.0.5.2.tgz oniguruma-devel-6.8.2-1.el7.x86_64.rpmnginx-1.18.0 openresty-1.17.8.2.tar.gznginx-1.18.
摘要由CSDN通过智能技术生成

一.前言

我们都知道Nginx有很多的特性和好处,但是在Nginx上开发成了一个难题,Nginx模块需要用C开发,而且必须符合一系列复杂的规则,最重要的用C开发模块必须要熟悉Nginx的源代码,使得开发者对其望而生畏。为了开发人员方便,所以接下来我们要介绍一种整合了Nginx和lua的框架,那就是OpenResty,它帮我们实现了可以用lua的规范开发,实现各种业务,并且帮我们弄清楚各个模块的编译顺序。关于OpenResty,我想大家应该不再陌生,随着系统架构的不断升级、优化,OpenResty在被广泛的应用。

二.OpenResty运行原理

Nginx 采用的是 master-worker 模型,一个 master 进程管理多个 worker 进程,基本的事件处理都是放在 woker 中,master 负责一些全局初始化,以及对 worker 的管理。在OpenResty中,每个 woker 使用一个 LuaVM,当请求被分配到 woker 时,将在这个 LuaVM 里创建一个 coroutine(协程)。协程之间数据隔离,每个协程具有独立的全局变量_G。

ps. 协程和多线程下的线程类似:有自己的堆栈,自己的局部变量,有自己的指令指针,但是和其他协程程序共享全局变量等信息。线程和协程的主要不同在于:多处理器的情况下,概念上来说多线程是同时运行多个线程,而协程是通过代码来完成协程的切换,任何时刻只有一个协程程序在运行。并且这个在运行的协程只有明确被要求挂起时才会被挂起。

原理图如下:

三.OpenResty的优势

首先我们选择使用OpenResty,其是由Nginx核心加很多第三方模块组成,其最大的亮点是默认集成了Lua开发环境,使得Nginx可以作为一个Web Server使用。

借助于Nginx的事件驱动模型和非阻塞IO,可以实现高性能的Web应用程序。

而且OpenResty提供了大量组件如Mysql、Redis、Memcached等等,使在Nginx上开发Web应用更方便更简单。目前在京东如实时价格、秒杀、动态服务、单品页、列表页等都在使用Nginx+Lua架构,其他公司如淘宝、去哪儿网等。

四.Nginx和lua的简介

  1. Nginx:

(1) Nginx的优点

轻量级同样起web 服务比apache占用更少内存及资源 

抗并发nginx 处理请求异步非阻塞而apache 则阻塞型高并发下nginx 能保持低资源低消耗高性能 

高度模块化设计编写模块相对简单 

社区活跃各种高性能模块出品迅速啊

(2) Nginx为什么性能高,占用内存少

众所周知,nginx性能高,而nginx的高性能与其架构是分不开的。在这里,我们简单粗略的介绍一下nginx的架构。

首先,nginx采用的是多多进程模式,好处是什么呢?首先,对于每个worker进程来说,独立的进程,不需要加锁,所以省掉了锁带来的开销,同时在编程以及问题查找时,也会方便很多。其次,采用独立的进程,可以让互相之间不会影响,一个进程退出后,其它进程还在工作,服务不会中断,master进程则很快启动新的worker进程。当然,worker进程的异常退出,肯定是程序有bug了,异常退出,会导致当前worker上的所有请求失败,不过不会影响到所有请求,所以降低风险。

Nginx是采用异步非阻塞的方式去处理请求的,什么是异步非阻塞呢?其实就是当一个线程调用出现等待的io之类的情况时,而不是阻塞在这里,而是去处理别的事情,等io准备好了,然后再去执行,具体的我就不在这里和大家描述了。
  1. lua:

(1) Lua 是一个小巧的脚本语言。作者是巴西人。该语言的设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能

(2) Lua的特点:

Lua脚本可以很容易的被C/C++代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,Ini等文件格式,并且更容易理解和维护。

Lua由标准C编写而成,代码简洁优美,几乎在所有操作系统和平台上都可以编译,运行。一个完整的Lua解释器不过200k,在目前所有脚本引擎中,Lua的速度是最快的。这一切都决定了Lua是作为嵌入式脚本的最佳选择。
openresty --help
--with需要加
--without不需要加,内置模块
nginx 同理





[root@server1 ~]# ls
memcache-4.0.5.2      oniguruma-6.8.2-1.el7.x86_64.rpm
memcache-4.0.5.2.tgz  oniguruma-devel-6.8.2-1.el7.x86_64.rpm
nginx-1.18.0          openresty-1.17.8.2.tar.gz
nginx-1.18.0.tar.gz   package.xml
nginx-1.19.1          php-7.4.6
nginx-1.19.1.tar.gz   php-7.4.6.tar.bz2
[root@server1 ~]# tar zxf openresty-1.17.8.2.tar.gz 
[root@server1 ~]# cd openresty-1.17.8.2
[root@server1 openresty-1.17.8.2]# ls
bundle     COPYRIGHT  README.markdown     util
configure  patches    README-windows.txt
[root@server1 openresty-1.17.8.2]# ll
total 104
drwxrwxr-x 46 nginx nginx  4096 Jul 14 01:56 bundle
-rwxrwxr-x  1 nginx nginx 52486 Jul 14 01:55 configure
-rw-rw-r--  1 nginx nginx 22924 Jul 14 01:55 COPYRIGHT
drwxrwxr-x  2 nginx nginx  4096 Jul 14 01:55 patches
-rw-rw-r--  1 nginx nginx  4689 Jul 14 01:55 README.markdown
-rw-rw-r--  1 nginx nginx  8972 Jul 14 01:55 README-windows.txt
drwxrwxr-x  2 nginx nginx    52 Jul 14 01:55 util
[root@server1 openresty-1.17.8.2]# ./configure 
[root@server1 openresty-1.17.8.2]# gmake install
[root@server1 openresty-1.17.8.2]# cd /usr/local/openresty/
[root@server1 openresty]# ls
bin  COPYRIGHT  luajit  lualib  nginx  pod  resty.index  site
[root@server1 openresty]# cd bin/
[root@server1 bin]# ls
md2pod.pl  nginx-xml2pod  openresty  opm  resty  restydoc  restydoc-index      
[root@server1 bin]# ll openresty 
lrwxrwxrwx 1 root root 37 Aug 14 09:59 openresty -> /usr/local/openresty/nginx/sbin/nginx
[root@server1 bin]# cd ../nginx/sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# ./nginx -v
nginx version: openresty/1.17.8.2
[root@server1 sbin]# cd ..
[root@server1 nginx]# cd conf/
[root@server1 conf]# systemctl start memcached
[root@server1 conf]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      13192/memcached     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3376/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      3600/master         
tcp        0      0 172.25.4.1:22           172.25.4.250:35876      ESTABLISHED 3873/sshd: root@pts 
tcp6       0      0 :::11211                :::*                    LISTEN      13192/memcached     
tcp6       0      0 :::22                   :::*                    LISTEN      3376/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      3600/master         

server3,server4
yum install -y memcached telnet
systemctl start memcached
netstat -antlp|grep memcached
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      3556/memcached      
tcp6       0      0 :::11211                :::*                    LISTEN      3556/memcached      



[root@server1 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server1 conf]# vim nginx.conf

http {

        upstream memcache {
        server 172.25.4.3:11211;
        server 172.25.4.4:11211;
        keepalive 512;
	}


        location /memc {
        internal;
        memc_connect_timeout 100ms;
        memc_send_timeout 100ms;
        memc_read_timeout 100ms;
        set $memc_key $query_string;
        set $memc_exptime 300;
        memc_pass memcache;
}


[root@server1 conf]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@server1 conf]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3789         103        3295          16         389        3424
Swap:          2047           0        2047


[root@server1 conf]# vim nginx.conf


 79         location ~ \.php$ {
 80             root           html;
 81             fastcgi_pass   127.0.0.1:9000;
 82             fastcgi_index  index.php;
 83             #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 84             include        fastcgi.conf;
 85         }



[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload
[root@server1 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server1 conf]# cd ../html/
[root@server1 html]# vim index.php


<?php
phpinfo()
?>


[root@foundation4 ~]# ab -c10 -n 20000 http://172.25.4.1/index.php

Server Software:        openresty/1.17.8.2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值