Home  >  Article  >  Web Front-end  >  How to solve sockjs-node error reporting problem

How to solve sockjs-node error reporting problem

藏色散人
藏色散人Original
2021-12-08 11:18:315703browse

Solution to the error reported by sockjs-node: 1. Configure port 9097 on nginx; 2. Allow "/sockjs-node" to be properly proxied to the actual port of the machine.

How to solve sockjs-node error reporting problem

The operating environment of this article: Windows 7 system, nodejs version 10.16.2, Dell G3 computer.

How to solve sockjs-node error reporting problem?

sockjs-node interface error solution

project Background

When I took over other people's angular projects (vue.js may also encounter this problem), nginx was configured on the cross-domain front-end. After the project was started, the background ws reported an error, as shown in the following figure:
How to solve sockjs-node error reporting problem

Problem location:

Searched the Internet for the cause of the error and found that sockjs-node is the nodejs hot-loading interface used for browser and local communication. At this time, the hot-loading function fails. Therefore, the reason for the error is: this communication interface accesses the 9097 port of the nginx proxy instead of the 8082 port where the actual service is started, so communication cannot be carried out.

Solution:

Configure this 9097 port on nginx so that /sockjs-node can normally proxy to the actual port of the machine to solve the problem.
Specific measures:
Add nginx configuration in nginx

http {
    include       mime.types;
    default_type  application/octet-stream;
    map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
  }
 server {
        listen 9097;
        server_name localhost;

	location /sockjs-node {
            proxy_pass http://0.0.0.0:8082;
	    proxy_set_header Upgrade $http_upgrade;
	    proxy_set_header Connection $connection_upgrade;
        }
    }

Note: The most important thing about the above is to add the following two lines to the reverse proxy configuration. Others There is no difference between the part and the ordinary HTTP reverse proxy.
roxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
The key part here is that the HTTP request has the following header:
Upgrade: websocket
Connection: Upgrade
These two fields indicate that the server is requested to upgrade the protocol to WebSocket.

Conclusion

In actual situations, there may be many reasons for /sockjs-node error reporting, including shadowsock proxy, nodejs configuration, etc. As long as you understand the nature of the problem and correctly allow ws to access the local actual The address is enough.

Recommended learning: "nodejs video tutorial"

The above is the detailed content of How to solve sockjs-node error reporting problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn