平滑升级旧版nginx,支持lua动态代理转发多个云平台VNC画面

nginx是部署在华为欧拉的docker容器中,版本是2203sp1.x86_64

查看旧版nginx的版本与编译配置信息:

shell 复制代码
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中。

shell 复制代码
cd luajit2
# 编译
make
# 安装
make install PREFIX=/usr/local/luajit2

编译成功后如图所示:

安装成功后如图所示:

加入到环境变量中:

shell 复制代码
export LUAJIT_LIB=/usr/local/luajit2/lib
export LUAJIT_INC=/usr/local/luajit2/include/luajit-2.1

编译 nginx

shell 复制代码
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

shell 复制代码
cd lua-resty-core-0.1.27
# 安装
make install PREFIX=/usr/local

成功后如图所示:

替换nginx可执行文件

备份旧的二进制程序(旧程序的路径可通过编译配置信息的--sbin-path获知):

shell 复制代码
mv /usr/sbin/nginx /usr/sbin/nginx.bak

将新生成的二进制程序替换旧的(新生成的二进制程序在objs目录中):

shell 复制代码
cp /root/nginx-1.24.0/objs/nginx /usr/sbin/nginx

查看nginx版本信息,执行nginx自检命令,查看是否升级成功:

shell 复制代码
nginx -V # 查看版本

打印下面的信息说明升级成功:

在nginx中引用 lua-resty-core 模块

修改nginx配置文件,在http全局配置添加lua配置:

shell 复制代码
lua_package_path "/usr/local/lib/lua/?.lua;;"; # 设置lua模块
lua_shared_dict dynamic_upstreams 1m; # 共享字典 有需要可以加上

修改之前的nginx wss配置文件,加上lua脚本(业务说明:我需要通过nginx lua动态代理来连接不同第三方云平台虚拟机vnc,从而避免前端的跨域及证书问题):

shell 复制代码
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服务:

shell 复制代码
systemctl restart nginx

前端组织url访问/ws/vnc,成功实现nginx lua动态转发VNC连接:

相关推荐
不念霉运23 分钟前
Gitee DevOps:中国企业数字化转型的“本土化加速器“
运维·gitee·团队开发·代码规范·devops·代码复审
安迪小宝41 分钟前
6 任务路由与负载均衡
运维·python·celery
遇见火星1 小时前
jenkins流水线常规配置教程!
运维·docker·jenkins
LunarCod2 小时前
Ubuntu使用Docker搭建SonarQube企业版(含破解方法)
linux·运维·服务器·ubuntu·docker·开源·sonarqube
什么半岛铁盒2 小时前
Linux信号的保存
linux·运维·网络
noravinsc3 小时前
国产化中间件 替换 nginx
运维·nginx·中间件
惜.己3 小时前
Linux常用命令(十四)
linux·运维·服务器
linkingvision4 小时前
H5S 视频监控AWS S3 对象存储
linux·运维·aws·视频监控s3对象存储
doupoa4 小时前
Fabric 服务端插件开发简述与聊天事件监听转发
运维·python·fabric
BillKu4 小时前
服务器多JAR程序运行与管理指南
运维·服务器·jar