title: Rewrite spaces in url query on nginx (with express)
body: <p>I have an nginx server load-balancing several express (node.js) servers. Every time I get a request with unencoded white spaces on the query express closes the connection and nginx thinks it is down. Then it makes the same request to the next express upstream, gets the same result, and consequently sets the whole cluster down, when nothing is really down.</p>
  
  <p>Looking at the logs, those requests are coming from old browsers like Mozilla 4. I can also emulate the behaviour with curl:</p>
  
  <pre><code>curl "http://mysite.com/path/?q=foo bar"
  </code></pre>
  
  <p>I've tried adding a rewrite rule with all sorts of regular expressions to encode that white-space with %20:</p>
  
  <pre><code>rewrite     "^(.*)\ (.*)$"  "$1%20$2"    last;
  rewrite     "^(.*)\s(.*)$"  "$1 $2"    last;
  rewrite     "\s"  "%20"    last;
  ...
  </code></pre>
  
  <p>But nothing seems to work. Is it a problem on my regular expression or is just that nginx does not allow to just switch a character?
  Do you know any other way to solve the problem? Maybe on the express/node level, to avoid it to just close the connection?</p>