5

我正在开发一个完全 js、HTML5 画布游戏,并希望它是“实时的”。根据我的研究,我发现 node.js 是一个令人兴奋的前景,所以我在我的 ubuntu 12 网络服务器上使用 socket.io、express 等配置了它。

我是一名程序员,但只是网络服务器后端领域的新手,这就是我寻求您帮助的原因。我对整个系统模型感到困惑,想弄清楚它是如何工作的。也许,我在短时间内阅读了太多文章。

首先:我在我的网络服务器上运行 nginx 1.2.x。据我所知,nginx 正在处理 rquests,它专用于端口 80(对我而言)和服务 http 请求(也使用 php-fpm 服务 php)。再说一次,我在端口 8080 上有一个成功运行的 nodejs 服务器。我想要通过 websocket 连接(由于它的性质和协议),因为 nginx 不支持 websocket 但我对发生的事情感到困惑。

如果我去 http//mydomain.tld:8080,这是否会通过节点服务器并远离 nginx?在这种情况下,连接可以通过 websocket 而不是回退到 xhr 或其他任何东西(我不想要它,因为可伸缩性),对吧?

那么我应该怎么做才能在 http//mydomain.tld/game/ 产生相同的效果?只是将 nginx.conf 中的请求代理到节点服务器?像:

 # if a file does not exist in the specified root and nothing else is definded, we want to serve the request via node.js
try_files   $uri    @nodejs;          

location @nodejs
{       
     proxy_pass  127.0.0.1:8080;
     break;
}

来自:https ://stackoverflow.com/a/14025374/2039342

当我们需要通过 nginx 进行 websocket 通信时,它是否是一个很好的代理解决方法?当我们想要一个常规的 php 站点和其中的 socket.io 连接时,我们会这样做吗?到这个时候,我认为重点是在端口 80 上运行流量,并将标准请求和 websocket 流量分开。就我而言,最简单的解决方案是什么?

http://www.exratione.com/2012/07/proxying-websocket-traffic-for-nodejs-the-present-state-of-play/在这篇文章中我发现 HAProxy 可能是我直到 nginx 1.3 的那个, 是吗?

我知道我的问题有点混乱,但我正在努力理解确切的技术。请给我一些提示| 文章阅读 | 起点 | 基本配置。

PS.:我在这里阅读了大部分相关主题。

Ps2.:看起来不那么愚蠢:我已经在red5(基于java的Flash服务器)+ Flash中完成了这个游戏,所以我只想重新考虑并使用适当的当前技术发布它。

4

1 回答 1

6

最后,我的基本问题是以正确的方式配置 nginx。

首先,我使用 nginx_tcp_proxy_module 重新安装了 nginx 作为补丁版本。

下一步是设置正确的配置来处理请求:通过 http 或 tcp。我希望从 webroot 正常提供标准文件,只是 node.js 的游戏逻辑(以及 socket.io js 本身 ofc)和 php_fpm 的 .php 文件。

所以我最终得到了以下工作的 nginx 设置:

user  www-data;
worker_processes  16;

events {
    worker_connections  1024;
}

http {
    upstream node-js-myapp {
        server 127.0.0.1:3000;
    }

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

    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen          80;
        server_name    domain.xx;  # Multiple hostnames seperated by spaces
        root            /var/www/domain.xx; # Replace this
        charset         utf-8;
        access_log  /var/log/nginx/domain.xx.access.log  combined;
        error_log   /var/log/nginx/domain.xx.error.log;

        location ~ \.php$ {
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name; 
                include /etc/nginx/conf.d/php_fpm; # Includes config for PHP-FPM (see below)
        }


        location / {
            index  index.html index.htm;
        }


        location ^~ /socket.io/ {
            try_files $uri @node-js-myapp;
        }

        location /status {
            check_status;
        }

        location @node-js-myapp { 
          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_pass  http://node-js-myapp;
        }
    }
}

tcp {
  upstream websocket-myapp {
    server 127.0.0.1:8080;
    check interval=3000 rise=2 fall=5 timeout=1000;
  }

  server {
    listen 3000;
    server_name _;
    access_log  /var/log/nginx/domain.xx.access.log;

    proxy_read_timeout 200000;
    proxy_send_timeout 200000;
    proxy_pass websocket-myapp;
  }
}

它与这个 node.js 服务器运行良好:

var app = require('express').createServer()
var io = require('socket.io').listen(app);
io.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
  ]);

app.listen(8080);

虽然请求的文件位于我的服务器的公共端及其 HEAD 部分:

<script src="/socket.io/socket.io.js"></script>

我很确定我的 nginx 不完整并且可能包含公牛...,但它有点工作并且是一个很好的起点。

于 2013-02-05T13:59:11.677 回答