nginx是部署在华为欧拉的docker容器中,版本是2203sp1.x86_64
查看旧版nginx的版本与编译配置信息:
nginx -V
nginx version: nginx/1.24.0
built by gcc 10.3.1 (GCC)
built with OpenSSL 1.1.1wa 16 Nov 2023
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt=‘-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/generic-hardened-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fPIC -D_FORTIFY_SOURCE=2 -O2 -Wtrampolines -fsigned-char’ --with-ld-opt=‘-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/generic-hardened-ld -Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack’ --add-module=/usr/lib64/nginx/modules/nginx_upstream_check_module-master
根据版本信息,下载好升级所需的组件:
nginx 1.24.0
lua-nginx-module 0.10.25
luajit2 v2.1-20231117
ngx_devel_kit 0.3.3
lua-resty-core 0.1.27
以上版本是依据 Openresty 1.25.3的依赖列表配置的,想找1.24版本但是没有找到,所以就找了一个临近版。
注意:lua-nginx-module 从 0.10.16 开始部分函数被拆分到 lua-resty-core 中,例如 ngx.req.get_uri_args(),所以需要安装 lua-resty-core,同时 lua-resty-core 对 lua-nginx-module 的版本有要求需要一一对应,对应关系可以在 lib\resty\core\base.lua 中找到。
将下载好的文件全部解压到服务器的root目录下,如图所示:
编译安装 luajit
将luajit2编译安装到/usr/local/luajit2
中。
cd luajit2
# 编译
make
# 安装
make install PREFIX=/usr/local/luajit2
编译成功后如图所示:
安装成功后如图所示:
加入到环境变量中:
export LUAJIT_LIB=/usr/local/luajit2/lib
export LUAJIT_INC=/usr/local/luajit2/include/luajit-2.1
编译 nginx
cd nginx-1.24.0
# 编译参数配置
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/generic-hardened-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fPIC -D_FORTIFY_SOURCE=2 -O2 -Wtrampolines -fsigned-char' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/generic-hardened-ld -Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack' --add-module=/usr/lib64/nginx/modules/nginx_upstream_check_module-master --with-ld-opt='-lpcre -Wl,-rpath,/usr/local/luajit2/lib' --add-module=/root/ngx_devel_kit-0.3.3 --add-module=/root/lua-nginx-module-0.10.25
# 编译
make
编译成功后如图所示:
安装 lua-resty-core
cd lua-resty-core-0.1.27
# 安装
make install PREFIX=/usr/local
成功后如图所示:
替换nginx可执行文件
备份旧的二进制程序(旧程序的路径可通过编译配置信息的--sbin-path
获知):
mv /usr/sbin/nginx /usr/sbin/nginx.bak
将新生成的二进制程序替换旧的(新生成的二进制程序在objs
目录中):
cp /root/nginx-1.24.0/objs/nginx /usr/sbin/nginx
查看nginx版本信息,执行nginx自检命令,查看是否升级成功:
nginx -V # 查看版本
打印下面的信息说明升级成功:
lua_package_path "/usr/local/lib/lua/?.lua;;"; # 设置lua模块
lua_shared_dict dynamic_upstreams 1m; # 共享字典 有需要可以加上
修改之前的nginx wss配置文件,加上lua脚本(业务说明:我需要通过nginx lua动态代理来连接不同第三方云平台虚拟机vnc,从而避免前端的跨域及证书问题):
location /ws/vnc {
set $target_upstream '';
access_by_lua_block {
local args = ngx.req.get_uri_args()
local target = args["target"]
if not target then
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.say("Missing 'target' parameter")
ngx.exit(ngx.HTTP_BAD_REQUEST)
end
ngx.var.target_upstream = target
}
proxy_ssl_verify off;
proxy_pass $target_upstream;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Sec-WebSocket-Key $http_sec_websocket_key;
proxy_set_header Sec-WebSocket-Protocol $http_sec_websocket_protocol;
proxy_set_header Sec-WebSocket-Version $http_sec_websocket_version;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
保存配置后,重启nginx服务:
systemctl restart nginx