title: kubectl error while connecting to Rancher Kube API server behind nginx
body: <p>My setup has Rancher and Nginx.
  Nginx handles SSL Certificate and forwards requests to Rancher.</p>
  <p>Rancher recommends using this config in Nginx:</p>
  <p><a href="https://i.stack.imgur.com/5RNcb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5RNcb.png" alt="enter image description here" /></a></p>
  <p>The nginx config is done <a href="https://rancher.com/docs/rancher/v2.x/en/installation/resources/advanced/single-node-install-external-lb/" rel="nofollow noreferrer">as recommended by rancher</a>. It looks like this:</p>
  <pre><code>location / {
  
      proxy_pass http://localhost:8082/;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection &quot;upgrade&quot;;
      # This allows the ability for the execute shell window to remain open for up to 15 minutes. Without this parameter, the default is 1 minute and will automatically close.
      proxy_read_timeout 900s;
      proxy_buffering off;
  }
  </code></pre>
  <p>However I get this error when I use kubectl:</p>
  <pre><code>$ kubectl get all
  Error from server (InternalError): an error on the server (&quot;invalid upgrade response: status code 200&quot;) has prevented the request from succeeding
  </code></pre>
  <p>Since the error message points to some problem related to &quot;upgrade&quot;, I comment these two lines from the nginx config above and reload nginx:</p>
  <pre><code>#proxy_set_header Upgrade $http_upgrade;
  #proxy_set_header Connection &quot;upgrade&quot;;
  </code></pre>
  <p>After this, kubectl starts working properly but Rancher UI gives this error:</p>
  <p><a href="https://i.stack.imgur.com/iF7Uf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iF7Uf.png" alt="Error connecting to WebSocket
  Unable to establish a WebSocket connection to the server. If your server is behind a proxy or SSL-termination device, Browser can not connect to WebSocket. If you run the server behind a proxy, please make sure the proxy supports WebSockets. Streaming stats, logs, shell/console, and auto-updating of the state of resources may not work until this is resolved." /></a></p>
  <p>How do I make both - kubectl and Rancher UI - work properly?</p>