使用哪种方法设置动态端口映射器

时间:2009-10-02 14:23:24

标签: apache networking routing tcp mapping

我有一个依赖于大量后端服务的Web应用程序,我希望能够在这些服务的不同实例之间动态切换。因此,我们的想法是创建一个中间应用程序,它可以监听某些端口并根据需要重定向流量。

如果我正在处理纯HTTP,我会考虑使用Apache和proxy_pass这样做,这将是理想的(快速,可重新配置,没有停机时间),但它不仅仅是HTTP流量,这就是我被卡住的原因< / p>

任何正确方向的推动都会受到赞赏。

谢谢,

罗布

2 个答案:

答案 0 :(得分:1)

为什么不使用软件(例如Vyatta,pfSense等)或硬件路由器并在某些地方放置一些NAT规则?

我有时会使用一个非常简洁的开源应用程序,当我很快需要转发某些端口等进行一些测试时,是TcpTunnel:http://www.vakuumverpackt.de/tcptunnel/

答案 1 :(得分:1)

你可以配置Apache来动态监听一堆端口(似乎有一个上限,在我的系统上大约100)然后使用mod_rewrite动态地将这些端口映射到你的内容。您也可以使用在Perl中编写的动态主机,但是您还需要包含对每个实例都相同的所有其他配置。

# Use Perl to write out many Listen directives
LoadModule perl_module  libexec/apache2/mod_perl.so

<Perl>

  # The Dynamic Ports are those from 49152 through 65535
  # On the machines I tested 100 seems to be the upper limit
  # Apache 2 seems to have issues starting (memory?) when the number
  # is too high. Once the server has started, there does not seem
  # to be a performance hit for having a large number of ports open

  # Starting at 50000 for cleanliness
  my $lower_port = 50000;
  my $max_ports_to_use = 100;


  my $upper_port = $lower_port + $max_ports_to_use;

  foreach my $port ($lower_port .. $upper_port) {

    # Listen on a specific port number
    push @Listen, $port;

  };

</Perl>


# if we are in the range of dynamic ports
RewriteEngine on
RewriteCond %{SERVER_PORT} >49152
RewriteCond %{SERVER_PORT} <65535
  # serve up content on that port number
  RewriteRule (.*) /dynamic_sites/%{SERVER_PORT}/$1