Angular/Sockjs通过Docker+ nginx do

发布于 2025-02-02 15:22:21 字数 1386 浏览 1 评论 0原文

我使用以下命令在 linux 上运行Angular:ng Serve -horst 0.0.0.0 App

[WDS]实时重新加载效果很好。

但是,当我尝试将 Angular 放在代理 nginx 的Docker中时,由于Websocket-connection失败而停止实时重载停止工作:“ get/sockjs-node/034 /gdu0ifc0/websocket http/1.1“ 499(在建立连接之前已关闭WebSocket)

这是我的nginx配置:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 80;
    server_name app.local;

    location ~* ^/sockjs-node/[0-9]+/[A-Za-z0-9]+/websocket {
        resolver 127.0.0.1 valid=30s;
        set $front_host http://app_1.localnet;
        proxy_pass $front_host:4200;


        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
    }

    location / {
        try_files $uri @proxyapp;
    }

    location @proxyapp {
        resolver 127.0.0.11 ipv6=off;
        set $dev_container http://app_1.localnet;
        proxy_pass $dev_container:4200;
        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-Proto $scheme;
        proxy_set_header Forwarded "";
    }
}

我使用了很多配置说明来修复它,但是没有什么可用的...

我在做什么...我在做什么错误的?)

I run angular on Linux using the following command: ng serve --host 0.0.0.0 app

[WDS] Live Reloading works well.

But when I try to place angular in docker with proxying nginx, Live Reloading stops working because of the websocket-connection failure: "GET /sockjs-node/034/gdu0ifc0/websocket HTTP/1.1" 499 (WebSocket is closed before the connection is established)

Here is my nginx configuration:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 80;
    server_name app.local;

    location ~* ^/sockjs-node/[0-9]+/[A-Za-z0-9]+/websocket {
        resolver 127.0.0.1 valid=30s;
        set $front_host http://app_1.localnet;
        proxy_pass $front_host:4200;


        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
    }

    location / {
        try_files $uri @proxyapp;
    }

    location @proxyapp {
        resolver 127.0.0.11 ipv6=off;
        set $dev_container http://app_1.localnet;
        proxy_pass $dev_container:4200;
        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-Proto $scheme;
        proxy_set_header Forwarded "";
    }
}

I used a lot of config instructions to fix it, but nothing works...

What am I doing wrong?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2025-02-09 15:22:21

由于Web插座使用两个HTTP标头(连接和升级),因此您的Websocket连接故障发生了。

添加' proxy_set_header升级$ http_upgrade; '和' proxy_set_header连接$ http_connection; 'to nginx config:

location @proxyapp {
        resolver 127.0.0.11 ipv6=off;
        set $dev_container http://app_1.localnet;
        proxy_pass $dev_container:4200;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
        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-Proto $scheme;
        proxy_set_header Forwarded "";
    }

edit:edit:edit:

edit:edity

location / {
        proxy_pass       http://localhost:4200;
        proxy_set_header Upgrade    $http_upgrade;
        proxy_set_header Connection $http_connection;
        proxy_set_header Host       $host;
    }

NG服务-Public-Host 0.0.0.0应用

Your websocket-connection failure happens because web sockets use two HTTP headers (Connection and Upgrade).

Add 'proxy_set_header Upgrade $http_upgrade;' and 'proxy_set_header Connection $http_connection;' to your nginx config:

location @proxyapp {
        resolver 127.0.0.11 ipv6=off;
        set $dev_container http://app_1.localnet;
        proxy_pass $dev_container:4200;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $http_connection;
        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-Proto $scheme;
        proxy_set_header Forwarded "";
    }

Edit:

Try remove everything and use:

location / {
        proxy_pass       http://localhost:4200;
        proxy_set_header Upgrade    $http_upgrade;
        proxy_set_header Connection $http_connection;
        proxy_set_header Host       $host;
    }

Furthermore, run your app with:

ng serve --public-host 0.0.0.0 app

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文