Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

What is the simplest way to configure apache 2.2 to listen on, say, 20 different ports and then proxy those requests to the same ports on another server? For example, a request to http://myserver.com:1000/ should be proxy-forwarded to http://otherserver.com:1000/, a request to http://myserver.com:1001/ should be proxy-forwarded to http://otherserver.com:1001/, and so on, for ports 1000 through 1019.

Getting my server to listen on 20 different ports is easily done with 20 Listen directives. The trouble is the proxy directives. The mod_proxy module's ProxyPass directive doesn't seem to have a place for any reference to the port number. I can make 20 different VirtualHost sections and put proxy directives in each one:

<VirtualHost *:1000>
  ProxyPass        / http://otherserver.com:1000/
  ProxyPassReverse / http://otherserver.com:1000/

<VirtualHost *:1001>
  ProxyPass        / http://otherserver.com:1001/
  ProxyPassReverse / http://otherserver.com:1001/

<VirtualHost *:1002>
  ProxyPass        / http://otherserver.com:1002/
  ProxyPassReverse / http://otherserver.com:1002/


but that seems extremely tedious. Though I can generate all those blocks programmatically from a template, I have to believe there's a better way, maybe with a single mod_rewrite rule or something? Suggestions?

share|improve this question
The <VirtualHost>s are tedious, but the Listens weren't? I don't think there's gonna be any 'smooth' way to do this with Apache, unfortunately.. it's not really built for bulk port listeners. –  Shane Madden Apr 14 '11 at 21:05
The Listens were one line each, vs. at least 4 lines for the VirtualHost sections. I was looking for a one-liner like RewriteRule (.*) http://otherserver.com:%{REQUEST_PORT}$1 or similar (but one that actually works :) If it's really not possible to do this in significantly fewer lines, then type that up as an answer and I'll accept it after I've given up hope of better answers :) –  John Siracusa Apr 15 '11 at 0:10

2 Answers 2

You'd be better served by using "rinetd", which is a TCP connection forwarder.

This is introduced & documented here:

share|improve this answer

Make a script to write your config.

share|improve this answer
Like I said, I can generate the config programmatically (and I am actually doing so with a Puppet template). I'm just wondering if there's a simpler solution. –  John Siracusa Apr 15 '11 at 0:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.