平滑升级旧版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连接:

相关推荐
夸克App2 小时前
实现营销投放全流程自动化 超级汇川推出信息流智能投放产品“AI智投“
运维·人工智能·自动化
Rainbond云原生3 小时前
83k Star!n8n 让 AI 驱动的工作流自动化触手可及
运维·人工智能·自动化
木觞清3 小时前
深度对比评测:n8n vs Coze(扣子) vs Dify - 自动化工作流工具全解析
运维·自动化
中云时代-防御可测试-小余3 小时前
高防IP是如何防护DDoS攻击和CC攻击的
运维·服务器·tcp/ip·安全·阿里云·ddos·宽度优先
网硕互联的小客服4 小时前
如何模拟黑客攻击(Red Teaming)以测试服务器安全性
运维·服务器
樽酒ﻬق4 小时前
Kubernetes 常用运维命令整理
运维·容器·kubernetes
wt_cs5 小时前
身份证实名认证:通往数字安全与便捷生活的钥匙
运维·服务器
zym大哥大5 小时前
Linux实现网络计数器
运维·服务器·网络
Tender_光6 小时前
DNS域名解析服务
运维·服务器
白总Server6 小时前
Nginx 中间件
大数据·linux·运维·服务器·nginx·bash·web