最近,遇到一个问题,就是我们CMS系统制作的产品页面和产品列表页面,发布到nginx服务器上,因为业务要求,客户看到的链接必须是短链接,当前的做法就是在nginx的配置中通过rewrite的方式做的。这个是静态的做法,当业务要修改短链接或者有新产品发布的话,就存在问题,要运维人员去修改nginx的配置文件,若这个运维忘记了或者改错了,或者业务运营人员找不到运维人员,会是什么效果?404呗。
针对这个问题,我希望通过lua+nginx+redis的方式,动态的加载这些链接的映射关系。
openresty对lua以及nginx的诸多模块都有很好的封装,所以,我就要自己在测试环境下尝试下,基于openresty的lua功能,首先就是部署openresty。
1. 下载openresty。从官网下载最新的版本。本博文案例用到的是openresty-1.11.2.2.tar.gz。
解压缩,首先,因为openresty(确切的说,是nginx)依赖pcre做正则引擎,还有要用openssl做安全引擎。所以,先要安装pcre和openssl。千万注意,要用源码,在安装openresty的时候,通过--with-<module>指定源码路径。
[root@localhost openresty-1.11.2.2]# ./configure --prefix=/usr/local/openresty --with-pcre=/opt/pcre2-10.23 --with-openssl=/opt/openssl-1.1.0e
。。。。。。。
Configuration summary
+ using PCRE library: /opt/pcre2-10.23
+ using OpenSSL library: /opt/openssl-1.1.0e
+ using system zlib library 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" cd ../..
Type the following commands to build and install:
gmake
gmake install
上面日志中可以看出来,我的pcre和openssl的版本。pcre是pcre2-10.23,openssl的版本是openssl-1.1.0e。
这时看上去一切ok,没问题,可以依据提示信息,执行gmake了。
2. 执行gmake
。。。。。。。。。。。。。。。
install apps/openssl -> /opt/openssl-1.1.0e/.openssl/bin/openssl
install ./tools/c_rehash -> /opt/openssl-1.1.0e/.openssl/bin/c_rehash
gmake[]: 离开目录“/opt/openssl-1.1.0e”
cc -c -I/opt/openresty-1.11.2.2/build/luajit-root/usr/local/openresty/luajit/include/luajit-2.1 -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -g -O2 -DNDK_SET_VAR -DNDK_UPSTREAM_LIST -DNDK_SET_VAR -DNDK_SET_VAR -DNDK_SET_VAR -DLUA_DEFAULT_PATH='"/usr/local/openresty/site/lualib/?.lua;/usr/local/openresty/site/lualib/?/init.lua;/usr/local/openresty/lualib/?.lua;/usr/local/openresty/lualib/?/init.lua"' -DLUA_DEFAULT_CPATH='"/usr/local/openresty/site/lualib/?.so;/usr/local/openresty/lualib/?.so"' -DNDK_SET_VAR -I src/core -I src/event -I src/event/modules -I src/os/unix -I ../ngx_devel_kit-0.3./objs -I objs/addon/ndk -I ../ngx_lua-0.10./src/api -I /opt/pcre2-10.23 -I /opt/openssl-1.1.0e/.openssl/include -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
In file included from src/core/ngx_core.h::,
from src/core/nginx.c::
src/core/ngx_regex.h:15:18: 致命错误:pcre.h:没有那个文件或目录
#include <pcre.h>
^
编译中断。
gmake[]: *** [objs/src/core/nginx.o] 错误
gmake[]: 离开目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
gmake[]: *** [build] 错误
gmake[]: 离开目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
gmake: *** [all] 错误
gmake有问题,如上红色的错误显示,找不到pcre.h,我到/opt/pcre2-10.23/src下面看,的确没有这个文件,莫非是版本的问题?我果断的更换了一个pcre的版本,没有用pcre2,选择的是pcre-8.40.再次configure并gmake。
。。。。。。。。。。。。。。。
-o objs/src/os/unix/ngx_linux_sendfile_chain.o \
src/os/unix/ngx_linux_sendfile_chain.c
cc -c -I/opt/openresty-1.11.2.2/build/luajit-root/usr/local/openresty/luajit/include/luajit-2.1 -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -g -O2 -DNDK_SET_VAR -DNDK_UPSTREAM_LIST -DNDK_SET_VAR -DNDK_SET_VAR -DNDK_SET_VAR -DLUA_DEFAULT_PATH='"/usr/local/openresty/site/lualib/?.lua;/usr/local/openresty/site/lualib/?/init.lua;/usr/local/openresty/lualib/?.lua;/usr/local/openresty/lualib/?/init.lua"' -DLUA_DEFAULT_CPATH='"/usr/local/openresty/site/lualib/?.so;/usr/local/openresty/lualib/?.so"' -DNDK_SET_VAR -I src/core -I src/event -I src/event/modules -I src/os/unix -I ../ngx_devel_kit-0.3./objs -I objs/addon/ndk -I ../ngx_lua-0.10./src/api -I /opt/pcre-8.40 -I /opt/openssl-1.1.0e/.openssl/include -I objs \
-o objs/src/event/ngx_event_openssl.o \
src/event/ngx_event_openssl.c
src/event/ngx_event_openssl.c: 在函数‘ngx_ssl_connection_error’中:
src/event/ngx_event_openssl.c:2048:21: 错误:‘SSL_R_NO_CIPHERS_PASSED’未声明(在此函数内第一次使用)
|| n == SSL_R_NO_CIPHERS_PASSED /* 182 */
^
src/event/ngx_event_openssl.c::: 附注:每个未声明的标识符在其出现的函数内只报告一次
gmake[]: *** [objs/src/event/ngx_event_openssl.o] 错误
gmake[]: 离开目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
gmake[]: *** [build] 错误
gmake[]: 离开目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
gmake: *** [all] 错误
这次看来,没有pcre的那个头文件找不到的问题了,走的更远一步了,这次报错是openssl有问题,这个就不是那么简单好查看了,去网络上查看吧,找到原因,是因为openssl的API改动很大,对应openresty的使用,我选择了老版本的openssl,将当前的1.1.0e版本的改成1.0.1u的了。再次configure并gamke。
。。。。。。。。。。。。。
objs/addon/src/ngx_http_rds_csv_output.o \
objs/ngx_modules.o \
-L/opt/openresty-1.11.2.2/build/luajit-root/usr/local/openresty/luajit/lib -Wl,-rpath,/usr/local/openresty/luajit/lib -Wl,-E -ldl -lpthread -lcrypt -L/opt/openresty-1.11.2.2/build/luajit-root/usr/local/openresty/luajit/lib -lluajit-5.1 -lm -ldl /opt/pcre-8.40/.libs/libpcre.a /opt/openssl-1.0.1u/.openssl/lib/libssl.a /opt/openssl-1.0.1u/.openssl/lib/libcrypto.a -ldl -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/openresty/nginx|" \
-e "s|%%PID_PATH%%|/usr/local/openresty/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/usr/local/openresty/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/usr/local/openresty/nginx/logs/error.log|" \
< docs/man/nginx. > objs/nginx.
gmake[]: 离开目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
gmake[]: 离开目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
这次,没有openssl的问题了。gmake的过程ok。
3. gmake install
。。。。。。。。。。
gmake[]: 进入目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
gmake -f objs/Makefile install
gmake[]: 进入目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
test -d '/usr/local/openresty/nginx' || mkdir -p '/usr/local/openresty/nginx'
test -d '/usr/local/openresty/nginx/sbin' \
|| mkdir -p '/usr/local/openresty/nginx/sbin'
test ! -f '/usr/local/openresty/nginx/sbin/nginx' \
|| mv '/usr/local/openresty/nginx/sbin/nginx' \
'/usr/local/openresty/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/openresty/nginx/sbin/nginx'
test -d '/usr/local/openresty/nginx/conf' \
|| mkdir -p '/usr/local/openresty/nginx/conf'
cp conf/koi-win '/usr/local/openresty/nginx/conf'
cp conf/koi-utf '/usr/local/openresty/nginx/conf'
cp conf/win-utf '/usr/local/openresty/nginx/conf'
test -f '/usr/local/openresty/nginx/conf/mime.types' \
|| cp conf/mime.types '/usr/local/openresty/nginx/conf'
cp conf/mime.types '/usr/local/openresty/nginx/conf/mime.types.default'
test -f '/usr/local/openresty/nginx/conf/fastcgi_params' \
|| cp conf/fastcgi_params '/usr/local/openresty/nginx/conf'
cp conf/fastcgi_params \
'/usr/local/openresty/nginx/conf/fastcgi_params.default'
test -f '/usr/local/openresty/nginx/conf/fastcgi.conf' \
|| cp conf/fastcgi.conf '/usr/local/openresty/nginx/conf'
cp conf/fastcgi.conf '/usr/local/openresty/nginx/conf/fastcgi.conf.default'
test -f '/usr/local/openresty/nginx/conf/uwsgi_params' \
|| cp conf/uwsgi_params '/usr/local/openresty/nginx/conf'
cp conf/uwsgi_params \
'/usr/local/openresty/nginx/conf/uwsgi_params.default'
test -f '/usr/local/openresty/nginx/conf/scgi_params' \
|| cp conf/scgi_params '/usr/local/openresty/nginx/conf'
cp conf/scgi_params \
'/usr/local/openresty/nginx/conf/scgi_params.default'
test -f '/usr/local/openresty/nginx/conf/nginx.conf' \
|| cp conf/nginx.conf '/usr/local/openresty/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/openresty/nginx/conf/nginx.conf.default'
test -d '/usr/local/openresty/nginx/logs' \
|| mkdir -p '/usr/local/openresty/nginx/logs'
test -d '/usr/local/openresty/nginx/logs' \
|| mkdir -p '/usr/local/openresty/nginx/logs'
test -d '/usr/local/openresty/nginx/html' \
|| cp -R docs/html '/usr/local/openresty/nginx'
test -d '/usr/local/openresty/nginx/logs' \
|| mkdir -p '/usr/local/openresty/nginx/logs'
gmake[]: 离开目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
gmake[]: 离开目录“/opt/openresty-1.11.2.2/build/nginx-1.11.”
mkdir -p /usr/local/openresty/site/lualib /usr/local/openresty/site/pod /usr/local/openresty/site/manifest
ln -sf /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/bin/openresty
这么看来,openresty安装部署完毕,是不是要检查一下,openresty能否启用呢?先查看下openresty的文件目录结构,如下,含有lua的组件,测试lua,后续就靠它了。
[root@localhost openresty]# ll
总用量
drwxr-xr-x. root root 3月 : bin
drwxr-xr-x. root root 3月 : luajit
drwxr-xr-x. root root 3月 : lualib
drwxr-xr-x. root root 3月 : nginx
drwxr-xr-x. root root 3月 : pod
-rw-r--r--. root root 3月 : resty.index
drwxr-xr-x. root root 3月 : site
4.验证openresty是否安装成功
[root@localhost sbin]# pwd
/usr/local/openresty/nginx/sbin
[root@localhost sbin]# ./nginx
nginx: [emerg] bind() to 0.0.0.0: failed (: Address already in use)
nginx: [emerg] bind() to 0.0.0.0: failed (: Address already in use)
nginx: [emerg] bind() to 0.0.0.0: failed (: Address already in use)
nginx: [emerg] bind() to 0.0.0.0: failed (: Address already in use)
nginx: [emerg] bind() to 0.0.0.0: failed (: Address already in use)
报错,地址被占用了,看到这个第一时间想到的是这个机器上安装了基本版的nginx,于是乎,将openresty的nginx配置文件中listen的端口修改一下,不用默认的80,改成8090.重新启动nginx。这次没有地址被占用的错误了。在地址栏输入地址http://localhost:8090, 看到下面的内容,OK,部署完毕!
到此,openresty部署完毕,总结一下,这里的坑无外乎两点:
1》 openresty需要的pcre插件,不能用pcre2来代替。
2》 openssl的版本更替中,API的改动非常大,版本兼容有问题,openresty需要的openssl版本,一定要匹配。
解除上面两个坑之后,我的部署其实变成了openresty-1.11.2.2 + pcre-8.40 + openssl-1.0.1u。