code-server icon indicating copy to clipboard operation
code-server copied to clipboard

Web socket fails to connect through Apache

Open lplassman opened this issue 4 years ago • 52 comments

OS/Web Information

  • Web Browser: Chrome
  • Local OS: MacOS 12.2
  • Remote OS: Ubuntu 20.04 LTS
  • Remote Architecture: amd64
  • code-server --version: 4.0.1

Logs

code-server --log debug [2022-01-11T03:38:43.612Z] info code-server 4.0.1 735c6da829535969ff7193c79379299e4a1cb9bc [2022-01-11T03:38:43.613Z] info Using user-data-dir ~/.local/share/code-server [2022-01-11T03:38:43.628Z] info Using config file ~/.config/code-server/config.yaml [2022-01-11T03:38:43.628Z] info HTTP server listening on http://0.0.0.0:8080/ [2022-01-11T03:38:43.628Z] info - Authentication is enabled [2022-01-11T03:38:43.628Z] info - Using password from ~/.config/code-server/config.yaml [2022-01-11T03:38:43.628Z] info - Not serving HTTPS [22:38:51] Extension host agent started. [22:38:51] [Theme Service] Scanning for theme extension... [22:38:51] [Theme Service] theme-abyss [22:38:51] [Theme Service] theme-defaults [22:38:51] [Theme Service] theme-kimbie-dark [22:38:51] [Theme Service] theme-monokai [22:38:51] [Theme Service] theme-monokai-dimmed [22:38:51] [Theme Service] theme-quietlight [22:38:51] [Theme Service] theme-red [22:38:51] [Theme Service] theme-solarized-dark [22:38:51] [Theme Service] theme-solarized-light [22:38:51] [Theme Service] theme-tomorrow-night-blue [22:38:51] [Theme Service] Attempting to find user's active theme: Default Dark+ [2022-01-11T03:38:54.949Z] debug got latest version {"latest":"4.0.1"} [2022-01-11T03:38:54.949Z] debug comparing versions {"current":"4.0.1","latest":"4.0.1"} [2022-01-11T03:39:51.007Z] debug 0 active connections

Screenshot

139257954-98741db0-07d2-47f0-a529-fbe57cdd1188 Screen Shot 2022-01-10 at 10 47 35 PM Screen Shot 2022-01-10 at 10 48 36 PM

Notes

I currently only experience this error on code-server 4.0.1. code-server 3.12.0 works without issue. I am currently using apache2 with a reverse proxy. The current config for the apache2 virtual host is below:

<Location "/"> ProxyPass http://127.0.0.1:8080/ ProxyPassReverse http://127.0.0.1:8080/ ProxyPassReverse / RewriteEngine on RewriteCond %{HTTP:Upgrade} websocket [NC] RewriteCond %{HTTP:Connection} upgrade [NC] RewriteRule ^/?(.*) "ws://127.0.0.1:8080/" [P,L]

I also made sure all the websockets modules are loaded in apache2 and working without problems. No errors occur on the Apache web server. Any suggestions on how to get this working with Apache2?

This issue can be reproduced in VS Code: No

lplassman avatar Jan 11 '22 04:01 lplassman

@ihowell

Are you receiving the same error as OP? Is it not upgrading the websocket connection properly?

You say you're running code-server on a separate machine from apache; can you directly access code-server via the port it's listening on? Depending on the error you're receiving, I think it may just be a communication issue.

If you are indeed receiving the error on the websocket upgrade and can access the frontend webpage of code-server; you'll simply just need to upgrade apache2 as the version does not handle websocket upgrades according to RFC 7230

leuit avatar May 05 '22 19:05 leuit

I am receiving the same 1006 error as op, however I don't know how to tell if the websocket is being upgraded properly. I can access code-server from http://192.168.50.60:8081 in my browser and everything works fine. I just followed instructions and upgraded my server to Apache2 version 2.4.53, but it still isn't working.

Edit: When I run code-server with --log debug, I don't see any errors in the logs.

Edit 2: I now have it working by using the rewriting rules above instead of using my configuration for proxy passing and reverse proxy passing.

ihowell avatar May 05 '22 19:05 ihowell

@ihowell Wonderful! I was going to suggest changing the apache config next; glad you got it resolved!

leuit avatar May 06 '22 00:05 leuit

Here's my working config using apache2 v2.4.52 & code-server v4.3.0, using ProxyPass (and RewriteEngine only for WebSocket).

<VirtualHost *:80>
    ServerName code.kaki87.net
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:443>
    ServerName code.kaki87.net
    ProxyRequests Off
    ProxyPass / http://localhost:3090/
    ProxyPassReverse / http://localhost:3090/
    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /(.*) ws://localhost:3090/$1 [P,L]
    SSLCertificateFile /home/kaki/.acme.sh/kaki87.net/fullchain.cer
    SSLCertificateKeyFile /home/kaki/.acme.sh/kaki87.net/kaki87.net.key
</VirtualHost>

I always try to make those as light as possible. I don't think proxying to the local host or to another host on the local network changes anything. Also, @ihowell, when making future reports about something that isn't working, please document the unexpected behavior as much as you can, because it is as important as documenting your configuration. :)

KaKi87 avatar May 06 '22 07:05 KaKi87

Upgrade Apache2 to 2.4.52 also solved the issue for me without change anythingelse in apache configuration.

Drealine avatar May 21 '22 21:05 Drealine

Hi @ihowell can you please share your solution? I have your same issue running last Apache version (2.4.6)

maverjk avatar May 30 '22 12:05 maverjk

Upgrading apache2 worked for me.

lachlansimpson avatar Sep 16 '22 05:09 lachlansimpson

This issue has not been resolved in version 4.

qjaden avatar Sep 03 '23 15:09 qjaden

try nginx config as follows:

location ~ ${your frontend url pattern} {
    # add it if your url is dynamic
    resolver {your DNS server}
    
    # trick here, consult here for Nginx official doc
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection upgrade;
    # trick here, if absent, the server will block the websocket request if the origin is NOT equal to the host
    proxy_set_header Origin http://${your backend host}/;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    
    proxy_set_header Accept-Encoding gzip;
    proxy_cookie_domain ${your backend host} ${your frontend host};
    
    proxy_pass ${your backend url pattern};
}

config above working for me. my scene:

  • code-server: 4.18.0, running in an container of Kubernetes
  • nginx:1.9.7, running in an container of another Kubernetes.

mashroomxl avatar Nov 12 '23 09:11 mashroomxl

As of Apache 2.4.58 I'm still having this issue. 😒

lilithe@lilithebowman:~$ apache2 -v
Server version: Apache/2.4.58 (Ubuntu)
Server built:   2025-04-03T14:36:49
Status
404
Not Found
VersionHTTP/1.1
Transferred215 B (0 B size)
DNS ResolutionSystem

	
Connection
	Upgrade
Content-Encoding
	br
Content-Type
	text/plain
Date
	Thu, 24 Apr 2025 16:22:10 GMT
Server
	Apache/2.4.58 (Ubuntu)
Transfer-Encoding
	chunked
Vary
	Accept-Encoding
	
Accept
	*/*
Accept-Encoding
	gzip, deflate, br, zstd
Accept-Language
	en-US,en;q=0.5
Cache-Control
	no-cache
Connection
	keep-alive, Upgrade
Cookie
	code-server-session=%24argon2id%24v%3D19%24m%3D65536%2Ct%3D3%2Cp%3D4%24mBGJVPPISLtELQT8KxmJcQ%24aZdISVoXpjpHH89DwmOOTlcTQ%2Bq257Nnfb2iiXvlPP0
Host
	<my site HOST>
Origin
	<my site URL>
Pragma
	no-cache
Sec-Fetch-Dest
	empty
Sec-Fetch-Mode
	websocket
Sec-Fetch-Site
	same-origin
Sec-WebSocket-Extensions
	permessage-deflate
Sec-WebSocket-Key
	CPDPULLXB/3igjyaVRePmw==
Sec-WebSocket-Version
	13
Upgrade
	websocket
User-Agent
	Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0

lilithebowman avatar Apr 24 '25 16:04 lilithebowman

As of Apache 2.4.58 I'm still having this issue. 😒

Share your config.

KaKi87 avatar Apr 24 '25 16:04 KaKi87

As of Apache 2.4.58 I'm still having this issue. 😒

Share your config.

Sure thing! Sorry I was just cleaning up my /etc/apache2/sites-available/ because it's been there for years with a few unnecessary site configs and things LetsEncrypt made that don't need to be there. Now that it's cleanly whittled down to the configs I need for my sites, I can begin to debug this.

Okay false alarm. Here is my fixed config!

cat /etc/apache2/sites-available/code-server.conf

<VirtualHost *:443>
        ServerName code.mysite.com

        <Proxy *>
                Allow from all
                Allow from localhost
        </Proxy>

        Header add Connection "Upgrade"
        RequestHeader set Connection "Upgrade"


        RewriteEngine On RewriteCond %{HTTP:Upgrade} =websocket [NC]
        RewriteRule /(.*) ws://127.0.0.1:1024/$1 [P,L]
        RewriteCond %{HTTP:Upgrade} !=websocket [NC]
        RewriteRule /(.*) http://127.0.0.1:1024/$1 [P,L]
        ProxyRequests off ProxyPass / http://127.0.0.1:1024/ nocanon retry=10 
        ProxyPassReverse / http://127.0.0.1:1024/ retry=10

        ProxyPass /api/websocket ws://127.0.0.1:8123/api/websocket
        ProxyPassReverse /api/websocket ws://127.0.0.1:8123/api/websocket
        #Header add Connection "Upgrade"
        #RequestHeader set Connection "Upgrade"

        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/mysite.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mysite.com/privkey.pem
</VirtualHost>

lilithebowman avatar Apr 24 '25 16:04 lilithebowman