防止nginx杀死空闲的TCP插座

发布于 2025-02-07 00:58:42 字数 1975 浏览 3 评论 0原文

我正在尝试将NGINX用作SSL/TCP插座的反向代理(以便我可以将服务器自定义作为RAW TCP编写,但具有NGINX处理SSL证书)。我的用例要求TCP连接还活着,但是要长时间闲置(由客户确定,但长达一个小时)。不幸的是,NGINX在不活动的前10分钟(时间内定时到一秒钟内)杀死了我的插座连接,而且我无法在线或在Docs中找到实际控制此超时的内容。

我知道它必须是nginx执行此操作(不是我的原始服务器的时间,或者客户端的SSL套接字),因为我可以直接连接到服务器的RAW TCP服务器而无需超时问题,但是如果我将NGINX作为RAW TCP反向运行代理(无SSL)它可以超时。

这里有一些重现该问题的代码,请注意,我已经在NGINX中评论了SSL相关的作品,因为超时会出现任何一种方式。

/etc/nginx/modules-enabled/test.conf:server.js

stream {
    upstream tcp-server {
        server localhost:33445;
    }
    server {
        listen 33446;
        # listen 33446 ssl;

        proxy_pass tcp-server;

        # Certs 
        # ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        # ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    }
}

;

const net = require("net");

const s = net.createServer();

s.on("connection", (sock) => {
    console.log('Got connection from', sock.remoteAddress, sock.remotePort );
    sock.on("error", (err) => {
        console.error(err)
        clearInterval(i);
    });
    sock.on("close", () => {
        console.log('lost connection from', sock.remoteAddress, sock.remotePort );
        clearInterval(i);
    });
});

s.listen(33445);

client.js

const net = require('net');

const host = 'example.com';

let use_tls = false;

let client;
let start = Date.now()

// Use me to circumvent nginx, and no timeout occurs
// let port = 33445;

// Use me to use nginx, and no timeouts occur after 10 mins of no RX/TX
let port = 33446;

client = new net.Socket();
client.connect({ port, host }, function() {
    console.log('Connected via TCP');
    // Include me, and nginx doesn't kill the socket
    // setInterval(() => { client.write("ping") }, 5000);
});

client.on('end', function() {
    console.log('Disconnected: ' + ((Date.now() - start)/1000/60) + " mins");
});

我尝试了Nginx流块中的各种指令,但似乎没有任何帮助。提前致谢!

I'm trying to use nginx as a reverse proxy for ssl/tcp sockets (so that I can write my server custom as raw tcp, but have nginx handle the ssl certificates). My use case requires the tcp connections remain alive, but to go idle (no packets back and forth) for extended periods of time (determined by the client, but as long as an hour). Unfortunately, nginx kills my socket connections after the first 10 minutes (timed to within a second) of inactivity, and I haven't been able to find either online or in the docs what actually controls this timeout.

I know that it has to be nginx doing it (not my raw server timing out, or my client's ssl socket), since I can directly connect to the server's raw tcp server without timeout issues, but if I run nginx as a raw tcp reverse proxy (no ssl) it does timeout.

Here's some code to reproduce the issue, note that I've commented out the ssl relevent pieces in nginx because the timeout occurs either way.

/etc/nginx/modules-enabled/test.conf:

stream {
    upstream tcp-server {
        server localhost:33445;
    }
    server {
        listen 33446;
        # listen 33446 ssl;

        proxy_pass tcp-server;

        # Certs 
        # ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        # ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    }
}

server.js;

const net = require("net");

const s = net.createServer();

s.on("connection", (sock) => {
    console.log('Got connection from', sock.remoteAddress, sock.remotePort );
    sock.on("error", (err) => {
        console.error(err)
        clearInterval(i);
    });
    sock.on("close", () => {
        console.log('lost connection from', sock.remoteAddress, sock.remotePort );
        clearInterval(i);
    });
});

s.listen(33445);

client.js

const net = require('net');

const host = 'example.com';

let use_tls = false;

let client;
let start = Date.now()

// Use me to circumvent nginx, and no timeout occurs
// let port = 33445;

// Use me to use nginx, and no timeouts occur after 10 mins of no RX/TX
let port = 33446;

client = new net.Socket();
client.connect({ port, host }, function() {
    console.log('Connected via TCP');
    // Include me, and nginx doesn't kill the socket
    // setInterval(() => { client.write("ping") }, 5000);
});

client.on('end', function() {
    console.log('Disconnected: ' + ((Date.now() - start)/1000/60) + " mins");
});

I've tried various directives in the nginx stream block, but nothing seems to help. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文