Nginx +(nodejs,socketio,express)+ php站点 [英] Nginx + (nodejs, socketio, express) + php site
问题描述
我是一个程序员,但只是网络服务器后端世界的菜鸟,这就是为什么我要求你的帮助。我对整个系统模型感到困惑,并希望澄清它的工作原理。也许,我在短时间内读了太多的文章。
首先:我在我的网络服务器上运行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中的请求代理到节点服务器?喜欢:
#如果一个文件不存在于指定的根目录中,没有任何其他文件被清除,我们要通过node.js
try_files $ uri @nodejs;
位置@nodejs
{
proxy_pass 127.0.0.1:8080;
break;
}
From: https://stackoverflow.com/a/14025374/2039342
如果它是一个很好的代理解决方法,当我们需要通过nginx进行websocket通信?当我们想要一个常规的php网站和socket.io连接在它里面。在这个时候,我假设要运行端口80上的流量,并分离标准请求和Websocket流量。在我的情况下,最简单的解决方案是什么?
我知道我的问题是有点混乱,但我很惊讶地了解确切的技术。请给我一些提示!文章阅读|起点|基本配置。
PS:我已经阅读了大部分相关主题。
Ps2。 :看起来不那么愚蠢:我已经在red5(基于java的Flash服务器)+ flash中完成了这个游戏,所以我只想重新考虑并用适当的当前技术发布它。
最后,我的基本问题是以正确的方式配置nginx。
首先我将nginx重新安装为修补版本使用nginx_tcp_proxy_module。
下一步是设置正确的配置来处理请求:通过http或tcp。
我希望通过webroot正常地提供标准文件,只有node.js(和socket.io js本身的c)和.php文件的php_fpm的游戏逻辑。
所以我结束了以下工作的nginx设置:
用户www-data;
worker_processes 16;
事件{
worker_connections 1024;
}
http {
上游节点-js-myapp {
服务器127.0.0.1:3000;
}
包括mime.types;
default_type application / octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name domain.xx; #由空格分隔的多个主机名
root /var/www/domain.xx; #替换这个
字符集utf-8;
access_log /var/log/nginx/domain.xx.access.log组合;
error_log /var/log/nginx/domain.xx.error.log;
位置〜\.php $ {
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
包含/etc/nginx/conf.d/php_fpm; #包含PHP-FPM的配置(见下文)
}
location / {
index index.html index.htm;
}
位置^〜/socket.io/ {
try_files $ uri @ node-js-myapp;
}
位置/状态{
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主机$ 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服务器工作正常: / p>
var app = require('express')。createServer()
var io = require('socket.io' )。听(APP);
io.set('transports',[
'websocket'
,'flashsocket'
,'htmlfile'
,'xhr-polling'
,'jsonp-polling'
]);
app.listen(8080);
虽然所请求的文件位于我的服务器的公共端,但在HEAD部分中:
< script src =/ socket.io/socket.io.js\"></script>
我很确定我的nginx不完整,可以包含公牛,但它是善良的的工作和良好的起点。
I'm working on a fully js, HTML5 canvas game and want it to be 'real-time'. Based on my research I find out node.js is an exciting prospect, so I configured it on my ubuntu 12 webserver with socket.io, express etc.
I'm a programmer, but just a rookie in the world of webserver backends, that's why I ask for your help. I got confused about the overall system model and want to be clarified how it's working. Maybe, I've read too much article in a short time.
First of all: I run nginx 1.2.x on my webserver. As I know, nginx is handling the rquests, it's dedicated to port 80 (for me) and serving http requests (also using php-fpm to serve php). Then again, I have a succesfully running nodejs server on port 8080. I want the connection via websocket (due it's nature and protocol), since nginx not support websocket yet I got confused about what's going on.
If I go to http//mydomain.tld:8080, is this going to through node server and keep off nginx? In this case the connection could be via websocket and not falling back to xhr or anything else (i dont want it, because of scalability), right?
Then what should i do to have the same effect at http//mydomain.tld/game/ ? Just proxy the request in nginx.conf to node server? Like:
# 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;
}
From: https://stackoverflow.com/a/14025374/2039342
And if it is a good proxy workaround when we need the websocket communication via nginx? Do we when we want a regular php site and socket.io connection inside it. By this time I presume the point is to run the traffic on port 80 and separate standard requests and websocket traffic. In my case what is the simpliest solution?
http://www.exratione.com/2012/07/proxying-websocket-traffic-for-nodejs-the-present-state-of-play/ in this article i found out HAProxy could be the one for me till nginx 1.3, is it?
I know my questions are a bit chaotic, but I'm straggling to understand the exact technik. Please give me some hint | article to read | starting point | basic config.
PS.: I've read the most of the related topics here.
Ps2.: to look less dumb: I've already done this game in red5 (java based flash server) + flash, so I just want to reconsider and publish it with proper current technologies.
Finally, my basic problem was configuring the nginx in the right way.
First I reinstalled nginx as a patched version with nginx_tcp_proxy_module.
The next step was setting up the right config to handle requests: via http or tcp. I wanted the standard files to be served normally from webroot, just the game logic by node.js (and the socket.io js itself ofc) and .php files by php_fpm.
So I ended up with the following working nginx setup:
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;
}
}
It's working well with this node.js server:
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);
While the requested file is in the public side of my server and in its HEAD section:
<script src="/socket.io/socket.io.js"></script>
I'm pretty sure my nginx is not complete and could contain bulls..., but it's kind of working and a good starting point.
这篇关于Nginx +(nodejs,socketio,express)+ php站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!