code-server: [Bug]: 'Error: WebSocket close with status code 1006' behind nginx proxy

Is there an existing issue for this?

  • I have searched the existing issues

OS/Web Information

  • Web Browser: edge
  • Local OS: macos
  • Remote OS: ubuntu
  • Remote Architecture: x86
  • code-server --version: 4.6.0

Steps to Reproduce

proxy with nginx:

  location /code/ {
      proxy_pass https://0.0.0.0:9997/;
      proxy_http_version 1.1;
      proxy_set_header Host $host;
      proxy_set_header Upgrade $http_upgrade;
      proxy_read_timeout 86400;
      proxy_set_header Connection $connection_upgrade;
      proxy_set_header Accept-Encoding gzip;
  }

run code-server

code-server  --cert MyCertificate.crt --cert-key MyKey.key --port 9997
  1. visit from url: ${myhost}/code/

Expected

Code server opens up to home screen.

Actual

error log: z1

Logs

z2

Screenshot/Video

No response

Does this issue happen in VS Code or GitHub Codespaces?

  • I cannot reproduce this in VS Code.
  • I cannot reproduce this in GitHub Codespaces.

Are you accessing code-server over HTTPS?

  • I am using HTTP.

Notes

Why does code-server create a websocket connection with port 80? And how can this issue be solved?

The network environment of the host machine: The port 80 has been disabled by the administrator of the host machine, only a few ports are open.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 26 (13 by maintainers)

Most upvoted comments

In my installation with Nginx-Proxy the following works with 4.11.0

location / { proxy_pass http://127.0.0.1:3443/; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; proxy_set_header Accept-Encoding gzip; proxy_set_header Origin https://$host; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; }

docker run -it --name code-server -p 127.0.0.1:3443:8080 -v "$HOME/.config:/home/coder/.config" -v "$PWD:/home/coder/project" -u "$(id -u):$(id -g)" -e "DOCKER_USER=$USER" codercom/code-server:latest

I think you will have to set trusted_proxies in the LAN Caddy so it trusts the X-Forwarded-* headers from the WAN Caddy and passes them along rather than overriding them. https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#defaults

Thanks for your help! After a few more debugging and testing, it finally worked, that’s really saved my day! Perhaps I should note some additional info here if it can help someone stuck in a similar situation:

Keep the network topology AS SIMPLE AS YOU CAN to avoid hard-to-locate errors. Or, if you have to set a relatively complex cases that are not directly connected to code-server using one layer Caddy’s reverse_proxy, just like mine, then, switch on all debug logs available during the whole route to locate the main issue. For me, some lack of network knowledge I guess led me to not accurately understand the default behavior of how caddy deal with requests.

Besides the trusted_proxies asher mentioned above, I found:

after setting it, the Host in my LAN caddy’s log changed to the WAN Router’s WAN IP:port, which lead to a blank response even did not reach code-server at all.

To fix that, I edit the Caddyfile of my WAN caddy, replace the header_up Host {upstream_hostport} to header_up Host 192.168.LAN.CaddyHost:port.

After reloading, everything worked. Remember to switch off those debug to set a production state! Hope this helped XD.

@code-asher Thanks a lot.

I had this next line missing in my reverse proxy config

proxy_set_header Host $host;

and this next one had to be commented

# proxy_set_header X-Forwarded-Host $remote_addr;