简体   繁体   English

Angular 13 反向代理 web 找不到套接字错误 404

[英]Angular 13 reverse proxy web socket error 404 not found

Versions:版本:

Angular 13 : "socket.io-client": "^4.5.0"
Nodejs 16.x.x : "socket.io": "^4.5.0"

Problem: When running websocket with static websocket endpoint(using socket.io-client), I am able to connect to backend and the code works fine.问题:当运行 websocket 和 static websocket 端点(使用 socket.io-client)时,我能够连接到后端并且代码工作正常。 But if I use angular proxy config it is unable to reach the backend and I get an error.但是,如果我使用 angular 代理配置,它无法到达后端,我会收到错误消息。 Below are the configuration and the headers from the browser.以下是来自浏览器的配置和标头。

Code (NodeJs)代码(NodeJs)

const http = require('http');
const chat = require('./chat');
var server = http.createServer(app);
const io = require('socket.io')(server, {
  cors: true,
  origin: '*',
  rejectUnauthorized: false
});
chat.createSocket(io)

Code (Angular)代码(角度)

this.socket = io("http://localhost:3000");

Reply Headers in chrome在 chrome 中回复标题

Request URL: http://localhost:3000/socket.io/?EIO=4&transport=polling&t=O36i0QS&sid=BtttiXg7zxVnEyCRAAAA
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:3000
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 58
Content-Type: text/plain; charset=UTF-8
Date: Sun, 15 May 2022 07:47:46 GMT
Keep-Alive: timeout=5
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Host: localhost:3000
Origin: http://localhost:4200
Referer: http://localhost:4200/
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36

Code #2 (Angular)代码 #2(角度)

Proxy Config:代理配置:


{
    "context": [
      "/socket/*"
    ],
    "target": "http://localhost:3000/socket.io/",
    "ws": true,
    "logLevel": "debug"
  }

service code:服务代码:

this.socket = io("/socket");

Reply Headers in chrome:在 chrome 中回复标题:

Request URL: http://localhost:4200/socket.io/?EIO=4&transport=polling&t=O36iuH0
Request Method: GET
Status Code: 404 Not Found
Remote Address: 127.0.0.1:4200
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 149
Content-Security-Policy: default-src 'none'
Content-Type: text/html; charset=utf-8
Date: Sun, 15 May 2022 07:51:35 GMT
Keep-Alive: timeout=5
X-Content-Type-Options: nosniff
X-Powered-By: Express
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Cookie: connect.sid=s%3AngS43jIUDoTzp30plUzBEQ0od_I-Br_o.Hth2gCXa8Dw3BwrN%2FN5MXYtAhRvwtLhNcPTc4igQie4
Host: localhost:4200
Referer: http://localhost:4200/home
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36

Update on running angular with command使用命令运行 angular 的更新

ng serve --proxy-config proxy.conf.json --verbose

Angular Compiler logs show: Angular 编译器日志显示:

[webpack-dev-server] [HPM] Proxy created: /sock/*  -> http://localhost:3000/

And on initiaiting a socket connection I am now getting an error in the logs在启动套接字连接时,我现在在日志中收到错误消息

[webpack-dev-server] [connect-history-api-fallback] Not rewriting GET /socket.io/?EIO=4&transport=polling&t=O37i8TH because the client does not accept HTML.

Is there something wrong in this configuration?这个配置有问题吗? I am using reverse proxy for making normal server calls and it works fine.我正在使用反向代理进行正常的服务器调用,它工作正常。 I am able to receive data from the backend.我能够从后端接收数据。 But not in the case of websockets.但不是在 websockets 的情况下。

Please let me know if you need any more information I will edit the question accordingly.如果您需要更多信息,请告诉我,我将相应地编辑问题。 Thanks in advance.提前致谢。

You need to change in your proxy.conf.json您需要更改您的proxy.conf.json

{
  "/sock/*": {
    "target": "http://localhost:3000/socket.io/",
    "ws": true,
    "logLevel": "debug"
  }
}

Run angular project by ng serve --proxy-config proxy.conf.json通过ng serve --proxy-config proxy.conf.json运行 angular 项目

You should see in terminal你应该在终端看到

10% building modules 2/2 modules 0 active[HPM] Proxy created: /sock  ->  http://localhost:3000

I hope you clear with the solution我希望你清楚解决方案

Finally, I've found the solution, got the hint from the nginx configuration.最后,我找到了解决方案,从 nginx 配置中得到了提示。

It worked when I added a route before socket.io.当我在 socket.io 之前添加一条路线时,它起作用了。

Configure the socket path at the frontend as:在前端配置套接字路径为:

this.socket = io('/', {
      path: "/chat/socket.io/"
    });

proxy-cofig as:代理配置为:

{
    "context": [
      "/chat"
    ],
    "target": "http://localhost:3000/",
    "secure": false,
    "ws": true,
    "logLevel": "debug"
  }

Configure the backend as:将后端配置为:

const io = require('socket.io')(server, {
  path: '/chat/socket.io',
  secure: true,
  // rejectUnauthorized: false,
  cors: true
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM