Javascript 如何在node js中将http重定向到https
我正在使用部署一个节点js服务器Javascript 如何在node js中将http重定向到https,javascript,node.js,node-modules,Javascript,Node.js,Node Modules,我正在使用部署一个节点js服务器 npm run start 现在我想添加ssl私钥,所以我在下面添加了https\u server.js文件: var https = require('https'); var fs = require('fs'); var https_options = { key: fs.readFileSync("/home/user/org.key"), cert: fs.readFileSync('/home/user/cert.
npm run start
现在我想添加ssl私钥,所以我在下面添加了https\u server.js文件:
var https = require('https');
var fs = require('fs');
var https_options = {
key: fs.readFileSync("/home/user/org.key"),
cert: fs.readFileSync('/home/user/cert.pem')
};
https.createServer(https_options , function (req, res) {
res.writeHead(200);
res.end("Welcome to Node.js HTTPS Servern");
}).listen(3000,'0.0.0.0')
然后我就跑
node https_server.js
因此,当我运行此程序时,它会显示“Welcome to Node.js HTTPS Servern”,而不是web应用程序的主页。
我应该换这部分吗
res.end("Welcome to Node.js HTTPS Servern");
感谢您的帮助
编辑
我也试过了
var fs = require('fs');
var net = require('net');
var http = require('http');
var https = require('https');
var baseAddress = 3000;
var redirectAddress = 3001;
var httpsAddress = 3002;
var httpsOptions = {
key: fs.readFileSync('/home/user/org.key'),
cert: fs.readFileSync('/home/user/cert.pem')
};
net.createServer(tcpConnection).listen(baseAddress);
http.createServer(httpConnection).listen(redirectAddress);
https.createServer(httpsOptions, httpsConnection).listen(httpsAddress);
function tcpConnection(conn) {
conn.once('data', function (buf) {
// A TLS handshake record starts with byte 22.
var address = (buf[0] === 22) ? httpsAddress : redirectAddress;
var proxy = net.createConnection(address, function () {
proxy.write(buf);
conn.pipe(proxy).pipe(conn);
});
});
}
function httpConnection(req, res) {
var host = req.headers['host'];
res.writeHead(301, { "Location": "https://" + host + req.url });
res.end();
}
function httpsConnection(req, res) {
res.writeHead(200, { 'Content-Length': '5' });
res.end('HTTPS');
}
但这一条也指向“HTTPS”,而不是应用程序的主页下面是我如何部署node js应用程序和部署的 -在服务器上安装nginx -在服务器上安装pm2 -使用pm2启动节点js服务器。e、 g:localhost:3000 -更新nginx配置 o将所有http请求重定向到https o使用代理服务器将https请求转发到localhost:3000 nginx配置:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
ssl_certificate path_to_certificate;
ssl_certificate_key path_to_private_key;
root /usr/share/nginx/html;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_read_timeout 5m;
proxy_connect_timeout 5m;
proxy_redirect off;
}
...
如果你有任何问题,请告诉我 As@Jikun说可以使用nginx作为代理。通过nginx,您可以配置ssl证书,接收到80端口的所有请求,并将它们重定向到节点服务器或错误页面。
它允许您集中所有请求,并作为一个附加层,以避免直接请求服务器。Hi,您打算使用Node JS+Nginx实现SSL吗?您应该配置Nginx以启用SSL,而不是更新Node JS app。如果您想将HTTP重定向到HTTPS,您实际上必须侦听HTTP请求。@Brad这像我的第二种方法吗?我们可以不使用pm2吗?pm2用于Node JS生产。您想永远使用而不是pm2吗?我们可以只使用Nginx而不使用pm2还是永远使用?当然,但你想用什么来启动节点应用程序呢?