title: golang ReverseProxy not working
body: <p>I don't know why this reverse proxy is not working.  I've seen several examples and I can't find anything wrong with it.</p>
  
  <pre><code>package main
  
  import (
      "log"
      "net/url"
      "net/http"
      "net/http/httputil"
  )
  
  func report(r *http.Request){
    log.Print("URL: " + r.URL.Path)
    log.Print("Scheme: " + r.URL.Scheme)
    log.Print("Host: " + r.URL.Host)
    //r.URL.Scheme = "http"
    //r.URL.Host = "stackoverflow.com"
  
    //r.Header.Set("Host", "stackoverflow.com")
    //log.Print("Header Host: " + r.Header.Get("Host"))
  }
  
  func main() {
    proxy := httputil.NewSingleHostReverseProxy( &amp;url.URL{Scheme:"http",Host:"myrealserver.com"})
      proxy.Director = report
      // http.Handle("/", proxy)
      error := http.ListenAndServe("mylocalhost.com:8080", proxy)
      if(error != nil) {
          log.Fatal(error)
      }
  }
  </code></pre>
  
  <p>It logs: </p>
  
  <pre><code>2014/04/18 21:32:50 URL: /arg/es
  2014/04/18 21:32:50 Scheme:
  2014/04/18 21:32:50 Host:
  2014/04/18 21:32:50 http: proxy error: unsupported protocol scheme ""
  
  2014/04/18 21:32:51 URL: /favicon.ico
  2014/04/18 21:32:51 Scheme:
  2014/04/18 21:32:51 Host:
  2014/04/18 21:32:51 http: proxy error: unsupported protocol scheme ""
  </code></pre>
  
  <p>If I uncomment the line that redefines the Schema the error message becomes:</p>
  
  <pre><code>2014/04/18 21:38:05 http: proxy error: http: no Host in request URL
  </code></pre>
  
  <p>If I uncomment the line that redefines the host also, then the target server becomes stackoverflow.com (I mean, it never uses "myrealserver.com").</p>
  
  <p>If I ask for mylocalhost.com:8080/somepath (or even /) then I get a 404 from Stackoverflow, no matter if stackoverflow.com/somepath exists or not.  It says: </p>
  
  <pre><code>Couldn't find mylocalhost.com:8080
  The Q&amp;A site mylocalhost.com:8080 doesn't seem to exist... yet
  </code></pre>
  
  <p>It does not translate the Host header automatically.</p>
  
  <p>If then I uncomment the line that sets (and the other one that prints) the Header "Host".  Then I can read "stackoverflow.com" in the log, but I still get the same 404 page reporting that I am trying to access "mylocalhost.com".</p>
  
  <p>I'm using <code>go1.2.1 linux/amd64</code></p>
  
  <p>How is it that I am supposed to make the program work as a proxy?</p>