NGINX 没有为 Parse Server 和 Socket.io 打开端口

NGINX not opening ports for Parse Server and Socket.io

我在 Ubuntu 16.04.4 x64

DigitalOcean.com 上安装了 Socket.io Parse Server

在 HTTP 上一切正常

然后我决定通过 SSL 协议连接到服务器,所以我安装了带有 Let's Encrypt 证书的 NGINX。

NGINX 工作正常,当我在浏览器中输入时 mydomain.com 我可以看到 NGINX 徽标和 https 连接。在这种情况下,一切正常,但无法访问端口 1337 和端口 3000 上的套接字。

看起来 NGINX 不知道他需要让解析服务器和套接字打开他们需要的端口。

我是 iOS 开发人员,看起来我做错了什么

任何帮助将不胜感激!!!

我用来安装所有东西的教程

解析服务器: https://www.digitalocean.com/community/tutorials/how-to-run-parse-server-on-ubuntu-14-04

解析服务器 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 和证书 https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14-04

这是我的 NGINX 配置(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

}

这是我的解析服务器初始化

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);
});

嗯,终于找到问题和解决办法了。

我找到了 Parse Dashboard 问题案例 #429(Getting dashboard to run through https)

而且我知道我的 Parse Server 需要告诉 NGINX 他需要 运行 在 SLL 中并提供我用 "Let's Encrypt" 创建的证书。

所以这是正确的代码:

    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); 
    });

与 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'));
  });

}

所有 NGINX 配置都是正确的

我浪费了很多时间,在网络上没有找到类似的解决方案。

希望对大家有所帮助。

此致。