title: WebSocket handshake Error with caddy proxy in Golang
body: <p>I’m trying to initiate a websocket connection between chrome browser client and server.</p>
  
  <p>Overview of my implementation :
  There are set of different up and running projects. The load balancer to all other projects and it handle all http requests, routes and proxy to other sub projects. These all projects use load balancers. My attempt is to create a websocket connection from chrom browser to one sub project.</p>
  
  <blockquote>
    <p>caddy version : 0.9.3</p>
    
    <p>websocket go library : github.com/gorilla/websocket 1</p>
  </blockquote>
  
  <pre><code>// Starting caddy server to server static files
  args := []string{"bin/caddy", "--conf=" + commons.ServerConfigurations.CaddyFile, "-pidfile=bin/caddy.pid"}
  
  if err := exec.Command("nohup", args...).Start(); err != nil {
      log.Fatalln("Error occourred while starting caddy server : ", err.Error())
      os.Exit(1)
  }
  
  //Starting the API server
  router := routes.NewRouter()
  http.Handle("/", router)
  
  httpsServer := &amp;http.Server{
      Addr: ":" + strconv.Itoa(commons.ServerConfigurations.HttpsPort+
          commons.ServerConfigurations.PortOffset),
      Handler:        logHandler,
      ReadTimeout:    time.Duration(commons.ServerConfigurations.ReadTimeOut) * time.Second,
      WriteTimeout:   time.Duration(commons.ServerConfigurations.WriteTimeOut) * time.Second,
      MaxHeaderBytes: 1 &lt;&lt; 20,
  }
  
  httpsServer.ListenAndServeTLS(commons.ServerConfigurations.SSLCertificateFile,
          commons.ServerConfigurations.SSLKeyFile)
  </code></pre>
  
  <p>First when client reach to the domain, $DOMAIN_NAME such as www.sample.com. Caddy proxy the client request to the <a href="https://localhost:8107/analytics" rel="nofollow noreferrer">https://localhost:8107/analytics</a> </p>
  
  <pre><code>https://{$DOMAIN_NAME}/analytics/ {
      tls pem_file key_file
      proxy /  https://localhost:8105/analytics { 
         websocket
         insecure_skip_verify
      }
  }
  </code></pre>
  
  <p>There are multiple standalone servers. Following is the caddy configuration of server.It serve static files at the directory in <em>webapps/analytics</em></p>
  
  <pre><code>localhost:8105/analytics {
      root  webapps/analytics
      gzip
      ext    .html
      tls pem_file key_file
      proxy  /api https://localhost:8108 {  HTTPS port:8108
           websocket
           insecure_skip_verify
      }
  }
  </code></pre>
  
  <p>Inside the analytics sub project, " /api/ws " would trigger CreateSocketConnection() method.</p>
  
  <pre><code>//Starting the API server
      router := routes.NewRouter()
      http.Handle("/", router)
      http.HandleFunc("/api/ws", api.CreateSocketConnection)
  </code></pre>
  
  <p>CreateSocketConnection implementation :</p>
  
  <pre><code>func CreateSocketConnection(w http.ResponseWriter, r *http.Request) {
      var upgrader = websocket.Upgrader{
          ReadBufferSize:  1024,
          WriteBufferSize: 1024,
      }
      _, err = upgrader.Upgrade(w, r, nil)
      if err != nil {
          log.Fatal("upgrader failed :", err.Error())
      }
      //controllers.HandleSocket(ws)
  }
  </code></pre>
  
  <p>Client side implementation :</p>
  
  <pre><code>conn = new WebSocket("wss://xxxx.com/analytics/api/ws");
  </code></pre>
  
  <p>Websocket upgrade error happen in golang backend server</p>
  
  <blockquote>
    <p>2018/06/21 16:53:41 http: TLS handshake error from 127.0.0.1:47216:
    remote error: tls: bad certificate</p>
  </blockquote>
  
  <p>How I use this secure https and websocket in same web server.Is it posible ?</p>
  
  <blockquote>
    <p>web request : 'wss://xxxx.com/analytics/api/ws' failed: Error during
    WebSocket handshake: Unexpected response code: 502</p>
  </blockquote>
  
  <p>Am I missing something in my implementation?</p>
  
  <p>Thanks in advance</p>