title: How to find request header in node.js net.createserver?
body: <p>I have a problem while using apache reverse proxies to handle virtualhosts for multiple applications on the same server in conjunction with node.js (I'm unable to resolve remote addresses <strong>and</strong> pass connections to other processes - doing either one seems to work well enough.)  This seems to come down to parsing the HTTP header <code>x-forwarded-for</code> created by the Apache reverse proxy engine.</p>
  
  <p>To start, my node.js application makes use of the following packages (so avoiding additional packages is a must, this combination alone is already a house of cards)</p>
  
  <ul>
  <li>socket.io</li>
  <li>socket.io-redis</li>
  <li>cluster</li>
  <li>redis</li>
  <li>express</li>
  </ul>
  
  <p>Now, for the actual issue - this code works flawlessly without a reverse proxy:</p>
  
  <pre><code>const server = net.createServer({pauseOnConnect: true}, function (connection) {
      console.log('Connection from ' + connection.remoteAddress.toString());
      var worker = workers[worker_index(connection.remoteAddress, worker_count)];
      worker.send('sticky-session:connection', connection);
  }).listen(port, host);
  </code></pre>
  
  <p>However the <code>net</code> module doesn't parse out HTTP headers, needed to determine the <code>remoteAddress</code> for sticky sessions from behind an apache reverse proxy.</p>
  
  <p>The ideal solution for this would function like:</p>
  
  <pre><code>const server = require('http').createServer(function (connection) {
      console.log('Connection from ' + (connection.headers['x-forwarded-for'] || '127.0.0.1'));
      var worker = workers[worker_index((connection.headers['x-forwarded-for'] || '127.0.0.1'), worker_count)];
      worker.send('sticky-session:connection', connection);
  }).listen(port, host);
  </code></pre>
  
  <p>However it seems connections from the <code>http</code> module cannot be passed across processes as those from the <code>net</code> module can be.  A solution which allows the passing of <code>http</code>-derived connections across processes in a <code>cluster</code> would likewise be appreciated.</p>
  
  <p>Just please don't suggest <code>sticky-session</code>, <code>sticky-session-reverse-proxy</code>, <code>socket-io-sticky-session</code> or anything derived from them, none work without breaking connections to either <code>socket.io</code>, <code>express</code> or in the cases of the more dated ones, <code>cluster</code>.</p>
  
  <p>By the way, <code>workers</code> is populated by:</p>
  
  <pre><code>var workers = [];
  for (var i = 0; i &lt; worker_count; i++) {
      workers.push(cluster.fork({ 'ID': i }));
  }
  </code></pre>