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

news/2025/2/26 5:39:02

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 # 查看版本

打印下面的信息说明升级成功:
在这里插入图片描述

nginx中引用 lua-resty-core 模块

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

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

前端组织url访问/ws/vnc,成功实现nginx lua动态转发VNC连接:
在这里插入图片描述


http://www.niftyadmin.cn/n/5868071.html

相关文章

短剧源码部署搭建小程序搭建IAA+IAP混合解锁模式

在当今数字化内容消费迅速增长的时代,短剧作为一种新兴的内容形式,凭借其短小精悍、节奏紧凑的特点,迅速吸引了大量用户。作为一名软件体验测试人员,我有幸体验了一款集创新与实用为一体的短剧小程序。这款小程序不仅在前端用户体…

【WordPress】发布文章时自动通过机器人推送到钉钉

在您的主题下functions.php中添加如下代码: function wpso_dingding_publish_notify($post_ID) {// 获取文章对象$post get_post($post_ID);// 检查是否是文章首次发布(即不是修订版)if (get_post_status($post_ID) publish && !g…

Jupyter Notebook中使用GPU进行计算

要在Jupyter Notebook中使用GPU进行计算,请按照以下步骤操作: 检查GPU可用性 在终端/Anaconda Prompt中运行以下命令以确认是否有可用的GPU: nvidia-smi安装所需的库 首先,在终端/Anaconda Prompt中,依次安装PyTorch及…

Windows前端开发IDE选型全攻略

Windows前端开发IDE选型全攻略 一、核心IDE对比矩阵 工具名称最新版本核心优势适用场景推荐指数引用来源VS Code2.3.5轻量级/海量插件/跨平台/Git深度集成全栈开发/中小型项目⭐⭐⭐⭐⭐14WebStorm2025.1智能提示/框架深度支持/企业级调试工具大型项目/专业前端团队⭐⭐⭐⭐47…

Deepseek 实战全攻略,领航科技应用的深度探索之旅

想玩转 Deepseek?这攻略别错过!先带你了解它的基本原理,教你搭建运行环境。接着给出自然语言处理、智能客服等应用场景的实操方法与代码。还分享模型微调、优化技巧,结合案例加深理解,让你全面掌握,探索科技…

树莓派4基于Debian GNU/Linux 12 (Bookworm)系统安装

1. 安装驱动程序和系统烧录软件 有两个软件,一个负责驱动安装,一个负责系统烧录。 1. 点击rpiboot_setup.exe进行驱动程序的安装 安装成功后: 2. 点击imager_1.8.5.exe进行系统烧录软件的安装 安装成功后: 2. 将电脑和树莓派连…

Apache IoTDB: 物联网时序数据库的分布式核心技术与实际应用

随着物联网(IoT)的快速发展,时序数据管理成为了一个重要的技术挑战。Apache IoTDB,作为Apache基金会的顶级项目,提供了一种高效、可扩展的解决方案来应对这一挑战。本文将深入探讨Apache IoTDB的分布式核心技术和其在实…

Java 设计模式 面试题及答案整理,最新面试题

Java中单例设计模式有哪些实现方式? Java中单例设计模式主要有以下几种实现方式: 1、懒汉式(线程不安全): 这种实现方式支持延迟加载,但是在多线程环境下不能保证单例的唯一性。 2、懒汉式(线…