Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The workbench failed to connect to the server (Error: WebSocket close with status code 1006) #4443

Closed
isolume opened this issue Nov 4, 2021 · 43 comments

Comments

@isolume
Copy link

isolume commented Nov 4, 2021

OS/Web Information

  • Web Browser: Chromium (brave), although I also tested with Firefox and Edge and got the same result.
  • Local OS: Windows 11
  • Remote OS: Ubuntu
  • Remote Architecture: x86-64
  • code-server --version: 3.12.0

Steps to Reproduce

  1. Ran code-server (i also tried using --proxy-domain to see if I got any different results.) I used nginx to reverse proxy to my domain, I also use Cloudflare.
  2. When I open the domain on my browser, I get
The workbench failed to connect to the server (Error: WebSocket close with status code 1006)

Expected

The server would work normally.

Actual

See error above.

Logs

Logs look normal for the remote server, however browser gives error:

[vscode] failed to initialize VS Code vscode.browserified.js:726:13
    [4]< <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:726
    o <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:1
    r <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:1
    <anonymous> <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:1

(I replaced my domain with for privacy reasons.)

Error: [vscode] Could not set body background to theme background color. Could not find colorThemeData in localStorage.
    setBodyBackgroundToThemeBackgroundColor <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:668
    main <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:719
    [4]< <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:723
    o <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:1
    r <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:1
    <anonymous> <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:1
vscode.browserified.js:727:13
    [4]< <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:727
    o <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:1
    r <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:1
    <anonymous> <domain>/static/b37ff28a0a582aee84a8f961755d0cb40a4081db/usr/lib/code-server/out/browser/pages/vscode.browserified.js:1

Screenshot

image

Notes

This issue can be reproduced in VS Code: No

@woshilaiba
Copy link

woshilaiba commented Nov 4, 2021

