如何使用 iptables 和 parse-server (Parse Server) 将 HTTP 重定向到 HTTPs
Posted
技术标签:
【中文标题】如何使用 iptables 和 parse-server (Parse Server) 将 HTTP 重定向到 HTTPs【英文标题】:how to redirects HTTP to HTTPs using iptables and parse-server (Parse Server) 【发布时间】:2017-03-16 02:02:49 【问题描述】:我正在我的服务器上处理 SSL/https。
我使用 iptables 配置了两个端口,一个用于 HTTP (4000),另一个用于 HTTPS (9080):
$ sudo iptables -t natL -n --line-number
1 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 redir ports 9080
2 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 redir ports 4000
如何告诉 Parse Server 将所有来自 http 的流量重定向到使用 https?
我之前使用 expressJS 做了类似的配置,但它不适用于 Parse Server,这是我之前使用的代码:
app.use(function (req, res, next)
return res.redirect(['https://domainName.tv', req.url].join(''));
);
这是我的应用 index.js
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var path = require('path');
var bodyParser = require('body-parser');
var SimpleSendGridAdapter = require('parse-server-sendgrid-adapter');
var stripe = require("stripe");
var https = require('https');
var http = require('http');
var fs = require('fs');
var databaseUri = 'mongodb://localhost:27017/dev';
if (!databaseUri)
console.log('DATABASE_URI not specified, falling back to localhost.');
var options =
key: fs.readFileSync(__dirname + '/ssl/domainName.key'),
cert: fs.readFileSync(__dirname + '/ssl/domainName.crt')
;
var api = new ParseServer(
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
cloud: __dirname + '/cloud/main.js',
appId: 'xN6xxxxxxxxxxxxxxxxmx',
masterKey: 'egxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdn',
javascriptKey: 'O2xxxxxxxxxxxxxxxxxxxxxxxxxYy', //Add your master key here. Keep it secret!
serverURL: 'https://localhost:9080/parse',
restAPIKey: 'lxxxxxxxxxxxxxxxN',
appName: 'domainName.TV',
publicServerURL: 'https://domainName.tv/parse',
emailAdapter: SimpleSendGridAdapter(
apiKey: 'SG.SbxxxxxxxxxxxxxxxxxxxxxxxxxxxxMxc',
fromAddress: 'team@domainName.tv',
),
liveQuery:
classNames: ["UsPrIn", "Event"] // List of classes to support for query subscriptions
);
var app = express();
// redirect all http requests to https
// THIS IS NOT WORKING!!!!
app.use(function (req, res, next)
return res.redirect(['https://domainName.tv', req.url].join(''));
);
// Serve static assets from the /public folder
app.use('/public', express.static(path.join(__dirname, '/public')));
// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);
app.get('/*', function(req, res)
res.sendFile(path.join(__dirname, '/public/index.html'));
);
var port = process.env.PORT || 9080;
var httpsServer = https.createServer(options, app).listen(port, function()
console.log('parse-server running on SSL port ' + port + '.');
);
var httpServer = http.createServer(app).listen(4000, function()
console.log('parse-server running on port 4000.');
);
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpsServer);
我的解决方案与此类似,但我使用的是 Parse Server:Automatic HTTPS connection/redirect with node.js/express
【问题讨论】:
【参考方案1】:万一有人需要,我找到了答案:req.secure
这是代码:
app.use(function (req, res, next)
if(!req.secure && (req.headers.host !== 'localhost:7777'))
return res.redirect(['https://mydomain.tv', req.url].join(''));
next();
);
【讨论】:
以上是关于如何使用 iptables 和 parse-server (Parse Server) 将 HTTP 重定向到 HTTPs的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 iptables 和 parse-server (Parse Server) 将 HTTP 重定向到 HTTPs