Skip to content

[Bug]: proxy by nginx websocket origin check error #6161

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

Closed
4 tasks done
502647092 opened this issue Apr 23, 2023 · 4 comments
Closed
4 tasks done

[Bug]: proxy by nginx websocket origin check error #6161

502647092 opened this issue Apr 23, 2023 · 4 comments
Labels
bug Something isn't working triage This issue needs to be triaged by a maintainer

Comments

@502647092
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

OS/Web Information

  • Web Browser: EDGE
  • Local OS: Windows
  • Remote OS: Linux
  • Remote Architecture: x64
  • code-server --version: 4.12.0

Steps to Reproduce

  1. start code server by docker
  2. proxy by nginx
  3. open website

Expected

.

Actual

websocket connect error code 1006

Logs

debug host "ide.wsc.ink:443" does not match origin "ide.wsc.ink";

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 HTTPS.

Notes

No response

@502647092 502647092 added bug Something isn't working triage This issue needs to be triaged by a maintainer labels Apr 23, 2023
@502647092
Copy link
Author

I fix it by modify nginx config
Origin Config

            proxy_set_header      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-Host    $host:$server_port;
            proxy_set_header      X-Forwarded-Proto   $scheme;

Modified

            proxy_set_header      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-Host    $host;    <=== remove port
            proxy_set_header      X-Forwarded-Proto   $scheme;

@502647092 502647092 changed the title [Bug]: proxy by nginx origin check error [Bug]: proxy by nginx websocket origin check error Apr 23, 2023
@code-asher
Copy link
Member

Oh interesting! We should make special exceptions for 443 and 80. #6166

@kylefmohr
Copy link

kylefmohr commented Jun 28, 2023

@502647092

I fix it by modify nginx config Origin Config

            proxy_set_header      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-Host    $host:$server_port;
            proxy_set_header      X-Forwarded-Proto   $scheme;

Modified

            proxy_set_header      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-Host    $host;    <=== remove port
            proxy_set_header      X-Forwarded-Proto   $scheme;

Thanks for this, I was a few versions behind on code-server, and was getting this error. I had none of these set in my old nginx file, and adding them solved the issue for me.

@mashroomxl
Copy link

mashroomxl commented Nov 12, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage This issue needs to be triaged by a maintainer
Projects
None yet
Development

No branches or pull requests

4 participants