地址 https://openresty.org/download/openresty-1.19.3.2.tar.gz
GitHub https://github.com/openresty/openresty
配置文件位置 /usr/local/openresty/nginx/conf/nginx.conf
日志名称 access.log access-2021-08-15.log error.log

安装

本次安装同时配置 ssl http2 和 brotli压缩算法
需要额外下载 openssl库 和 brotli算法 的支持库

下载源码

wget https://openresty.org/download/openresty-1.19.3.2.tar.gz

安装依赖

对于不同发行版,安装部分依赖的方式不太相同

Debian

apt-get install libpcre3-dev libssl-dev perl make build-essential curl

RedHat

yum install pcre-devel openssl-devel gcc curl

下面是共同的部分

配置 openssl 和 brotli

wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
tar -xzvf openssl-1.0.2l.tar.gz

得到 openssl-1.0.2l/


对于brotli,分别 clone 以下仓库

  • ngx_http_trim_filter_module(用于去掉响应多余空格)
  • ngx_brotli(nginx的br模块)
  • brotli(br算法)

其中,brotli要配置在 ngx_brotli/deps 下,需要先删除存在的brotli文件夹
另外,只有当使用https时,客户端才会发送Accept-Encoding:br,在不支持https的环境下建议使用gzip

git clone git@github.com:taoyuanyuan/ngx_http_trim_filter_module.git
git clone git@github.com:google/ngx_brotli.git
cd ngx_brotli/deps
rm -rf ./brotli
git clone git@github.com:google/brotli.git

现在得到了三个文件夹

openssl-1.0.2l/
ngx_brotli/
ngx_http_trim_filter_module/

将这三个文件夹转移到/usr/local/src

cp -R openssl-1.0.2l/ ngx_brotli/ ngx_http_trim_filter_module/ /usr/local/src

安装

切换到openresty目录下,执行以下configure以配置

cd openresty-1.19.3.2
./configure   --add-module=/usr/local/src/ngx_brotli \ 
		--with-http_v2_module \
		--with-openssl=/usr/local/src/openssl-1.0.2l \ 
		--add-module=/usr/local/src/ngx_http_trim_filter_module

无误后执行

make && make install

默认安装位置在/usr/local/openresty
最好在配置文件中中添加openresty,我的做法是,在.zshrc中插入一条alias,如下文所示

alias openresty="/usr/local/openresty/bin/openresty"

配置

大部分等同于nginx配置

开启压缩

注意:gzip 和 br 是可以同时开启的,在br算法不生效时使用gzip
在http块中配置

#gzip config
gzip on;
gzip_min_length 5k;
gzip_buffers 4 32k;
gzip_comp_level 5;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml text/javascript application/javascript;

# brotli config
brotli on;
brotli_static on; # 将静态文件预编译成br文件,条件满足直接使用
brotli_comp_level 6;
brotli_buffers 16 8k;
brotli_min_length 5k;
brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml text/javascript application/javascript;

开启限速

在http块中配置

$binary_remote_addr 表示按照ip地址限速
zone=空间名称:空间大小
rate=请求次数/时间 可以设置分钟和小时

# limit_req_zone config
limit_req_zone $binary_remote_addr zone=api:10m rate=30r/s;
limit_req_zone $binary_remote_addr zone=static:10m rate=60r/s;
limit_req_status 500; # when over max req num, return 500

在location配置

zone为对应的空间名称

location /v1 {
    limit_req zone=api burst=5 nodelay; # 5r/s per ip
}

日志分割功能

在server块中添加以下内容,$time_iso8601为内置变量,可以直接使用

if ( $time_iso8601 ~ "^(\d{4}-\d{2}-\d{2})") {
    set $time $1;
}

access_log  logs/access.$time.log;
error_log logs/error.log;

启动

由于安装环节已经配置了openresty的环境变量,所以这里以openresty别名举例

启动

# 启动 携带-c参数可指定配置文件位置
openresty
# 测试配置文件是否正常
openresty -t
# 停止
openresty -s stop
# 重载配置文件 适用于修改配置文件后重载openresty
openresty -s reload
# 重新打开配置文件 用于使用脚本分割日志后,重新创建日志文件
openresty -s reopen

基础配置

nginx path prefix: "/usr/local/openresty/nginx"
nginx binary file: "/usr/local/openresty/nginx/sbin/nginx"
nginx modules path: "/usr/local/openresty/nginx/modules"
nginx configuration prefix: "/usr/local/openresty/nginx/conf"
nginx configuration file: "/usr/local/openresty/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/openresty/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/openresty/nginx/logs/error.log"
nginx http access log file: "/usr/local/openresty/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

其他

OpenResty 的优势在于能直接使用 Lua 脚本对请求和响应做修改和限制,后续会更新 Lua脚本 相关内容