【发布时间】:2019-10-24 10:58:34
【问题描述】:
在我使用 NGINX 在我的 MEAN 堆栈 Web 应用程序上安装 CertBot (SSL/HTTPS) 后,我试图让我的 URL 端点(例如:https://www.busoutlaughing.com:443/api/nextshow)工作。我使用的是 UBUNTU 18.04 服务器。
我可以访问我的网址 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/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
}
感谢您的帮助
【问题讨论】:
标签: node.js express ssl https mean-stack