By PatrickB

2014-08-29 15:05:18 8 Comments

i've got this error requests.

Errors from FireBug

The last sentence in german means "Firefox cant connect to the server which is located in ws://.......".

The server wouldnt be the problem i think.

Because that here is the nginx configuration, because i think there is the problem!

server {
    listen 80 default_server;
    root /var/www/web;

    location / # for symfony2
        try_files $uri @rewriteapp;

    location @rewriteapp # for symfony2
                rewrite ^(.*)$ /app.php/$1 last;

    location ~ ^/app\.php(/|$)
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param HTTPS off;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    location ~ ^/socket
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

nginx version: nginx/1.4.7

app.js (thats the server!)

var express = require('express'),
    io = require('').listen(server),
    server = require('http').createServer(app),
    bodyParser = require('body-parser');

var app = express();

app.use(bodyParser.json());'/', function(request, response)
    io.emit('MessageForAll', request.body);

io.on('connection', function (socket){});

console.log('Server running on port 8080.');


@rajesh ujade 2014-09-01 14:06:29

  1. Nginx(nginx version: nginx/1.4.6) Change:-

    server {
            listen   80;
            root /var/www/stack/25571070;
            index  index.html index.htm;
            location / {
            location ^~ /socket {
               rewrite  ^/socket/(.*)  /$1 break; #used to send request to base url
               proxy_redirect off;
               proxy_pass_request_headers on;
               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;

Note: You need to change location ~ ^/socket to location ^~ /socket

Node Changes:

  1. app.js:

    app.enable('trust proxy');
    app.set('port', process.env.PORT || 3000);
    var server = app.listen(app.get('port'), function() {
         debug('Express server listening on port ' + server.address().port);
    var sockets = require('')({
      'transports': ['websocket', 'flashsocket','htmlfile','xhr-polling','jsonp-polling']
    var io = sockets.listen(server,{ resource: '/','sync disconnect on unload':true });
    io.sockets.on('connection', function (socket) {  
      setInterval(function() {socket.emit('news', { hello: 'hello world' })}, 1000);
  2. index.ejs:

    <!DOCTYPE html>
        <title><%= title %></title>
        <link rel='stylesheet' href='/socket/stylesheets/style.css' />
        <h1><%= title %></h1>
        <p>Welcome to <%= title %></p>
        <div id="divID">
        <script src=""></script>
                var socket = io.connect('ws://');
                //var socket = io.connect('');
                var i = 0;
                socket.on('news', function(data) {
                    var div = document.getElementById('divID');
                    i = i + 1;
                    div.innerHTML = div.innerHTML + '<p>'+ data.hello+'('+i+')'+'</p>';


  "name": "25571070",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  "dependencies": {
    "body-parser": "~1.6.6",
    "cookie-parser": "~1.3.2",
    "debug": "~1.0.4",
    "ejs": "~0.8.5",
    "express": "~4.8.6",
    "moment": "^2.8.2",
    "morgan": "^1.2.3",
    "serve-favicon": "^2.0.1",
    "": "^1.0.6",
    "stylus": "0.42.3"

Firefox Response:

Firefox websocket response

Chrome Response:

Chrome Websocket Response

FYI. I have used below version:

  • "node": "v0.10.31"
  • "ejs": "~0.8.5"
  • "express": "~4.8.6",
  • "": "^1.0.6"
  • "nginx": "1.4.6"

For Quick Start With node.js go to

@PatrickB 2014-09-02 07:45:34

xhr polling works, i think. Look at the screenshot above. The problem is that ws:// doesnt work. If the system using xhr polling i could use ajax requests to background server instead using a extra server. But thanks for your answer.

@rajesh ujade 2014-09-03 10:06:36

@PatrickB I have changed the answer.Tested on Google Chrome Version 37.0.2062.94 (64-bit) and Mozilla Firefox version 32.0.

@PatrickB 2014-09-08 10:10:33

Wtf... it doesnt works for me :( I dont know why the server doesnt send 101 switchting protocols...

@rajesh ujade 2014-09-08 10:24:17

@PatrickB Can you please try to setup a new instance on your local server as I said above or put location ^~ /socket on top just above location/ or below location/ in ngnix configuration.I am sure its happens only because of ngnix configuration. I tried it with your same directory except symfony2 setup. I am updating answer with package.json too. Please check it.

@PatrickB 2014-09-08 10:41:44

I did. Thats the result. Thats the app.js:

@rajesh ujade 2014-09-08 10:56:35

@PatrickB I am pushing my code and ngnix config in git now. I have tried your code. It working on my system.

@PatrickB 2014-09-08 11:00:11

Yes thanks for help. I will checkout your git repository and check it. I could give you access to the server if i cant handle the problem. I think there is something with the nginx proxy... The Server said 400 and thats an error ... also no bad request... an error from the server. I try to debug nginx and now.

@rajesh ujade 2014-09-08 11:14:41

I dont understand I think the url should be like this []. Also you need to add router in app.js like var routes = require('./routes/index'); app.use('/', routes);

@enRaiser 2019-07-19 12:15:17

Thanks a lot, you saved my week

@Stefano Falsetto 2014-09-02 13:59:49

Can I see the server code?

Have you double-checked that the websocket port is not blocked by any firewall?

I think you've already read this blog post, right?

@PatrickB 2014-09-03 06:46:22

Yes i had. The only port which is accessable from remote hosts is the port 80. The Server port 8080 is local reachable from the nginx server. In your blog post, which i readed before, uses only port 8020 from internet (reachable for everyone) and the port 8010 for internal use. I add the server code for you above. Wait a minute ^^

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Node.js + Nginx - What now?

  • 2011-02-15 20:49:02
  • Van Coding
  • 331916 View
  • 941 Score
  • 11 Answer
  • Tags:   node.js nginx concept

19 Answered Questions

[SOLVED] How is an HTTP POST request made in node.js?

3 Answered Questions

[SOLVED] Share Nginx server configuration

  • 2014-06-11 04:04:32
  • Anam
  • 1563 View
  • 4 Score
  • 3 Answer
  • Tags:   php nginx

0 Answered Questions

Cannot get index.php page to display in docker container

  • 2019-02-06 04:42:32
  • sp156
  • 176 View
  • 0 Score
  • 0 Answer
  • Tags:   php docker nginx

2 Answered Questions

Keycloak Redirect url with nginx is going to http rather than https

  • 2018-04-03 12:02:15
  • Atulya Nair
  • 1193 View
  • 3 Score
  • 2 Answer
  • Tags:   nginx jboss

2 Answered Questions

[SOLVED] Express - req.ip returns

1 Answered Questions

[SOLVED] Continious Polling issue

1 Answered Questions

Wordpress constant redirect with nginx upstream

1 Answered Questions

[SOLVED] migrating from lighttpd to nginx

Sponsored Content