我正在尝试将 Node + Express + Socket.io 组成的应用程序配置为我的服务器上的生产环境,但几个小时后我仍然遇到相同的错误。
索引.js:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(3000);
索引.twig
<script src="/node_modules/jquery/dist/jquery.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
...
配置 Nginx
server {
listen 80;
server_name sub.domain.com;
location / {
proxy_pass http://127.0.0.1:3000/;
}
location /socket.io/ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:3000/socket.io/;
}
}
错误 502 我明白了:
socket.io/socket.io.js net::ERR_ABORTED 502 (Bad Gateway)