Search code examples
nginxsocket.iodigital-oceanparse-server

NGINX not opening ports for Parse Server and Socket.io


I have installed Parse Server with Socket.io on DigitalOcean.com on Ubuntu 16.04.4 x64

Everything works fine on HTTP

Then I decided to make a connection to the server via SSL protocol, So I been installed NGINX with Let's Encrypt certificates.

NGINX works fine, when I enter in browser mydomain.com I can see NGINX logo and https connection. In this case, everything works fine but there is no access on the port 1337 and to the socket on the port 3000.

Looks like NGINX don't know that he needs to let to the parse server and socket to open ports they needed.

I am iOS Developer and looks like I did something wrong

Any help will be appreciated !!!

Tutorials I used to install everything

Parse Server: https://www.digitalocean.com/community/tutorials/how-to-run-parse-server-on-ubuntu-14-04

Parse Server https://www.digitalocean.com/community/tutorials/how-to-migrate-a-parse-app-to-parse-server-on-ubuntu-14-04#step-3-%E2%80%93-install-and-configure-parse-server-and-pm2

NGINX and Certificate https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14-04

This is my NGINX configuration(example.com = mydomain.com)

server {

# SSL configuration

# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name example.com www.example.com;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
}

location /parse/ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:1337/;
    proxy_ssl_session_reuse off;
    proxy_set_header Host $http_host;
    proxy_redirect off;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#   include snippets/fastcgi-php.conf;
#
#   # With php7.0-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php7.0-fpm:
#   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#   deny all;
#}

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

This is my Parse Server init

var api = new ParseServer({
databaseURI: 'mongodb://localhost:27017/admin',
cloud: __dirname + '/cloud/main.js',
appId: 'appId',
masterKey: 'masterKey',
serverURL: 'https://example.com:1337/parse',
liveQuery: {
classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
  }
});

var port = 1337;
var parseServer = require('https').createServer(app);
parseServer.listen(port, function() {
     console.log('parse-server-example running on port ' + port + '.');
});

//Socket.io
const socketIO = require('socket.io');
const socketPort = 3000;
var socketServer = require('https').createServer(express());
socketServer.listen(socketPort, function(){
     console.log('listening on *:'+socketPort);
});

Solution

  • Well, finally I found the problem and the solution.

    I found Parse Dashboard issue case #429(Getting dashboard to run through https)

    And I understood that my Parse Server need to tell to NGINX that he need to run in SLL and provide certificates I am created with "Let's Encrypt".

    So here is the correct code:

        var fs = require('fs'); 
        var https = require('https'); 
        var express = require('express'); 
        var ParseServer = require('parse-server').ParseServer; 
        var ParseDashboard = require('parse-dashboard'); 
        var path = require('path'); 
        var app = express(); 
        var port = 1337; 
        var options = { key: fs.readFileSync(path.resolve(__dirname, '/etc/letsencrypt/live/sitedomain/privkey.pem')),
                       cert: fs.readFileSync(path.resolve(__dirname, '/etc/letsencrypt/live/sitedomain/fullchain.pem')), }; 
    
        var parse = new ParseServer({ databaseURI: 'mongodb://localhost:540556/admin', 
        cloud: __dirname + '/cloud/main.js', 
        appId: 'appId', 
        masterKey: 'masterKey', 
        serverURL: 'https://example.com', 
        liveQuery: { classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions } }); 
    
        var app = express(); 
        app.use('/public', express.static(path.join(__dirname, '/public'))); 
        app.use('/parse', parse); 
        app.get('/', function(req, res) { res.status(200).send('404'); });
        var server = https.createServer(options, app).listen(port, function() { 
        console.log("server listening on port " + port); 
        });
    

    And the same concept for the Socket.io

    var socket = null;
    
    function startSocketServer(){
        //Socket.io
      const express = require('express');
      const socketIO = require('socket.io');
      const https = require('https');
      const socketPort = 3000;
      const fs = require('fs');
      const path = require('path');
    
    
      var options = {
        key: fs.readFileSync(path.resolve(__dirname, '/etc/letsencrypt/live/sitedomain/privkey.pem')),
        cert: fs.readFileSync(path.resolve(__dirname, '/etc/letsencrypt/live/sitedomain/fullchain.pem')),
      };
    
      var socketServer = https.createServer(options, express()).listen(socketPort, function() {
        console.log("socketServer listening on port " + socketPort);
      });
    
      socket = socketIO(socketServer);
    
      socket.on('connection', (socket) => {
        console.log('Client connected');
        socket.on('disconnect', () => console.log('Client disconnected'));
      });
    
    }
    

    All NGINX configuration was correct

    I waste a lot of time and did't found any solution like this in the network.

    Hope it will help someone.

    Regards.