我想将 nodejs 绑定(bind)到一个 url,就像这样: http://myproject.com/nodejs/
目前,我的 Node 在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;
}
}
当我打开 url 时,我得到:
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
我在这个链接中看到了这个 conf:http://mattpatenaude.com/hosting-chatroom/
关于node.js - Nginx 在 80 端口设置 nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294775/