簡體   English   中英

如何使用 Nginx 在 MEAN 堆棧應用程序上正確設置 HTTPS/SSL

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

I am trying to accomplish having my URL endpoints (ex: https://www.busoutlaughing.com:443/api/nextshow ) to work after I have installed CertBot (SSL/HTTPS) on my MEAN stack web app using NGINX. 我正在使用 UBUNTU 18.04 服務器。

我可以訪問我的 URL www.busoutlaughing.com 並看到它在地址欄中使用 HTTPS://。

現在我希望能夠使用 HTTPS 訪問上述 API 端點,但必須在某處的配置中丟失某些內容。

我在嘗試訪問端點時收到的錯誤是 404:“加載資源失敗:服務器響應狀態為 404(未找到)”

這是我現在的代碼。如果我缺少任何信息,請告訴我,我認為所提供的就是理解我當前設置所需的全部內容。

感謝所有幫助!

來自 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 (角度服務文件)

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 服務器塊下 nginx/sites-available/

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM