node.js - 将 Websocket 流量从 Nginx 代理到 Socket.io(无 SSL)

标签 node.js nginx express websocket socket.io

从昨天开始,nginx 开始支持 websocket 连接,因此我试图让我的 nginx-nodejs-socket.io 应用程序在没有 harproxy 等的情况下工作(虽然不太幸运)。

我真正想要实现的是 nginx 只发送 websocket 连接请求到支持的服务器,或者 websocket 服务器,socket.io 更准确,同时 nginx 将提供 php 文件和所有静态内容包括 html 文件。我根本不希望 express 提供静态内容(如果可能的话)。

这是我的 nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    upstream backend {
        server 127.0.0.1:8080;
    }

    server {
        listen       80;
        server_name  localhost;

        charset UTF-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   /website/html_public;
            index  index.php index.html index.htm;
        }       
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /website/html_public;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
        root           /website/html_public;
        try_files      $uri =404;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }

         location /connection {
         proxy_pass http://backend;  
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection "upgrade";
             }
       }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   /website/html_public;
    #        index  index.php index.html index.htm;
    #    }
    #}
}

这是我在 Node 中的 server.js 文件

var express = require('express');
var app     = express();
var port    = 8080;

/* HTTP Server*/

server = require('http').createServer(app);
server.listen(port);
app.use(express.logger(':remote-addr - :method :url HTTP/:http-version :status :res[content-length] - :response-time ms'));
app.use(express.static(__dirname + '/html_public'));
app.use(express.favicon());

app.set('view engine', 'jade');
app.set('view options', { layout: false });
app.set('views', __dirname + '/views');

app.get('/', function(req, res){
    res.render('index.html');

});

/*
* Web Sockets
*/
io   = require('socket.io').listen(server),
io.configure('production', function(){
io.enable('browser client etag');
io.set('log level', 1);
io.set('transports', [ 'websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling' ]);
});


console.log('Chat Server started with Node '+ process.version +', platform '+ process.platform + 'to port %d',port); 

从我的客户那里,我尝试这样连接:

socket = new io.connect('http://localhost/connection');

现在,问题是当我尝试正常连接时,键入 localhost,我在 chrome 控制台上看到: **GET http://localhost/socket.io/socket.io.js 404 (Not Found)** ,在浏览器中输入 http://localhost/connection 我收到 “Cannot GET/connection” 这告诉我 nginx 不正常代理 websockets 我当前的配置。

预先感谢您的回答。

最佳答案

让我猜猜,你想要:

location /connection {
    proxy_pass http://backend/;
    ...
}

但是你有:

location /connection {
    proxy_pass http://backend;
    ...
}

http://nginx.org/r/proxy_pass

关于node.js - 将 Websocket 流量从 Nginx 代理到 Socket.io(无 SSL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14988791/

相关文章:

node.js - 对其应用程序脚本进行更改后重新启动 node.js 服务器的最佳方法是什么

apache - 从 Apache 到 Nginx 的大括号({ 和 })重写规则。

node.js - 从请求url中获取IP地址

javascript - 如何在 Pug 中定义内联变量

node.js - ExpressJS 请求正文中 JSON 中的反斜杠 - 使用 body-parser

node.js - 无法使用 Mongoose 连接到 mongodb 图集

node.js - 跨多个 Amazon EC2 实例水平扩展 socket.io

javascript - 使用 mongodb (waterline.js) 和 node.js (sails.js) 解决事务

mysql - 在node.js中使用promise处理MySQL返回值

google-chrome - NET::ERR_CERT_AUTHORITY_INVALID 在 Chrome 中不是隐身和 Firefox 在本地使用 nginx 上的有效证书