Heroku nginx websockets

时间:2017-02-19 22:20:39

标签: node.js nginx heroku websocket reverse-proxy

我一直试图让websockets在nginx上运行heroku并且似乎被卡住了。我正在使用效果很好的nginx-buildpack但到目前为止我还没有成功获得升级的websocket连接。

这是我的nginx.conf.erb,它只是从buildpack示例中稍作修改......

daemon off;
#Heroku dynos have 4 cores.
worker_processes 4;

events {
    use epoll;
    accept_mutex on;
    worker_connections 1024;
}

http {
    gzip on;
    gzip_comp_level 2;
    gzip_min_length 512;

    log_format l2met 'measure.nginx.service=$request_time request_id=$http_heroku_request_id';
    access_log logs/nginx/access.log l2met;
    error_log logs/nginx/error.log;

    include mime.types;
    default_type application/octet-stream;
    sendfile on;

    #Must read the body in 5 seconds.
    client_body_timeout 5;
    proxy_read_timeout 950s;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    server {
        listen <%= ENV["PORT"] %>;
        server_name _;
        keepalive_timeout 5;

        location /test {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://127.0.0.1:3000;
        }
        location / {
            proxy_pass http://127.0.0.1:3001;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;
        }
    }
}

测试此配置&lt;我刚刚使用websocket.org Echo Test。不幸的是它无法连接。

在端口3001上我刚刚有一个简单的socket.io服务器,我正在记录任何连接(到目前为止都没有...)......

var app2 = require('http').createServer().listen(3001);
var io = require('socket.io').listen(app2);

io.on('connection', function(socketconnection){
    socketconnection.send("Connected to Server-1");
    console.log("connected to websocket server!");
    socketconnection.on('message', function(message){
        socketconnection.send(message);
    });
});

当我尝试连接websockets测试仪时,这是我在heroku日志中出现的错误......

* 3上游过早关闭连接同时读取响应    来自上游的头,客户端:10.140.231.210,服务器:_,请求:“GET /?encoding = text HTTP / 1.1”,上游:“http://127.0.0.1:3001/?encoding=t  ext“,主持人:”www.mydomain.com“

我在这里做错了什么想法?

更新1 : 好的,我已经发现,如果我改变配置的部分,那就是:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

为:

map $http_upgrade $connection_upgrade {
    default upgrade;
}

这似乎可以防止发生错误。我想这是因为连接上返回了空白响应。但是,现在我已经这样做了,我得到了一个新的错误:

*5 connect() failed (111: Connection refused) while connecting to upstream, 
client: 10.99.212.2, server: _, request: "GET /?encoding=text HTTP/1.1",
upstream: "http://127.0.0.1:3001/?encoding=text", host: "www.mydomain.com"

我想这可能是与第一个无关的问题,但我不确定!

0 个答案:

没有答案