i got this too. i ran it in docker.
i found there must be lines below in the nginx.conf to make it work
location / {
proxy_pass http://codeserver.xxx.xxx:8443/;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
this is from
https://github.com/cdr/code-server/blob/main/docs/guide.md#using-lets-encrypt-with-nginx

@ChrisKimZHT
Copy link

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;

@isolume
Copy link
Author

isolume commented Nov 4, 2021

proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; proxy_set_header Accept-Encoding gzip;

Thanks, that seemed to work.

@isolume isolume closed this as completed Nov 4, 2021
@bobbywaz
Copy link

Any idea which version broke this? I can't get it to work with Nginx-Proxy-Manager so I'll just downgrade until code-server gets it's sh*t together.

@jsjoeio
Copy link
Contributor

jsjoeio commented Nov 15, 2021

cc @code-asher

@code-asher
Copy link
Member

Those headers enable proxying websockets so it is not a code-server issue as far as I can tell.

@bsmithio
Copy link

If anyone is looking for a solution for ingress-nginx on Kubernetes like I was, add this annotation to your ingress

nginx.org/websocket-services: "code-server"

Replace code-server with whatever your code-server service name is.

@ZandercraftGames
Copy link

A little late now, but does anyone have a working configuration for Apache2?

@tinuh
Copy link

tinuh commented Feb 26, 2022

A little late now, but does anyone have a working configuration for Apache2?

Hey looking for the same thing, having issues proxying code server in apache2

@AuthorShin
Copy link

When adding the domain or subdomain for code-server on Nginx Proxy Manager make sure to check the Websockets support to solve the problem.
Screenshot-2022-03-25-175109

@leuit
Copy link

leuit commented Apr 21, 2022

@tinuh @ZandercraftGames
For those having issues with reverse proxy in Apache, refer to #4723 (comment)

I hope this works for you!

@1m188
Copy link

1m188 commented Apr 26, 2022

A little late now, but does anyone have a working configuration for caddy2?

@sowhile
Copy link

sowhile commented Jun 8, 2022

proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; proxy_set_header Accept-Encoding gzip;

Thank you very much. It's very useful to me.

@getkub
Copy link

getkub commented Jun 12, 2022

for kubernetes, I had to add BOTH suggestions from Link1 and annotations as follows.

PS: the {{ .Values.user }} etc, are dynamic values from values.yml of helm chart which you need to replace

    nginx.org/websocket-services: code-server-{{ .Values.user }}
    nginx.org/server-snippets: |
      location / {
      proxy_pass http://{{ .Values.host_domain }}:{{ .Values.port }}/;
      proxy_set_header Host $host;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection upgrade;
      proxy_set_header Accept-Encoding gzip;
      }  

Entire example below using helm chart

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: code-server-{{ .Values.user }}
  namespace:  {{ .Values.namespace }}
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.org/websocket-services: code-server-{{ .Values.user }}
    nginx.org/server-snippets: |
      location / {
      proxy_pass http://{{ .Values.host_domain }}:{{ .Values.port }}/;
      proxy_set_header Host $host;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection upgrade;
      proxy_set_header Accept-Encoding gzip;
      }   
spec:
  tls:
  - hosts:
    - {{ .Values.host_domain }}
    secretName: code-server-secret-{{ .Values.user }}
  rules:
  - host: {{ .Values.host_domain }}
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: code-server-{{ .Values.user }}
            port:
              number: {{ .Values.port }}

@jeremy-code
Copy link

Hello, leaving this comment here just in case anyone in the future is having this issue.

Cloudflare tunnels, got this same error. Solution was going to dash.cloudflare.com and setting websockets to enabled

image

@type0dev
Copy link

type0dev commented Nov 6, 2022

When adding the domain or subdomain for code-server on Nginx Proxy Manager make sure to check the Websockets support to solve the problem. Screenshot-2022-03-25-175109

This worked Thank you. I was having trouble with error on OpenBooks and this fixed that to.

@smseidl
Copy link

smseidl commented Dec 9, 2022

A little late now, but does anyone have a working configuration for caddy2?

Did you ever get a resolution to this?

@janisii
Copy link

janisii commented Dec 28, 2022

If you are facing wss:// websocket 1006 error on kubernetes with NGINX Ingress Controller, you can try to add timeout annotations to service ingress.

nginx.org/proxy-read-timeout: "3600"
nginx.org/proxy-send-timeout: "3600"

@keesfluitman
Copy link

I don't want to wake up an old issue. But I've had it working, and suddenly this error came.
I'm using swag and my reverse proxy conf was working fine. The suggested addition of headers didn't work. Any tips?

@code-asher
Copy link
Member

code-asher commented Mar 27, 2023

Just to confirm, is Host one of the headers you added? v4.10.0 added a security check that uses the host and origin headers.

@mturilli
Copy link

@code-asher thanks. Adding Host solved the issues with v4.12.0.

@0x676e67
Copy link

0x676e67 commented May 8, 2023

code-server version: 4.12.0
NginxProxyManager custom config:

location /
{
    proxy_pass http://10.0.0.3:80;
    proxy_set_header Host code.xxx.com:7443;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header   Upgrade          $http_upgrade;  
    proxy_set_header   Connection       "Upgrade";
    proxy_connect_timeout      60;   
    proxy_send_timeout         90;   
    proxy_read_timeout         90; 
    proxy_buffer_size          4k; 
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;  
    proxy_temp_file_write_size 64k;
    add_header X-Cache $upstream_cache_status;
}

@code-asher
Copy link
Member

code-asher commented May 8, 2023

proxy_set_header Host $http_host; should also work if you want to avoid hard-coding the host.

Edit: Or proxy_set_header X-Forwarded-Host $http_host;.

@keesfluitman
Copy link

keesfluitman commented May 9, 2023

I just added these headers:

        include /config/nginx/proxy.conf;
        include /config/nginx/resolver.conf;
        set $upstream_app binhex-code-server;
        set $upstream_port 8500;
        set $upstream_proto http;
        proxy_pass $upstream_proto://$upstream_app:$upstream_port;

        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_set_header Accept-Encoding gzip;
        

Should I try to delete the binhex image and use yours? Cause it's not working for me.

@code-asher
Copy link
Member

I am not familiar with the binhex image but if you add --log debug to the code-server invocation it should tell you what it is getting for the headers which might provide a clue on what is going wrong.

@aayusharyan
Copy link

Please. Please don't forget to restart ngnix after changing the configuration.
sudo systemctl restart nginx

@xTCry
Copy link

xTCry commented Jul 5, 2023

Fix for Nginx-Proxy-Manager
VS Code since version 4.10.1

"Advanced" → Custom Nginx Configuration:

location / {
    # proxy_ssl_verify off;
    proxy_set_header Host $host;
    proxy_set_header Accept-Encoding gzip;
    proxy_set_header X-Forwarded-Host $host;
    #proxy_set_header X-Forwarded-Host $host:$server_port;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
    proxy_http_version 1.1;

    # Proxy!
    include conf.d/include/proxy.conf;
}

@nabilfikri
Copy link

this works for me. Thanks!

Fix for Nginx-Proxy-Manager VS Code since version 4.10.1

"Advanced" → Custom Nginx Configuration:

location / {
    # proxy_ssl_verify off;
    proxy_set_header Host $host;
    proxy_set_header Accept-Encoding gzip;
    proxy_set_header X-Forwarded-Host $host;
    #proxy_set_header X-Forwarded-Host $host:$server_port;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
    proxy_http_version 1.1;

    # Proxy!
    include conf.d/include/proxy.conf;
}

this one works for me. Thanks!

@maxoyed
Copy link

maxoyed commented Sep 18, 2023

proxy_set_header Host $http_host; should also work if you want to avoid hard-coding the host.

works, Thanks!

liuxhit added a commit to liuxhit/code-server that referenced this issue Oct 3, 2023
update nginx config to avoid wss error when expose code-server using a custom domain and a custom port via nginx.

see also: 
[issue of code-server](coder#4443)
[different between `$host` and `$http_host`](https://stackoverflow.com/a/76875724)
@liuxhit
Copy link
Contributor

liuxhit commented Oct 3, 2023

i got this too. i ran it in docker. i found there must be lines below in the nginx.conf to make it work location / { proxy_pass http://codeserver.xxx.xxx:8443/; proxy_set_header Host $host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; proxy_set_header Accept-Encoding gzip; } this is from https://github.com/cdr/code-server/blob/main/docs/guide.md#using-lets-encrypt-with-nginx

I re-produced too, caused by the slight differences in nginx config template from https://github.com/cdr/code-server/blob/main/docs/guide.md#using-lets-encrypt-with-nginx , and issued a pull-request: #6471

code-asher pushed a commit that referenced this issue Oct 4, 2023
update nginx config to avoid wss error when expose code-server using a custom domain and a custom port via nginx.

see also: 
[issue of code-server](#4443)
[different between `$host` and `$http_host`](https://stackoverflow.com/a/76875724)
@zeno-io
Copy link

zeno-io commented Oct 25, 2023

for me, below is work: (docker run & nginx proxy outside the docker container)

location / {
         add_header Access-Control-Allow-Origin *;
         add_header Access-Control-Allow-Methods *;
         add_header Access-Control-Allow-Headers *;
 
         # proxy_ssl_verify off;
         proxy_set_header Host $host;
         proxy_set_header Accept-Encoding gzip;
         #proxy_set_header X-Forwarded-Host $host;
         proxy_set_header X-Forwarded-Host $host:$server_port;
 
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $http_connection;
         proxy_http_version 1.1;
 
         proxy_pass http://127.0.0.1:8081/;
 
       }

@KJZH001
Copy link

KJZH001 commented Nov 29, 2023

Hello, leaving this comment here just in case anyone in the future is having this issue.

Cloudflare tunnels, got this same error. Solution was going to dash.cloudflare.com and setting websockets to enabled

image

Thank you. Your reply solved the problem I encountered.

@EITSxiaozhai
Copy link

对我来说,下面是工作:(docker run & nginx proxy externals of docker 容器)

location / {
         add_header Access-Control-Allow-Origin *;
         add_header Access-Control-Allow-Methods *;
         add_header Access-Control-Allow-Headers *;
 
         # proxy_ssl_verify off;
         proxy_set_header Host $host;
         proxy_set_header Accept-Encoding gzip;
         #proxy_set_header X-Forwarded-Host $host;
         proxy_set_header X-Forwarded-Host $host:$server_port;
 
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $http_connection;
         proxy_http_version 1.1;
 
         proxy_pass http://127.0.0.1:8081/;
 
       }

This config file worked for me. Solved the websock error. Thanks a lot!
code-server ---> nginx

@octopus2181
Copy link

Hi, I found an issue where if I set the port to 443:443 in the docker compose of Nginx-Proxy-Manager ,reverse proxies are good. I was able to use code-server in the docker . But If I set a different port mapping 443 in the docker compose of Nginx-Proxy-Manager (such as 9091:443), I can't use linuxserver/code-server . There is an error, "The workbench failed to connect to the server (Error: WebSocket close with status code 1006)" . Because my port 443 is forbidden, I want to use a different port.

@code-asher
Copy link
Member

@octopus2181 Could you open a new issue with a Docker compose I can use to reproduce the issue? I will look into it.

@keskinonur
Copy link

For you referece, with code-server version 4.20.1, my working nginx configuration is:

  location / {
    proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection upgrade;
    proxy_set_header Accept-Encoding gzip;
  }

@octopus2181
Copy link

For you referece, with code-server version 4.20.1, my working nginx configuration is:

  location / {
    proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection upgrade;
    proxy_set_header Accept-Encoding gzip;
  }

Yes, you are right ! Setting proxy_set_header X-Forwarded-Host $http_host , the port 9091 can connect to the code-server .
Thank you !

@fractalcounty
Copy link

fractalcounty commented Feb 13, 2024

Since nearly every solution on this issue refers to non-standard networking configurations without elaborating on what they're supposed to indicate, here's the configuration that worked for my specific local environment (NGINX Proxy Manager + lscr.io's code-server image in separate docker stacks sharing the same 'proxy' bridge network):

  1. Create a new proxy host via NGINX Proxy Manager
  2. Use the 'http' scheme, followed by your domain (i.e, code.mydomain.com), and the code-server port (by default this is 8443 on the lscr.io image)
  3. Enable 'Websockets Support'
  4. In the 'SSL' tab, enable 'Force SSL' and 'HTTP/2 Support'
  5. Navigate to the 'Advanced' tab and paste the following configuration into the input field:
  location / {
    proxy_pass http://192.168.32.52:80/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header X-Forwarded-Host $http_host;
    proxy_set_header Accept-Encoding gzip;
    proxy_set_header Connection upgrade;
  }
  1. Replace 192.168.4.58 and 80 with the IP and port that NGINX Proxy Manager is accessible through on your server.

This configuration uses the default ports that both code-server and NGINX Proxy Manager use out-of-the-box, at least while using lscr.io's code-server image- but it should still work either way.

If you run into any issues after this point, ensure you haven't incorrectly modified any of code-server's proxy-related environment variables (i.e, CS_DISABLE_PROXY or PROXY_DOMAIN), and ensure that you correctly configured your domain's DNS settings through your DNS provider if applicable (i.e, an A record pointing *.yourdomain.com to your server's IP or tunnel configuration).

Here's what my final docker-compose.yml file looked like (environment variables redacted):

version: "3.8"
  code-server:
    image: lscr.io/linuxserver/code-server:latest
    container_name: code-server
    restart: unless-stopped
    environment:
      - "PASSWORD=this_isnt_very_secure"
      - "SUDO_PASSWORD=this_is_extremely_insecure"
      - "DEFAULT_WORKSPACE=/srv" # Makes code-server start in /srv/ by default
    volumes:
      - '/srv/docker/apps/code-server:/config'
      - '/srv:/workspace' 
    ports:
      - '8443:8443'
    secrets:
      - CODE_SERVER_PWD
      - CODE_SERVER_SUDO_PWD
      
networks:
  default:
    name: proxy
    external: true # Assuming 'proxy' network has already been created and is also used by NPM

Full disclaimer that I have no idea if I'm following the best practices here, I'm just posting what worked for my very specific configuration. Feel free to correct me if there's a better way to go about any of this.

@wangyizhi1
Copy link

proxy_set_header Host $http_host; should also work if you want to avoid hard-coding the host.

Edit: Or proxy_set_header X-Forwarded-Host $http_host;.

@code-asher 's answer works for me, and a faster way:

./bin/code-server --host 0.0.0.0 --trusted-origins=* ...

@freeluo22
Copy link

for me, below is work: (docker run & nginx proxy outside the docker container)

location / {
         add_header Access-Control-Allow-Origin *;
         add_header Access-Control-Allow-Methods *;
         add_header Access-Control-Allow-Headers *;
 
         # proxy_ssl_verify off;
         proxy_set_header Host $host;
         proxy_set_header Accept-Encoding gzip;
         #proxy_set_header X-Forwarded-Host $host;
         proxy_set_header X-Forwarded-Host $host:$server_port;
 
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $http_connection;
         proxy_http_version 1.1;
 
         proxy_pass http://127.0.0.1:8081/;
 
       }

This configuration works for me.

@Venjy
Copy link

Venjy commented May 24, 2024

for me, below is work: (docker run & nginx proxy outside the docker container)

location / {
         add_header Access-Control-Allow-Origin *;
         add_header Access-Control-Allow-Methods *;
         add_header Access-Control-Allow-Headers *;
 
         # proxy_ssl_verify off;
         proxy_set_header Host $host;
         proxy_set_header Accept-Encoding gzip;
         #proxy_set_header X-Forwarded-Host $host;
         proxy_set_header X-Forwarded-Host $host:$server_port;
 
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $http_connection;
         proxy_http_version 1.1;
 
         proxy_pass http://127.0.0.1:8081/;
 
       }

This configuration works for me.

This works for me. I've tried lots of configuration, the key line is proxy_set_header X-Forwarded-Host $host:$server_port;

@yifanlu0227
Copy link

--host 0.0.0.0 --trusted-origins=*

@wangyizhi1 Awesome, this works!

yiliang114 pushed a commit to yiliang114/code-server that referenced this issue Jan 23, 2025
update nginx config to avoid wss error when expose code-server using a custom domain and a custom port via nginx.

see also: 
[issue of code-server](coder#4443)
[different between `$host` and `$http_host`](https://stackoverflow.com/a/76875724)
@kimboslice99
Copy link

For those that may be needing to proxy through IIS, here is a couple rewrite rules for that. This app uses permessage-deflate which ARR does not support, so the request fails with this 1006 error message

<rule name="removepermessagedeflate">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_SEC_WEBSOCKET_EXTENSIONS}" pattern="permessage-deflate(.*)" />
    </conditions>
    <serverVariables>
        <set name="HTTP_SEC_WEBSOCKET_EXTENSIONS" value="{C:1}" />
    </serverVariables>
    <action type="None" />
</rule>
<rule name="proxy">
    <match url=".*" />
    <action type="Rewrite" url="http://10.8.0.4:8080/{R:0}" />
</rule>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests