粤都财经网_广东省最专业的财经资讯门户

nginx-2:部署生产级openresty

2020-03-30 09:19:21 来源:互联网 阅读:-

目录

(1).下载源码

(2).openrestry低版本编译命令

(3).openrestry高版本编译命令

1.编译命令

2.注意事项

3.编译错误与解决

(4).openrestry安装第三方库lua_resty_http

1.安装第三方库lua_resty_http

2.安装第三方库nginx-lua-prometheus

(5).使用lua所必须的模块(openresty自带)

(6).configure参数详解

(7).相关阅读

(8).相关阅读


(1).下载源码


https://openresty.org/cn/download.html

我下载的是最新版本:openresty-1.15.8.1.tar.gz


tar -xzvf openresty-VERSION.tar.gz

cd openresty-VERSION/

#需要安装各种组件以适用于生产环境

下载第三方插件:

git clone https://github.com/replay/ngx_http_consistent_hash.git

后边安装openresty时有参数指向上述工程的本地地址。


(2).openrestry低版本编译命令


低版本编译时需要手动指定很多组件(使用高版本不用指定下述这么多):

./configure --prefix=/app/3rd/nginx/openresty --with-cc-opt=-O2 --add-module=build/ngx_devel_kit-0.3.1rc1 --add-module=build/ngx_stream_lua-0.0.7 --add-module=build/iconv-nginx-module-0.14 --add-module=build/echo-nginx-module-0.61 --add-module=build/xss-nginx-module-0.06 --add-module=build/ngx_coolkit-0.2 --add-module=build/set-misc-nginx-module-0.32 --add-module=build/form-input-nginx-module-0.12 --add-module=build/encrypted-session-nginx-module-0.08 --add-module=build/srcache-nginx-module-0.31 --add-module=build/ngx_lua-0.10.15 --add-module=build/ngx_lua_upstream-0.07 --add-module=build/headers-more-nginx-module-0.33 --add-module=build/array-var-nginx-module-0.05 --add-module=build/memc-nginx-module-0.19 --add-module=build/redis-nginx-module-0.3.7 --add-module=build/rds-json-nginx-module-0.15 --add-module=build/rds-csv-nginx-module-0.09 --with-ld-opt=-Wl,-rpath,/app/3rd/nginx/openresty/luajit/lib --add-module=/app/3rd/src-install/ngx_http_consistent_hash --with-http_stub_status_module --with-pcre-jit --with-stream_ssl_preread_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-openssl=/opt/openresty/openssl-1.1.0i --with-dtrace-probes --with-stream --with-stream_ssl_module --with-pcre=/opt/openresty/pcre-8.40


(3).openrestry高版本编译命令


1.15.8.1+,很多module不需要手工指定,默认安装:


1.编译命令


./configure --prefix=/app/3rd/nginx/openresty --with-cc-opt=-O2 --add-module=build/iconv-nginx-module-0.14 --with-ld-opt=-Wl,-rpath,/app/3rd/nginx/openresty/luajit/lib --add-module=/app/3rd/src-install/ngx_http_consistent_hash --with-http_stub_status_module --with-pcre-jit --with-stream_ssl_preread_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-openssl=/opt/openresty/openssl-1.1.0i --with-dtrace-probes --with-stream --with-stream_ssl_module --with-pcre=/opt/openresty/pcre-8.40

gmake

gmake install


2.注意事项


--add-module=build/ngx_stream_lua-0.0.7不需要加,因为:

ngx_stream_lua_module is already enabled in OpenResty by default. Your linker errors clearly indicate that. You should remove the option --add-module=build/ngx_stream_lua-0.0.7.

官方git issue回复:

https://github.com/openresty/openresty/issues/507

multiple definition of `ngx_http_rds_csv_output_more_field_data':表示add-module中的已经自动包含,不需要add-module主动指定。


3.编译错误与解决


3.1.编译错误1


configure阶段报错:the HTTP rewrite module requires the PCRE library


./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel与openssl-devel解决问题

yum -y install pcre-devel openssl openssl-devel


3.2.编译错误2


gmake阶段报错:dtrace command not foud错误:

安装dtrace

git clone https://github.com/dtrace4linux/linux.git

有可能提示libgcc需要upgrade,执行yum upgrade libgcc即可。

centos执行:tools/get-deps-fedora.sh安装依赖。

make all

make install

make load


3.3.编译错误3

gmake阶段报错:/opt/openresty/openssl-1.1.0i: No such file or directory

https://github.com/openssl/openssl/releases?after=OpenSSL_1_1_1-pre9

下载:

wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_0i.tar.gz

正常安装openssl的步骤:

./config --prefix=/opt/openresty/openssl-1.1.0i

make

make install

但是openrestry依赖的openssl不能这样安装,需要把openssl的源码copy到目录:

/opt/openresty/openssl-1.1.0i

然后执行openrestry阶段时,会自动到上边目录部署openssl.

错误提示(gmake阶段):collect2: error: ld returned 1 exit status

网上相同问题的解决方式:

https://blog.csdn.net/iaiot/article/details/78273921

openrestry执行configure时指定pcre目录。也可以参考官方文档:https://openresty.org/en/installation.html

./configure --with-pcre=../pcre-8.40 -j4

pcre8.40下载地址:

https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz

all version:

https://ftp.pcre.org/pub/pcre/

但是又遇到了另一个问题:

configure: error: You need a C++ compiler for C++ support.

解决方法:

yum install -y gcc gcc-c++

/opt/openresty/pcre-8.40/missing: line 81: automake-1.15: command not found:

wget http://ftp.gnu.org/gnu/automake/automake-1.15.tar.gz

./bootstrap.sh

./configure

make

make install


(4).openrestry安装lua第三方库


1.安装第三方库lua_resty_http


参考:

https://git.102no.com/2018/12/21/lua-resty-http/

第一步

首先找到项目地址:https://github.com/pintsized/lua-resty-http

第二步

然后将 lua-resty-http/lib/resty/ 目录下的 http.lua 和 http_headers.lua 两个文件拷贝到openresty/lualib/resty 目录下即可,

OpenResty 安装目录为 openresty)。不需要重启。(少数需要清空 Openresty shared_dict 数据的情况需要重启 )。


2.安装第三方库nginx-lua-prometheus


openrestry添加prometheus监控的Lua扩展:

参考:

https://www.jianshu.com/p/3341db428978

git clone https://github.com/knyar/nginx-lua-prometheus

prometheus.lua拷贝到lualib下。


(5).使用lua所必须的模块(openresty自带)


lua-nginx-module:是 Lua 的最基本模块。


luajit:官网,luajit 是采用 C 语言编写的 Lua 语言解释器。当然如果你不想用 luajit 的话,也可以 yum 安装 lua 并提供给 Nginx,不推荐那样而已。


ngx_devel_kit:一般简称 NDK,是一个供其它模块使用的拓展 Nginx 核心功能的模块,第三方模块开发可以基于它快速实现。强烈建议编译 Lua 时把它也带上,虽然不是必须的,缺少该依赖项时例如 set_by_lua* 和 ndk.set_var.* API 都将被自动禁用。


(6).configure参数详解


./configure

#指定安装的位置

--prefix=/app/3rd/nginx/openresty/nginx

#设置C编译器参数:

# -O1 提供基础级别的优化

# -O2提供更加高级的代码优化,会占用更长的编译时间

# -O3提供最高级的代码优化

--with-cc-opt=-O2

#一般简称 NDK,是一个供其它模块使用的拓展 Nginx 核心功能的模块;

# 第三方模块开发可以基于它快速实现。

# 强烈建议编译 Lua 时把它也带上,虽然不是必须的,缺少该依赖项时例如 set_by_lua* 和 ndk.set_var.* API 都将被自动禁用

--add-module=build/ngx_devel_kit-0.3.1rc1

#编码转换模块

--add-module=build/iconv-nginx-module-0.14

#nginx echo模块是在nginx程序上扩展了echo输出字符的功能, 对于调试很方便。

--add-module=build/echo-nginx-module-0.61

#Nginx 中的原生跨站点脚本支持

--add-module=build/xss-nginx-module-0.06

#操作cookie的模块

--add-module=build/ngx_coolkit-0.2

#模块是标准的HttpRewriteModule指令的扩展,提供更多的功能,如URI转义与非转义、JSON引述、Hexadecimal/MD5/SHA1/Base32/Base64编码与解码、随机数等等。

--add-module=build/set-misc-nginx-module-0.32

#作用是解析post请求中的参数。

--add-module=build/form-input-nginx-module-0.12

#encrypt and decrypt nginx variable values。

--add-module=build/encrypted-session-nginx-module-0.08

#针对代理层缓存,我们可以将静态资源放入cdn或者本地页面缓存加快用户访问速度,缓解服务器压力。比如结合pika实现。

--add-module=build/srcache-nginx-module-0.31

#Lua 的最基本模块。

--add-module=build/ngx_lua-0.10.15

#Lua&upstream模块。

--add-module=build/ngx_lua_upstream-0.07

#Set and clear input and output headers...more than "add"!

#提供操作header的各种方法。

--add-module=build/headers-more-nginx-module-0.33

#Add support for array variables to NGINX config files

#增加数组变量的支持。

--add-module=build/array-var-nginx-module-0.05

#提供memcached的各种操作。

--add-module=build/memc-nginx-module-0.19

#提供redis的各种操作。

--add-module=build/redis-nginx-module-0.3.7

#提供db的操作支持,json格式。

--add-module=build/rds-json-nginx-module-0.15

#提供db的操作支持,csv格式。

--add-module=build/rds-csv-nginx-module-0.09

#lua相关模块。

--add-module=build/ngx_stream_lua-0.0.7

#设置将会在链接(linking)过程中使用的额外参数。

--with-ld-opt=-Wl,-rpath,/app/3rd/nginx/openresty/luajit/lib

#通过一致性哈希算法来选择合适的后端节点。

--add-module=/tmp/open_install/openresty-1.13.6.2/../ngx_http_consistent_hash-master

#模块主要用于查看Nginx的一些状态信息。

--with-http_stub_status_module

# 打开pcre JIT支持,比不使用快好几倍。

--with-pcre-jit

#手头没证书,给https做代理时要用到这个功能。

--with-stream_ssl_preread_module

#在启用Mail模块后,单独地禁用pop3模块

--without-mail_pop3_module

#在启用mail模块后,单独地禁用IMAP模块

--without-mail_imap_module

#在启用mail模块后,单独地禁用smtp模块

--without-mail_smtp_module

#让nginx支持http/2。

--with-http_v2_module

#让nginx支持https。

--with-http_ssl_module

#获取真实IP模块。

--with-http_realip_module

#在响应之前或者之后追加文本内容,比如想在站点底部追加一个js或者css,可以使用这个模块来实现,这个模块和淘宝开发的nginx footer模块有点类似,但是还是有不同. 这个模块需要依赖子请求,nginx footer依赖nginx写死的配置.

--with-http_addition_module

#ngx_http_auth_request_module 第三方认证

# 编译 Nginx 时需要添加该模块 --with-http_auth_request_module

# 该模块可以将客户端输入的用户名、密码 username:password 通过 Base64 编码后写入 Request Headers 中

# 例如:wang:wang -> Authorization:Basic d2FuZzp3YW5n=

# 然后通过第三方程序解码后跟数据库中用户名、密码进行比较,Nginx 服务器通过 header 的返回状态判断是否认证通过。

--with-http_auth_request_module

#安全连接,用于防盗链。

#https://juejin.im/post/5bce737551882576e3102733

--with-http_secure_link_module

#如果你想提供从一个目录中随机选择文件的索引文件,那么这个模块需要被激活

--with-http_random_index_module

#nginx默认安装ngx_http_gzip_module,采用的是chunked方式的动态压缩,静态压缩需要使用http_gzip_static_module这个模块,进行pre-compress。

#模块 ngx_http_gzip_static_module 允许发送以".gz"作为文件扩展名的预压缩文件,以替代发送普通文件。需要指定 --with-http_gzip_static_module编译选项:

--with-http_gzip_static_module

#该模块实现了替代过滤,在响应中用一个字符串替代另一个字符串

--with-http_sub_module

#启用这个模块将激活使用WebDAV的配置指令。注意:这个模块也只在有需要使用的基础上启用,如果配置不正确,它将带来安全问题。

--with-http_dav_module

#如果需要提供Flash流媒体视频文件,那么该模块将会提供伪流媒体

--with-http_flv_module

#这个模块支持H.264/AAC文件伪流媒体

--with-http_mp4_module

#在服务端发送数据之前进行压缩也很重要。GZip 压缩就是其中的一种压缩方式。

#Nginx 默认就能提供上述功能,提供两个 GZip 压缩的增强插件:http_gzip_static_module,http_gunzip_module

--with-http_gunzip_module

#使用aio threads,需要添加--with-threads配置

#nginx这样配置:

server {              location /one {            aio threads=one;        }        location /two {            aio threads=two;        }    }

--with-threads

#指定nginx ssl模块依赖的ssl库

--with-openssl=/opt/openresty/openssl-1.1.0i

#openresty使用火焰图排查性能问题时要用到dtrace。

#参考文章:https://juejin.im/post/59ce27fef265da065b66d54b

--with-dtrace-probes

#把内网ip端口映射到外网地址。比如如果你想把云数据库(mysql等)开放外网地址。需要增加编译参数:--with-stream 与 --with-stream_ssl_module。

--with-stream

--with-stream_ssl_module


(7).相关阅读


nginx-1:生产级别nginx高性能配置

kubernetes-9:nginx-ingress容器化


推荐阅读:华为手机小技巧