Nginx反向代理Oracle

241 阅读2分钟

把 Oracle 搬到指尖:Nginx 反向代理实战笔记

场景速写
医院内网与互联网物理隔离,我的笔记本只有一个网口:
• 插光纤 → 外网
• 连 Wi-Fi → 内网
于是日常变成:光纤插本机 → TeamViewer 连隔壁的跳板机 → 再连 Oracle。
有没有更优雅的姿势?
把跳板机上的 Oracle “映射” 出来,直接在本机连!


思路:Nginx 做 TCP 反向代理

Oracle 监听 1521 → 跳板机监听 1522 → 本机直连 跳板机 IP:1522
配置极简,但默认参数会“掉线”,需要微调。


最终配置(已美化)

worker_processes auto;

events {
    worker_connections 1024;
}

# ========== TCP 反向代理 ==========
stream {
    upstream oracle_backend {
        server 192.168.8.11:1521;
        keepalive 32;              # 连接池保活
    }

    server {
        listen 1522 reuseport;     # 多核并发
        proxy_pass oracle_backend;
        proxy_timeout 1h;          # 防止空闲断线
        proxy_connect_timeout 10s;
        proxy_socket_keepalive on; # TCP keepalive

        access_log /var/log/nginx/oracle_tcp.log proxy;
    }

    # 日志格式
    log_format proxy '$remote_addr:$remote_port -> $upstream_addr '
                     '[$time_local] $bytes_sent/$bytes_received '
                     '$session_time';
}

三步验证

  1. 语法检查

    nginx -t && systemctl reload nginx
    
  2. 端口测试

    telnet 跳板机IP 1522   # 出现 Connected 即 OK
    
  3. 长连接验证
    用 SQL Developer 连上后静置 30 min,未掉线即成功。


掉线根因 & 对策

表格

复制

现象根因对策
几分钟后断开Nginx 默认 proxy_timeout 10m改成 1h
防火墙踢掉空闲连接网络设备超时开启 TCP keepalive,或让网管放宽策略

效果对比

表格

复制

方式延迟掉线操作路径
TeamViewer偶发本机 → 跳板机 → Oracle
Nginx 代理本机 → Oracle

彩蛋:一条命令查看实时会话数

watch -n1 'ss -ant | grep :1522'

至此,再也不用“远程远程机”了。
祝各位工程师早日摆脱 TeamViewer 的束缚,愉快写 SQL!