我是这方面的新手,有几个问题无法通过 Google 回答。
如果我做对了 - nginx 是我的网络服务器 - 对我的服务器的请求已完成并且 nginx 服务于我的客户端(Angular)但我也可以使用我的 Express应用程序使用 res.sendFile()
服务它?
如果我使用反向代理设置 nginx,它会作为我的客户端和后端之间的一个实例吗?所以他们不直接相互通信,而是通过 nginx 进行通信,为此我必须 proxy_pass
我的 Express 应用程序?
我的 nginx 站点可用配置:
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name blank-agency.org www.blank-agency.org;
return 301 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl on;
ssl_certificate /etc/letsencrypt/live/blank-agency.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/blank-agency.org/privkey.pem;
server_name blank-agency.org www.blank-agency.org;
root /var/www/webserver/public/website;
location ~ /.well-known {
allow all;
}
location / {
proxy_pass https://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
如你所见,我的目录结构是:
/var/www/webserver --> 这是我的 express app.js
/var/www/webserver/public/website --> 这是我的 Angular dist 文件
如果我使用 nginx 作为反向代理,我的客户端将不再提供服务(错误的网关 502
),所以我想我现在必须通过我的 Express 应用程序提供服务吗?
现在我的 app.js
:
// require section
var express = require('express'),
mysql = require('mysql'),
bodyParser = require('body-parser'),
cors = require('cors'),
path = require('path'),
app = express();
// middlewares bodyParser + cors
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
var connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'whatever',
database: 'whateverDB'
});
connection.connect();
app.use( express.static(__dirname + '/public/website'));
app.get('/', function(req, res){
res.sendFile(path.join(__dirname + '/public/website/index.html'));
});
app.route('/customers').get((req, res) => {
connection.query('SELECT * FROM customers', function (err, rows) {
const results = JSON.stringify(rows);
console.log(req.param("term"));
res.send(results);
})
})
// app listen port
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
同样,一切似乎都运行良好...但为什么我可以通过 http://blank-agency.org:3000
访问我的应用程序?首先应该将 HTTP 请求重定向到 HTTPS?同样适用于 https://blank-agency/customers --> 我在 URL 上暴露的数据似乎是一个安全问题哈哈
我知道它是在端口 3000
上提供的,因为我是通过我的 Express 应用程序发送它的,该应用程序在端口 3000 上监听,但它还应该如何提供?因为带有反向代理设置的 nginx 给出了如上所述的 502
错误,我已经通过 linux 上的 iptables
在我的防火墙上打开了端口 80 & 443
。
netstat -tlpn
在端口 80 和 443 上显示 nginx
systemctl status nginx
和
nginx -t
说一切都好。
我想我对这里使用的概念有很深的误解,或者其他地方不对。 如果您需要更多信息,请告诉我。
提前致谢:)
最佳答案
第一部分:
app.listen(3000, 'localhost', function () {
console.log('Example app listening on port 3000!');
});
这将阻止访问 http://blank-agency.org:3000
,因为现在带有 expressjs
的 node
不会监听在所有地址上,但仅限于 localhost
。 (假设 nginx 和 node/expressjs 都运行在同一台服务器上)。
第二部分:
location /customers {
proxy_pass https://localhost:3000/customers;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
这将仅将 https://blank-agency.org/customers
反向代理到 node/expressjs
。为了减少配置负担,最好将所有 expressjs
URL 收集在公共(public)路径下,如:/api/customers
、/api/products
、/api/auth
等。这样,您只需从 /api
反向代理配置一次。
如果你需要身份验证,你的angular
应用程序和node/expressjs
应该实现它,那么你将有更多的反向代理路径,比如location/api/auth { ... }
等。但是那部分是特定于应用程序的,nginx
只会将 HTTP 请求移交给 node/expressjs
,并将响应移交给客户端。从 nginx
的 Angular 来看,它应该是透明的。
关于node.js - Nginx 反向代理 + ExpressJS + Angular + SSL 配置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53807393/