如何使用 Nginx 在 MEAN 堆栈应用程序上正确设置 HTTPS/SSL

How to properly set up HTTPS/SSL on MEAN stack application using Nginx

我正在尝试使用 NGINX 在我的 MEAN 堆栈 Web 应用程序上安装 CertBot (SSL/HTTPS) 后让我的 URL 端点(例如:https://www.busoutlaughing.com:443/api/nextshow)正常工作。我正在使用 UBUNTU 18.04 服务器。

我可以访问我的 URL www.busoutlaughing.com 并看到它在地址栏中使用 HTTPS://。

现在我希望能够使用 HTTPS 访问上面的 API 端点,但必须在某处配置中缺少某些内容。

我在尝试到达端点时收到的错误是 404: 'Failed to load resource: the server responded with a status of 404 (Not Found)'

这是我现在的代码。如果我遗漏了任何信息,请告诉我,我认为所提供的是了解我当前设置所需的全部内容。

感谢所有帮助!

来自app.js(节点服务器代码)

//importing modules
var express = require('express');
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var cors = require('cors');
var path = require('path');
const http = require('http');
var https = require('https')
const fs = require('fs');

var app = express();

const route = require('./routes/route');

...

//port number
const port = 80;

//adding midleware - cors
app.use(cors());

//body - parser
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());

//static files
app.use(express.static(path.join(__dirname, 'public')));

//routes
app.use('/api', route);

//testing server
app.get('/',(req, res)=>{
    res.send('foobar');
});

/*  -- Used to have this before doing HTTPS
app.listen(80,()=>{
    console.log('Server started at port: 80');
});
*/

// Certificate
const privateKey = fs.readFileSync('/etc/letsencrypt/live/busoutlaughing.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/busoutlaughing.com/cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/busoutlaughing.com/chain.pem', 'utf8');

const credentials = {
    key: privateKey,
    cert: certificate,
    ca: ca
};

const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);


httpServer.listen(80, () => {
    console.log('HTTP Server running on port 80');
});

httpsServer.listen(443, () => {
    console.log('HTTPS Server running on port 443');
});

来自 route.js(API 个端点)

...

//Find the next available show
router.get('/nextshow', (req, res, next)=>{

logic here..

    })
});

...

来自我的 performers.service.ts(angular 服务文件)

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Show } from './show';

@Injectable({
  providedIn: 'root'
})
export class PerformersService {

  constructor(private http: HttpClient) { }

  //get list of shows to sign up form
  getNextShow()
  {
    return this.http.get<Show>('https://www.busoutlaughing.com:443/api/nextshow');
  }
}

来自 nginx/sites-available/

下的我的 nginx 服务器块
server {

        root /var/www/busoutlaughing.com/html/busoutlaughing;
        index index.html index.htm index.nginx-debian.html;

        server_name busoutlaughing.com www.busoutlaughing.com;

        location / {
                try_files $uri $uri/ =404;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/busoutlaughing.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/busoutlaughing.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


}
server {
    if ($host = www.busoutlaughing.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = busoutlaughing.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name busoutlaughing.com www.busoutlaughing.com;
    return 404; # managed by Certbot




}

感谢您的帮助

最终的答案是我需要更改 HTTP 和 HTTPS 服务器的端口(在 NodeJS 代码中)。 Nginx 已经使用了 80,HTTP 和 HTTPS 分别使用了 443。我将 HTTP 的端口改回 3000,将 HTTPS(后端调用,我试图实现的)改回 4430,然后一切又恢复正常了。

httpServer.listen(3000, () => {
    console.log('HTTP Server running on port 3000');
});

httpsServer.listen(4430, () => {
    console.log('HTTPS Server running on port 4430');
});