title: Reverse Proxy using Go to Cloud Run Instance
body: <p>I feel like I'm close to having this working but so far I&quot;m running into an issue building a small reverse proxy in Go to a GCP Cloud Run instance. The request 'goes through' but the response from the request is the default GCP Cloud Run 404. It appears when making the request back to Cloud Run the Host header is being ignored and therefore the request is not being routed correction.</p>
  <p>What might I be missing here?</p>
  <pre><code>package main
  
  import (
      &quot;log&quot;
      &quot;net/http&quot;
      &quot;net/http/httputil&quot;
      &quot;net/url&quot;
  )
  
  const apiUrl = &quot;MY_CLOUD_RUN.a.run.app&quot;
  
  func main() {
      http.HandleFunc(&quot;/&quot;, proxy)
      log.Fatal(http.ListenAndServe(&quot;:8081&quot;, nil))
  }
  
  func proxy(res http.ResponseWriter, req *http.Request) {
      // gets past CORS checks
      if req.Method == http.MethodOptions {
          headers := res.Header()
          headers.Add(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;)
          headers.Add(&quot;Vary&quot;, &quot;Origin&quot;)
          headers.Add(&quot;Vary&quot;, &quot;Access-Control-Request-Method&quot;)
          headers.Add(&quot;Vary&quot;, &quot;Access-Control-Request-Headers&quot;)
          headers.Add(&quot;Access-Control-Allow-Headers&quot;, &quot;*&quot;)
          headers.Add(&quot;Access-Control-Allow-Methods&quot;, &quot;GET,HEAD,PUT,PATCH,POST,DELETE&quot;)
          res.WriteHeader(http.StatusOK)
          return
      }
  
      p := httputil.NewSingleHostReverseProxy(&amp;url.URL{
          Scheme: &quot;http&quot;,
          Host:   apiUrl,
      })
      p.Director = func(req *http.Request) {
          req.Header.Add(&quot;X-Forwarded-Host&quot;, req.Host)
          req.Header.Add(&quot;X-Origin-Host&quot;, apiUrl)
          req.Header.Add(&quot;Host&quot;, apiUrl)
          req.Header.Add(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;)
          req.URL.Scheme = &quot;https&quot;
          req.URL.Host = apiUrl
      }
      p.ModifyResponse = func(res *http.Response) error {
          res.Header.Set(&quot;Access-Control-Allow-Methods&quot;, &quot;GET,HEAD,PUT,PATCH,POST,DELETE&quot;)
          res.Header.Set(&quot;Access-Control-Allow-Credentials&quot;, &quot;true&quot;)
          res.Header.Set(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;)
          res.Header.Set(&quot;Access-Control-Allow-Headers&quot;, &quot;*&quot;)
          return nil
      }
  
      p.ServeHTTP(res, req)
  }
  </code></pre>