Home > Article > Web Front-end > How to solve sockjs-node error reporting problem
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.
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
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:
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.
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.
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!