我有一个正在运行的Node应用程序,它可以在http和https上正常工作。
我安装了Nginx,它也运行良好,使用ssh隧道进行了测试,它从静态文件(如MyPath/index.html)获得了正确的响应。
然而,我正试图让Nginx成为Node的反向代理。因为我想在我的机器上创建另一个应用程序,而Nginx应该对每个应用程序的传入请求进行排序。
但Nginx似乎有个问题,我想不出来。我怀疑这是一个配置问题。当我试图通过localhost上的SSH隧道访问Node应用程序时,我总是从浏览器中得到一个错误页面,而不是获取一个网页,它说:
此站点无法提供发送无效响应的安全连接本地主机。ERR_SSL_PROTOCOL_ERROR。
当我试图从广域网找到它的时候,它只是超时了。
Nginx配置
server {
listen [::]:4444 default_server;
server_name localhost mysite.com www.mysite.com;
access_log /home/mysite/access-log;
location / {
proxy_pass http://127.0.0.1:5555;
}
} 我试着将http://127.0.0.1:5555更改为https://127.0.0.1:6666,但这并没有改变任何事情。
节点应用程序
const port = 5555;
const secureport = 6666; ..。
const privateKey = fs.readFileSync('PATHTOCERT');
const certificate = fs.readFileSync('PATHTOKEY');
const credentials = {key: privateKey, cert: certificate}; ..。我在这里使用一个快速应用实例,也配置了带有头盔的CSP。但我不认为这是问题所在,因为我没有戴头盔,这也解决不了任何问题。..。
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(port);
httpsServer.listen(secureport);发布于 2021-03-08 22:13:35
我解决了我的问题。存在一个问题,即SSL证书从未有效,因为域名"localhost“或ip地址(MyIP)显然与证书记录不匹配。我不得不把它放在我的“实时服务器”上,以检查它是否有效(信念的一小飞跃)。:-)
我现在让它在Nginx中使用下面的配置。
/etc/nginx/nginx.conf
#user www-data;
user $nginxuser $nginxusergroup;
worker_processes auto;
#pid run/nginx.pid
include /etc/nginx/modules-enabled/*.conf;为了提高安全性,我更改了Nginx的辅助进程的用户。我本来希望完全使用没有根权限的用户来运行Nginx,但这导致了几个错误。稍后我会调查的。在更改用户之后,我还不得不注释掉pid指令,因为它带来了问题。除了日志位置之外,主配置文件的其余部分未被更改。
HTTPS:
/etc/nginx/sites available/mysite-ssl
(使用从站点到此文件的符号链接-启用)
server {
# Ports
listen 4444 ssl http2 default_server;
listen [::]:4444 ssl http2 default_server;
# Server name
server_name localhost example.com www.example.com;
# SSL
ssl_certificate /mypath/fullchain.pem;
ssl_certificate_key /mypath/privkey.pem;
# Logs
access_log /mypath/access.log;
error_log /mypath/ssl-error.log;
location / {
proxy_pass https://127.0.0.1:6666;
proxy_redirect https://127.0.0.1:6666 https://www.example.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
}
}对于HTTP:
/etc/nginx/站点-现有/mysite
(使用从站点到此文件的符号链接-启用)
server {
# Ports
listen 3333 default_server;
listen [::]:3333 default_server;
# Server name
server_name localhost example.com www.example.com;
# Logs
access_log /mypath/access.log;
error_log /mypath/error.log;
location / {
proxy_pass http://127.0.0.1:5555;
proxy_redirect http://127.0.0.1:5555 http://www.example.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
}
}我的节点应用程序具有以下配置(不变):
const port = 5555;
const secureport = 6666; ..。
const privateKey = fs.readFileSync('PATHTOCERT');
const certificate = fs.readFileSync('PATHTOKEY');
const credentials = {key: privateKey, cert: certificate};..。
此部分将http重定向到节点内的https:
app.use(function (req, res, next) {
if(!req.secure) {
return res.redirect(301, ['https://', req.get('Host'), req.url].join(''));
}
next();
});..。一款快速应用程序.
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(port);
httpsServer.listen(secureport);在重新启动节点和nginx服务之后,此设置在https和http上为我工作。
https://serverfault.com/questions/1055349
复制相似问题