如何通过Nginx代理RDP

问题描述 投票:0回答:3

我在 nginx 中使用以下配置来代理 RDP 连接:

  server { 
    listen          80;
    server_name     domain.com;

    location / {
      proxy_pass      http://192.168.0.100:3389;
    }
  }

但是连接没有通过。我的猜测是问题出在

http
中的
proxy_pass
。谷歌搜索“Nginx RDP”没有得到太多结果。

有人知道这是否可能吗?如果可以的话怎么办?

nginx remote-desktop rdp
3个回答
13
投票

实际上你是对的,

http
是问题所在,但不完全是你的代码块中的问题。让我们解释一下:

在您的

nginx.conf
文件中,您有类似的内容:

http {  
    ...
    ...
    ...

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

因此,您在conf文件中编写的所有内容都在这个

http
块/范围内。但 rdp 不是 http 是不同的协议。

我知道 nginx 处理这个问题的唯一解决方法是在

tcp
级别上工作。

因此,在

nginx.conf
内部和
http
块外部,您必须像这样声明
stream
块:

stream {
    # ...
    server {
        listen     80;
        proxy_pass 192.168.0.100:3389;
    }
}

通过上述配置,只需在 TCP 层上代理您的后端,当然会有一定的成本。您可能会注意到它缺少

server_name
属性,因此您无法在
stream
范围内使用它,而且您会失去
http
级别上的所有日志记录功能。

有关此主题的更多信息,请查看 docs


0
投票

对于任何希望使用 Nginx 负载平衡 RDP 连接的人,这就是我所做的:

像平常一样配置 nginx,将 HTTP(S) 流量重新路由到所需的服务器。

在该服务器上,安装 myrtille(它需要 IIS 和 .Net 4.5),您将能够从浏览器通过 RDP 连接到您的服务器!


0
投票

如果您正在寻找一个完整且不包含一堆点的配置这永远不会通过 nginx 配置测试,下面是一个完整且经过测试的配置。

此示例侦听端口

25301
并将连接转发到
rds.example.com:25301
。然后,在您的无线路由器中,您必须将端口
25301
上的传入连接转发到目标 Windows PC IP 地址和端口
3389

端口转发 — 路由器设置

服务名称 外部端口 内部端口 内部IP地址 协议
SERVER9_RDP 25301 3389 10.0.0.9 两者

NGINX — RDP 配置

stream {
    tcp_nodelay            on;
    resolver               1.1.1.1 1.0.0.1 valid=60s;
    resolver_timeout       5s;

    server {
        listen 25301;
        # proxy_pass 192.168.0.1:25301;
        proxy_pass rds.example.com:25301;

        proxy_connect_timeout 10s;
        proxy_timeout 2h;
        # Preserve client IP address
        proxy_bind $remote_addr transparent;

        allow 192.168.1.0/24;  # Example for allowing specific IPs
        deny all;

        # Logging
        access_log             off;
        error_log              /var/log/nginx/error.log warn;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.