502 Bad Gateway remains! Meteor.js NGINX

May 4, 2016 1k views
Deployment Nginx DNS DigitalOcean Ubuntu

Hi!

I have been researching and trying out many different ways to kill my 502 Bad Gateway but to no success!

Domain Registrar:
GoDaddy.
My normal home web-page's domain is mydomain.com which forwards to an instapage website, but I have a subdomain login.mydomain.com to redirect (301) to my DO droplet. Added an A record for login that points to the DO IP.

Deploying Meteor
I follow the this guide https://www.digitalocean.com/community/tutorials/how-to-deploy-a-meteor-js-application-on-ubuntu-14-04-with-nginx where I run "sudo npm install" while being the myapp user.

My /etc/nginx/sites-available/myapp.conf file:

server_tokens off;
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}
server {
    listen 80 default_server; 
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html; # root is irrelevant
    index index.html index.htm; # this is also irrelevant

    server_name login.mydomain.com;
    location / {
        rewrite     ^ https://$server_name$request_uri? permanent;
    }
}
server {
    listen 443 ssl spdy;
    server_name login.mydomain.com;
    root html;
    index index.html;

    ssl_certificate /etc/nginx/ssl/server.pem;
    ssl_certificate_key /etc/nginx/ssl/server.key;

    ssl_stapling on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;

    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:RC4-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK';

    add_header Strict-Transport-Security "max-age=31536000;";
    if ($http_user_agent ~ "MSIE" ) {
        return 303 https://browser-update.org/update.html;
    }
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; # allow websockets
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Forwarded-For $remote_addr; # preserve client IP
        if ($uri != '/') {
            expires 30d;
        }
    }
}

My /etc/init/myapp.conf file:

description "Meteor.js (NodeJS) application"
author "Daniel Speichert <daniel@speichert.pro>"
start on started mongodb and runlevel [2345]
stop on shutdown
respawn
respawn limit 10 5
setuid myapp
setgid myapp

script
    export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    export NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
    export PWD=/home/myapp
    export HOME=/home/myapp
    export BIND_IP=127.0.0.1
    export PORT=8080
    export HTTP_FORWARDED_COUNT=1
    export MONGO_URL=mongodb://localhost:27017/myapp
    export ROOT_URL=https://login.mydomain.com
    exec node /home/myapp/bundle/main.js >> /home/myapp/myapp.log
end script

NOTES

  • My DO is Ubuntu 14.04 x64
  • the names mydomain and myapp are not the same if that matters
  • I set-up SSL exactly like this: https://www.digitalocean.com/community/tutorials/how-to-create-a-ssl-certificate-on-nginx-for-ubuntu-12-04
  • When I run "start myapp" it says "myapp start/running, process 12218" and when I type "status myapp" it then shows "myapp stop/waiting".
  • NGINX is running.
  • /home/myapp/myapp.log is empty so somehow the Meteor app doesn't see any errors.
  • "nginx -t" returns all OK except SSL stapling warning
  • My NGINX error log says 2016/05/04 00:30:42 [error] 18144#0: *28 connect() failed (111: Connection refused) while connecting to upstream, client: <IP ADDRESS>, server: login.mydomain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8080/favicon.ico", host: "login.mydomain.com", referrer: "https://login.mydomain.com/"
  • My code runs on my localhost just fine on my dev laptop (Mac OS).
  • I cannot access it on <IP ADDRESS>:8080 so it seems it just can't reach Meteor on the server.
  • "sudo netstat -plutn" output: tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12209/nginx
    tcp 0 0 127.0.0.1:28017 0.0.0.0:* LISTEN 2880/mongod
    tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1103/sshd
    tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 12209/nginx
    tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 2880/mongod
    tcp6 0 0 :::80 :::* LISTEN 12209/nginx
    tcp6 0 0 :::22 :::* LISTEN 1103/sshd

Anybody any idea why there's still a 502 Bad Gateway issue?

3 comments
  • Judging from the output of start myapp and netstat -plutn, it sounds like your app may not be starting correctly. Do you see any error output if you try running the application manually on the command line rather than with the Upstart service? Is Upstart logging anything to /var/log/upstart/myapp.log?

  • I have same problem and i don't know how to resolve this issue. i have spent hours following the same tutorial. Is there a way around this?
    I have tried using mup but i get this error
    ``` x Installing Node.js: FAILED

        -----------------------------------STDERR-----------------------------------
        .......... .......... .......... .......... 91% 59.3M 0s
          5050K .......... .......... .......... .......... .......... 91% 54.8M 0s
          5100K .......... .......... .......... .......... .......... 92% 37.8M 0s
          5150K .......... .......... .......... .......... .......... 93% 61.2M 0s
          5200K .......... .......... .......... .......... .......... 94% 44.1M 0s
          5250K .......... .......... .......... .......... .......... 95% 54.8M 0s
          5300K .......... .......... .......... .......... .......... 96% 33.4M 0s
          5350K .......... .......... .......... .......... .......... 97% 63.7M 0s
          5400K .......... .......... .......... .......... .......... 98% 43.5M 0s
          5450K .......... .......... .......... .......... .......... 99% 57.4M 0s
          5500K .......... .......... .......... .......... ...       100% 42.6M=0.1s
    
        2016-05-24 10:25:59 (41.8 MB/s) - ‘node-v0.10.36-linux-x64.tar.gz.1’ saved [5676610/5676610]
    
        mv: cannot move ‘node-v0.10.36-linux-x64’ to ‘/opt/nodejs’: No such file or directory
        -----------------------------------STDOUT-----------------------------------
        js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/link.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/install.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/uninstall.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/cache.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/star.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/search.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/docs.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/config.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/owner.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/init.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/faq.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/lib/dedupe.js
        node-v0.10.36-linux-x64/lib/node_modules/npm/LICENSE
        node-v0.10.36-linux-x64/lib/node_modules/npm/AUTHORS
        node-v0.10.36-linux-x64/lib/node_modules/npm/.npmrc
        node-v0.10.36-linux-x64/lib/node_modules/npm/.npmignore
        node-v0.10.36-linux-x64/lib/node_modules/npm/cli.js
        node-v0.10.36-linux-x64/LICENSE
        ----------------------------------------------------------------------------
    
    In my /var/log/upstart/myapp.log the error is logged ```
    Meteor requires Node v0.10.41 or later.```
    
    Ho can I install this on Ubuntu 14.04 on Digital ocean? Thanks
    
  • The same here.
    How you guys solve the problem?
    any help is appreciated

1 Answer
Have another answer? Share your knowledge.