从2天前开始,nginx开始支持websocket连接,因此我试图让我的nginx-nodejs-socket.io应用程序在没有HAproxy ect的情况下工作(虽然没有多less运气)。
我想要达到的是nginx发送只有websocket连接请求到支持的服务器,或websocket服务器,socket.io更准确,而在同一时间nginx将服务PHP文件,所有的静态内容,包括HTML文件。我不想快递服务静态内容(如果这是可能的话)。
这是我的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; # } #} }
这里是我的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');
问题是,当我尝试正常连接时,在Chrome浏览器中inputlocalhost,我在控制台上看到:
GET http://localhost/socket.io/socket.io.js 404 (Not Found)
而且,当在浏览器中input: http://localhost/connection我收到“ 不能GET /连接 ”这是告诉我,nginx不是通常使用我当前的configurationwebsockets。
好的websockets通过nginx在这里工作“
我将我的位置部分更改为:
location / { 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://backend; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
现在它使得networking套接字连接速度非常快。 Yipee!
socket.io有点偷偷摸摸,它从node_modules目录中的某个地方自动提供客户端脚本socket.io.js。
所以你要做的就是告诉nginx把那个位置的请求传递给你的node.js。 在我的nginx.conf中有:
location /chat { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location /socket.io { proxy_pass http://backend; }
但是,只有当我的websocket连接失败时才会出现502错误的网关错误。
之后,socket.io又回到了xhr-polling,这非常非常缓慢。