10

We are trying to run a Meteor application on a Debian server behind Nginx. The application is running but GET requests at http://url/sockjs?info?cb=[random_string] returns 502 Bad Gateway.

The Nginx configuration is set up as thus:

# this section is needed to proxy web-socket connections
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

upstream app_server {
    server 127.0.0.1:3000; # for a web port socket (we'll use this first)
    # server unix:/var/run/app/app.sock;
}

server {
    listen       80;
    server_name  app_server.com;

    charset utf-8;
    client_max_body_size 75M;

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

    location / {
        proxy_pass http://app_server;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Forwarded-For $remote_addr; # preserve client IP
        proxy_read_timeout 60s;
        keepalive_timeout 65;
        proxy_redirect off;
        # the root path (/) MUST NOT be cached
        if ($uri != '/') {
            expires 30d;
        }
    }
}

We have tried various configurations and could not figure out where the fault lies. Solution at Meteor WebSocket handshake error 400 with nginx didn't work either.

Edit: Tried following configuration found at recommended nginx configuration for meteor and it was still returning 502.

Edit: The application works fine when not obtaining images from Meteor CFS, which is used to store uploaded images via an admin dashboard. When loading images, a POST to domain/sockjs/img_location/cb/xhr_send causes a 502 error.

Community
  • 1
  • 1
mrkre
  • 1,548
  • 15
  • 43
  • Maybe try changing the proxy_pass to point to your meteor instance and remove the upstream app_server? Unless you need to load balance I suppose...proxy_pass http://localhost:3000; Also check out this resource perhaps: http://www.meteorpedia.com/read/nginx – Brett McLain Dec 09 '15 at 15:20
  • @Sparticus tried that config previously and the results are the same. – mrkre Dec 09 '15 at 17:23
  • I noticed you're using `proxy_set_header Connection $connection_upgrade;`... did you try `proxy_set_header Connection $http_connection;` instead? – Myst Dec 14 '15 at 23:11
  • @Myst yes, tried that but still getting the same error. Have edited the question with additional input for investigation. – mrkre Dec 15 '15 at 07:02
  • What build pack are you using? – Camron_Godbout Dec 16 '15 at 06:59

2 Answers2

3

Are you sure the issue is really coming from NGINX and websocket?

  • First you can try wcat as a websocket CLI to ensure if the websockets are working
  • You can also try to run the app in a console or look at the log (debug / verbose at max level) to see if there is not an underlying error
samidarko
  • 590
  • 7
  • 20
  • Okay, Websockets is working fine but after some debugging, I receive this: http://pastebin.com/qpAArbyM – mrkre Dec 16 '15 at 09:08
  • seems you got an error with cfs. I found this https://github.com/CollectionFS/Meteor-CollectionFS/issues/664 and if you look at the last comment from Aldeed, he is suggesting to install a package named cfs:http-methods@0.0.30 – samidarko Dec 16 '15 at 09:11
  • Okay. That fixed the issue. Thanks for your help. Please update your answer and I will accept it. Definitely hair-tearing! – mrkre Dec 16 '15 at 10:27
1

As per your question edit, CFS uses an HTTP transport as the underlying data transfer layer.

Unfortunately, depending on how you get CFS into your stack, you might end up with an old and buggy version of its dependencies, namely cfs:http-methods, which sometimes tries to end an already ended response, which then translates itself as a cryptic error message.

Fortunately, the bug has been resolved version 0.0.30 onwards and to ensure that Meteor loads that version as the minimum dependency, all you need to do is edit you .meteor/packages file and add the following:

cfs:http-methods@0.0.30

Which will ensure any version that is equal or greater than 0.0.30, which as of this moment, is the latest on Atmosphere (meteor's package server).

Serkan Durusoy
  • 5,447
  • 2
  • 20
  • 39