我想将nodejs绑定到一个url,如下所示: http://myproject.com/nodejs/
目前,我在端口 8080 有节点。
我有 nginx 配置:
upstream app {
server 127.0.0.1:8080;
}
server {
listen 80; ## listen for ipv4; this line is default and implied
root /home/myproject/www;
index index.html index.htm;
server_name myproject.com;
location /nodejs/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app/;
proxy_redirect off;
}
}
当我打开网址时,我得到:
Welcome to socket.io.
连接正常。
但是,当我尝试连接我的网站时,我得到了这个:
GET http://myproject.com/socket.io/1/?t=1385767934694 404 (Not Found) socket.io.js:1659
这是我用于连接的网站线路:
var socket = io.connect('http://myproject.com/nodejs/');
我该怎么做?
我收到错误=/
WebSocket connection to 'ws://myproject/socket.io/1/websocket/IomP5jiBBNv8rgGYFFUS' failed: Error during WebSocket handshake: 'Connection' header value is not 'Upgrade'
这是我的 nginx 服务器端配置:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80; ## listen for ipv4; this line is default and implied
root /home/myproject/www;
index index.html index.htm;
server_name myproject.com;
location /socket.io/ {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
如果我不添加:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
我收到此错误:
Restarting nginx: nginx: [emerg] unknown "connection_upgrade" variable
nginx: configuration file /etc/nginx/nginx.conf test failed
我在此链接中看到此配置:http://mattpatenaude.com/hosting-chatroom/
早些时候在另一个线程中回复了:https://stackoverflow.com/a/12904282/2324004
不幸的是,Socket.io wiki 缺乏一些信息,但线索是设置资源:
客户
var socket = io.connect('http://localhost:8081', {resource: 'test'});
服务器
var io = require('socket.io').listen(8081, {resource: '/test'});
以上是有效的(我现在已经测试过)。这应该与上面的配置一起使用:
location /test/ {
proxy_pass http://localhost:8081;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
我从未遇到过这样的配置开关,允许更改 URL 中的 socket.io 部分。但据我所知,如果您使用此配置,您的问题将得到解决:
location /socket.io/ {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
http://michieldemey.be/blog/proxying-websockets-with-nginx-and-socket-io/
我还不能发表评论,但你确定你的主机名没有拼写错误吗?
您已经发布了一些代码片段,一切都取决于您配置的内容。
只是 websocket 不起作用,如果我从 io 传输中删除 websocket,一切正常,我在我的客户端上使用此行:
io.transports = [ 'xhr-polling', 'jsonp-polling', 'htmlfile' ];
如果我不编辑此传输列表,它可以工作,但是,我有一个 websocket 错误,10 秒后,客户端使用 xhr-polling 并且它可以工作。
设置时遇到同样的错误
echo.websocket.in
,使用我的 nginx 配置中的以下代码修复
map $http_upgrade $connection_upgrade {
default Upgrade;
'' close;
}