我想将bind 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/');
我该怎么做?
答案 0 :(得分:5)
我收到错误= /
WebSocket connection to 'ws://myproject/socket.io/1/websocket/IomP5jiBBNv8rgGYFFUS' failed: Error during WebSocket handshake: 'Connection' header value is not 'Upgrade'
这是我的nginx服务器端conf:
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/
答案 1 :(得分:2)
之前在另一个帖子中回复: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;
}
答案 2 :(得分:1)
我从未遇到过这样的配置开关,允许从URL更改 socket.io 部分。但是AFAIK,如果您使用此配置,您的问题将得到解决:
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/
答案 3 :(得分:0)
我还不能发表评论,但你确定你的主机名没有错字吗?
您已经发布了一些代码片段,一切都取决于您配置的内容。
答案 4 :(得分:0)
这只是websocket不起作用,如果我从io传输中删除websocket,一切正常,我在我的客户端使用这一行:
io.transports = [ 'xhr-polling', 'jsonp-polling', 'htmlfile' ];
如果我不编辑此传输列表,它可以正常工作但是,我有一个websocket错误,并且在10秒后,客户端使用xhr-polling并且它正常工作。