让 Apache 允许通过 HTTPS 访问 Express API
Getting Apache to allow access to Express API over HTTPS
我在 Apache 上的网站 (https://www.tjbrackett.com/contact) 无法通过 HTTPS 访问我在同一服务器上的 Express 应用程序。在我向站点添加 SSL 证书之前,设置 运行 非常完美。当我还原 SSL 证书时,它又可以工作了。我在前端收到的错误是 ERR_CERT_AUTHORITY_INVALID.
我试过设置 proxy/reverse 代理。我不确定我是否正确设置了它们。我已经在 Express 应用程序上完成了自签名 SSL 证书。我已尝试在 HTTPS 域之上提供 Express 应用程序。
HTTPS 阿帕奇 mysite.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.tjbrackett.com
ServerAdmin tj@brackett.dev
ServerAlias tjbrackett.com
DirectoryIndex index.html
DocumentRoot /var/www/tjbrackett.com
<Directory /var/www/tjbrackett.com>
order allow,deny
allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.tjbrackett.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.tjbrackett.com/privkey.pem
ProxyRequests On
ProxyPass /contact https://www.tjbrackett.com:8443/
ProxyPassReverse /contact https://www.tjbrackett.com:8443/
</VirtualHost>
快递应用
const nodemailer = require('nodemailer');
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const http = require('http');
const https = require('https');
const app = express();
const options = {
key: fs.readFileSync(__dirname + '/key.pem'),
cert: fs.readFileSync(__dirname + '/cert.pem')
}
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
app.post('/', (req, res) => {
let name = req.body.name;
let email = req.body.email;
let subject = req.body.subject;
let message = req.body.message;
let mailOptions = "";
console.log(req.body);
console.log(req.hostname);
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: true,
auth: {
user: 'myEmail@bot.com',
pass: 'jsfoffamlhqzfqnu'
},
tls: {
rejectUnauthorized: false
}
});
if (req.hostname === "www.tjbrackett.com"){
mailOptions = {
from: email,
to: 'myEmail@gmail.com',
subject: subject,
text: message + "\nName: " + name + "\nEmail: " + email,
};
} else {
mailOptions = {
from: email,
to: 'anotherEmail@gmail.com',
subject: subject,
text: message + "\nName: " + name + "\nEmail: " + email,
}
}
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.send(req.body);
})
http.createServer(app).listen(8888, () => {
console.log("Server started on port 8888");
});
https.createServer(options, app).listen(8443, () => {
console.log("Server started on port 8443");
});
反应获取
fetch("https://www.tjbrackett.com:8443", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: this.state.name,
email: this.state.email,
message: this.state.message
})
我对 Apache/web 服务器还很陌生,所以目前我还没有足够的知识来研究这个问题。非常感谢任何建议。谢谢!
使用与我的 URL 关联的相同 SSL 证书允许我的网站访问 Express API。
新快递代码
const options = {
key: fs.readFileSync('/path/to/cert/info/privkey.pem'),
cert: fs.readFileSync('/path/to/cert/info/cert.pem'),
ca: fs.readFileSync('/path/to/cert/info/chain.pem')
}
我使用 Let's Encrypt/Certbot 作为 SSL。
我在 Apache 上的网站 (https://www.tjbrackett.com/contact) 无法通过 HTTPS 访问我在同一服务器上的 Express 应用程序。在我向站点添加 SSL 证书之前,设置 运行 非常完美。当我还原 SSL 证书时,它又可以工作了。我在前端收到的错误是 ERR_CERT_AUTHORITY_INVALID.
我试过设置 proxy/reverse 代理。我不确定我是否正确设置了它们。我已经在 Express 应用程序上完成了自签名 SSL 证书。我已尝试在 HTTPS 域之上提供 Express 应用程序。
HTTPS 阿帕奇 mysite.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.tjbrackett.com
ServerAdmin tj@brackett.dev
ServerAlias tjbrackett.com
DirectoryIndex index.html
DocumentRoot /var/www/tjbrackett.com
<Directory /var/www/tjbrackett.com>
order allow,deny
allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.tjbrackett.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.tjbrackett.com/privkey.pem
ProxyRequests On
ProxyPass /contact https://www.tjbrackett.com:8443/
ProxyPassReverse /contact https://www.tjbrackett.com:8443/
</VirtualHost>
快递应用
const nodemailer = require('nodemailer');
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const http = require('http');
const https = require('https');
const app = express();
const options = {
key: fs.readFileSync(__dirname + '/key.pem'),
cert: fs.readFileSync(__dirname + '/cert.pem')
}
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
app.post('/', (req, res) => {
let name = req.body.name;
let email = req.body.email;
let subject = req.body.subject;
let message = req.body.message;
let mailOptions = "";
console.log(req.body);
console.log(req.hostname);
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: true,
auth: {
user: 'myEmail@bot.com',
pass: 'jsfoffamlhqzfqnu'
},
tls: {
rejectUnauthorized: false
}
});
if (req.hostname === "www.tjbrackett.com"){
mailOptions = {
from: email,
to: 'myEmail@gmail.com',
subject: subject,
text: message + "\nName: " + name + "\nEmail: " + email,
};
} else {
mailOptions = {
from: email,
to: 'anotherEmail@gmail.com',
subject: subject,
text: message + "\nName: " + name + "\nEmail: " + email,
}
}
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.send(req.body);
})
http.createServer(app).listen(8888, () => {
console.log("Server started on port 8888");
});
https.createServer(options, app).listen(8443, () => {
console.log("Server started on port 8443");
});
反应获取
fetch("https://www.tjbrackett.com:8443", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: this.state.name,
email: this.state.email,
message: this.state.message
})
我对 Apache/web 服务器还很陌生,所以目前我还没有足够的知识来研究这个问题。非常感谢任何建议。谢谢!
使用与我的 URL 关联的相同 SSL 证书允许我的网站访问 Express API。
新快递代码
const options = {
key: fs.readFileSync('/path/to/cert/info/privkey.pem'),
cert: fs.readFileSync('/path/to/cert/info/cert.pem'),
ca: fs.readFileSync('/path/to/cert/info/chain.pem')
}
我使用 Let's Encrypt/Certbot 作为 SSL。