本文主要对openresty做入门简介以及初始化的安装配置介绍。
1、OpenResty简介
了解过web服务器的同学肯定对nginx不陌生,nginx作为目前占有率第一的web服务器,本身作为静态web服务器、反向代理服务和四层、七层负载均衡器都有着非常优秀的表现。但是对于web服务器而言,nginx的很多功能都偏向于静态web应用,也就是说它的动态处理能力是有一定的缺失的。举个最简单的例子,nginx无法在配置中直接进行一个条件以上的逻辑判断,这对于一些架构设计来说有着较大的不便。OpenResty的诞生就是为了解决这个问题。
官方对于OpenResty的介绍是:
个人简单的理解就是OpenResty=nginx+lua+第三方库,事实上nginx本身也可以通过编译的方式添加lua的支持,同时nginx也可以通过编译的方式添加第三方库的支持,OpenResty则是直接将其全部打包在一起。且不讨论OpenResty作为全能型web服务器是否能够满足所有的业务需求,就其突出的动态处理能力就能给我们的系统架构带来很大的灵活性和额外的多样化处理能力。
2、yum安装
2.1 配置yum源
openresty官网有提供各种主流Linux发行版的预编译包,如果没有特殊需求,一般可以直接使用官方提供的安装包进行安装。
下面我们以centos
为例进行示范:
# add the yum repo:
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/
# update the yum index:
sudo yum check-update
2.2 rpm包介绍
openresty的yum源中除了openresty本身之外,还有一些其他的工具包和依赖包,我们可以这样查看所有可用的rpm包,对于这些rpm包的详细内容解说可以查看官网的文档。
sudo yum --disablerepo="*" --enablerepo="openresty" list available
下面简单介绍几个重点的rpm包。
2.2.1 openresty
这是用于核心 OpenResty 服务的生产版本。也就是整个openresty中提供主要服务的核心功能文件。在使用rpm包安装的情况下,系统中的openresty指令会链接到/usr/bin/openresty
,而/usr/bin/openresty
实际上是/usr/local/openresty/nginx/sbin/nginx
的软链接
实际上/usr/local/openresty/nginx/sbin/nginx
才是整个openresty的nginx可执行文件的本体,我们加上-V参数即可查看它包括编译模块在内的详细情况。之所以要将其软链接到/usr/bin/openresty
,官方解释是为了避免和机器上已有的nginx发生冲突。
2.2.2 openresty-resty
这个包里面有 resty
命令行程序,这个工具包主要依赖于 resty-cli 这个项目,这个包依赖标准 的perl
包以及上面提到的 openresty
包才能正常工作。默认情况下它是指向/usr/bin/resty
目录的一个环境变量,并且源文件在/usr/local/openresty/bin/resty
个人认为这个工具包的主要作用就是方便我们进行一些简单的命令调试
$ resty -e 'ngx.say("hello")'
hello
2.2.3 openresty-doc
顾名思义这个rpm包最重要的功能就是提供文档查询,一般来说需要注意使用UTF-8编码的Terminal即可。和man命令有些类似,如果我们想要查询openresty中某个模块的相关文档,只需要使用restydoc
命令即可。
# 我们可以直接输入对应的模块命查看对应的文档
restydoc ngx_lua
restydoc ngx_lua_upstream
restydoc ngx_http_v2_module
restydoc luajit
# 也可以加入-s参数查看某条指令或某个部分的文档
restydoc -s content_by_lua
restydoc -s proxy_pass
2.2.4 openresty-openssl
这是openresty官方维护的 OpenSSL 库。为了节省开销,这个版本禁用了在构建中对于多线程的支持。此外,这个版本最重要的是OpenResty官方加入了他们自己的一些补丁包来支持一些openssl的最新特性,同时也做了一些优化使得比较旧的系统也能够支持最新版本的openssl。同理,不仅是openssl,openresty自己维护的pcre和zlib库也是有这种的目的。
2.2.5 其他
更多详细的包介绍可以查看官方的英文文档,介绍的比较全面,一般来说就是主要的几个工具软件如openresty、openssl、pcre、zlib等等的主要生产版本、debug版本、asan版本、valgrind版本等,这里不做赘述。
2.3 rpm包安装openresty
虽然上面介绍了很多个openresty相关的包,但是实际上安装的时候只需要安装openresty,其他的依赖关系会由yum自动处理。注意openresty-doc、openresty-resty等是可选安装的包。
# add the yum repo:
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/
# update the yum index:
sudo yum check-update
# use yum to install
yum install openresty
安装完成之后会在默认的目录路径/usr/local/openresty/nginx
下面生成对应的配置文件目录,我们将其和yum安装的nginx和编译安装的nginx对比可以发现几乎是一致的。
# 使用yum安装的nginx
[root@tiny-test nginx]# pwd
/etc/nginx
[root@tiny-test nginx]# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
# 源码编译安装的nginx
[root@tiny-test nginx]# pwd
/home/nginx
[root@tiny-test nginx]# ls
client_body_temp conf fastcgi_temp html logs sbin scgi_temp uwsgi_temp
# 使用yum安装的openresty目录下的nginx
[root@tiny-test nginx]# pwd
/usr/local/openresty/nginx
[root@tiny-test nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
我们简单写一个nginx.conf配置文件用来测试即可,文件使用默认路径的/usr/local/openresty/nginx/conf/nginx.conf
。
[root@tiny-test conf]# openresty -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
# configuration file /usr/local/openresty/nginx/conf/nginx.conf:
user root;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main
'$remote_addr | [$time_local] | $status | $scheme | '
'$server_name | request= $request | request_uri= $request_uri | http_referer= $http_referer | '
'UA= $http_user_agent | $request_time | $ssl_protocol | $ssl_cipher | '
'remote_port= $remote_port | $request_id';
access_log logs/access.log main;
server {
listen 8080;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
}
}
分别使用curl和浏览器测试均正常
[root@tiny-test conf]# curl 10.224.192.144:8080
<p>hello, world</p>
日志中也能看到对应的操作
[root@tiny-test conf]# tail -f ../logs/access.log
10.224.192.144 | [12/Mar/2021:14:42:25 +0800] | 200 | http | | request= GET / HTTP/1.1 | request_uri= / | http_referer= - | UA= curl/7.29.0 | 0.000 | - | - | remote_port= 34626 | 602882ad9b5cf910bea2f6d13ce4bf5e
10.228.18.249 | [12/Mar/2021:14:42:51 +0800] | 200 | http | | request= GET / HTTP/1.1 | request_uri= / | http_referer= - | UA= Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36 | 0.000 | - | - | remote_port= 7148 | 5a91256294bf307ed62f48a06905dbc0
10.228.18.249 | [12/Mar/2021:14:42:51 +0800] | 200 | http | | request= GET /favicon.ico HTTP/1.1 | request_uri= /favicon.ico | http_referer= http://10.224.192.144:8080/ | UA= Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36 | 0.000 | - | - | remote_port= 7148 | 372e7402db9f396950dae48b66a7e1ce
再查看端口监听和服务进程都正常
那么就可以基本判定这次的安装是顺利安装成功了。
3、源码编译安装
如果需要进行定制化,那么从源码开始进行编译安装就是我们的不二之选。首先我们去官网下载最新版本的openresty源码包并解压。
cd /home
wget https://openresty.org/download/openresty-1.19.3.1.tar.gz
tar -zxvf openresty-1.19.3.1.tar.gz
3.1 编译环境
[root@tiny-test home]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.9.2009 (Core)
Release: 7.9.2009
Codename: Core
同时我们还需要准备编译使用的工具如编译器等,对于CentOS7,我们可以简单的安装整个开发工具包Development Tools
。
yum grouplist
yum groupinstall "Development Tools" -y
3.2 准备openssl、pcre和zlib
和nginx一样,我们手动下载准备最新版本的openssl、pcre和zlib,注意这三个库只需要下载解压,并不需要提前进行安装。
# 下载
wget https://www.openssl.org/source/openssl-1.1.1j.tar.gz
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
wget https://zlib.net/zlib-1.2.11.tar.gz
# 解压
tar -zxvf openssl-1.1.1j.tar.gz
tar -zxvf pcre-8.44.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
3.3 编译安装
和nginx的编译安装一样,我们可以自定义编译安装的模块,这里可以查看openresty模块的官方说明。
cd /home/openresty-1.19.3.1/
./configure --prefix=/home/openresty --with-openssl=/home/openssl-1.1.1j --with-pcre=/home/pcre-8.44 --with-zlib=/home/zlib-1.2.11 --with-http_realip_module --with-http_stub_status_module --with-debug --with-http_ssl_module --with-http_v2_module
gmake -j8
gmake install
最后在安装完成的时候我们可以看到会把编译的openresty目录下的/home/openresty/nginx/sbin/nginx
软链接到/home/openresty/bin/openresty
,逻辑和yum安装的openresty一样
如果想要方便全局操作,可以创建一个软链接。
ln -s /home/openresty/bin/openresty /usr/local/bin/openresty
当然要是觉得这样子的目录不好操作,我们也可以在编译的时候多加一些参数,将常用的目录都手动指定,操作起来就和nginx几乎没有差别了,下面简单列举一个例子:
./configure \
--prefix=/home/openresty \
--sbin-path=/home/openresty/bin/nginx \
--conf-path=/home/openresty/conf/nginx.conf \
--http-log-path=/home/openresty/logs/access.log \
--error-log-path=/home/openresty/logs/error.log \
--pid-path=/home/openresty/logs/nginx.pid \
--lock-path=/home/openresty/logs/nginx.lock \
--http-client-body-temp-path=/home/openresty/client_body_temp/ \
--http-proxy-temp-path=/home/openresty/proxy_temp/ \
--http-fastcgi-temp-path=/home/openresty/fastcgi_temp/ \
--http-uwsgi-temp-path=/home/openresty/uwsgi_temp/ \
--http-scgi-temp-path=/home/openresty/scgi_temp/ \
--with-openssl=/home/openssl-1.1.1k \
--with-pcre=/home/pcre-8.44 \
--with-zlib=/home/zlib-1.2.11 \
--with-http_realip_module \
--with-http_stub_status_module \
--with-debug \
--with-http_ssl_module \
--with-http_v2_module \
--build=tinychen-build \
--dry-run
4、OpenResty操作
openresty的操作指令和nginx是完全一致的。具体如下:
[root@tiny-test home]# nginx -h
nginx version: nginx/1.19.8
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /etc/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
[root@tiny-test home]# openresty -h
nginx version: openresty/1.19.3.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/openresty/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
-h
打印帮助菜单-v
小写的v是打印版本并退出-V
大写的V是打印编译参数和版本信息并退出-t
小写的t是测试配置文件是否有错误并退出-T
大写的T是测试配置文件是否有误并且打印出生效的配置文件并退出,如果nginx.conf里面使用了include命令添加了其他的配置文件,也会一并拼接打印出来-q
在测试期间不打印非错误信息,适合debug使用-s
发送信号控制master进程,分别为stop
(暴力停止),quit
(优雅停止),reopen
(重新打开日志文件),reload
(优雅重启)-p
通过-p参数来指定实际的工作目录,例如我们有两个不同的测试项目,则可以通过openresty -p /path/to/app
在启动的时候指定不同的工作目录,从而实现允许多个不同的 OpenResty 应用共享同一个 OpenResty 服务程序的效果-c
通过-c参数来指定不同的配置文件而非只是默认路径下的配置文件-g
在启动的时候设定配置文件之外的全局指令,例如openresty -g "pid /var/run/nginx.pid; worker_processes 8;"