Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – vhost ip filter express节点大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用vhost with express来管理两个子域.一切正常,但我想通过ip过滤其中一个子域的请求.你知道这是否可行?

我试图在我的子域网站的app.js中管理它,但是req.connection.remoteAddress和req.ip给了我服务器的ip.

当我只有一个子域并且不使用vhost时我有正确的ip,但是因为我使用了vhost我已经得到了我服务器的ip …

这是我之前的文件夹结构:

-- subdomain1/
    -- app.js
    -- views/

这是我的新结构:

-- subdomain1/
    -- app.js
    -- views/
-- subdomain2/
    -- app.js
    -- views/
-- manageSubdomain/
    -- app.js

这是我的@L_618_4@,它在使用vhost之前工作,并且只用于一个子域:

subdomain1 / app.js:

var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
require('body-parser-xml')(bodyParser);

var routes = require('./routes/index');

var app = express();

// view ENGIne setup
app.set('views',path.join(__dirname,'views'));
app.set('view ENGIne','jade');

// uncomment after placing your favicon in /public
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname,'public')));

// Example middleware to get ip
app.use(function (req,res) {
    console.log(req.ip); // it give me the correct IP
});

app.use('/',routes);

module.exports = app;

以及之前管理服务器的文件

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('webservicePrestashop:server');
var https = require('https');
var fs = require('fs');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '443');
app.set('port',port);

/**
 * Create http server.
 */

//var server = http.createServer(app);
var options = {
     key: fs.readFileSync('/path/to/privkey.pem'),cert: fs.readFileSync('/path/to/fullchain.pem'),ca: fs.readFileSync('/path/to/chain.pem')
}
var server = https.createServer(options,app);

// Redirect from http port 80 to https
var http = require('http');
http.createServer(function (req,res) {
    res.writeHead(301,{ "LOCATIOn": "https://" + req.headers['host'] + req.url });
    res.end();
}).listen(80);

/**
 * Listen on provided port,on all network interfaces.
 */

server.listen(port);

/**
 * Event listener for http server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'String'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

这是我管理子域的@L_618_4@:

@H_247_5@manageSubdomain / app.js:

var express = require('express');
var vhost = require('vhost');
var http = require('http');
var https = require('https');
var fs = require('fs');
var tls = require('tls');

// Gestions des sites
const subdomain1 = {
    app: require('../subdomain1/app'),context: tls.createSecureContext({
        key: fs.readFileSync('path/to/privkey.pem').toString(),cert: fs.readFileSync('path/to/privkey.pem/fullchain.pem').toString(),ca: fs.readFileSync('path/to/privkey.pem/chain.pem').toString()
    }).context
};
const subdomain2 = {
    app: require('../subdomain2/app'),context: tls.createSecureContext({
        key: fs.readFileSync('path/to/privkey.pem/privkey.pem').toString(),ca: fs.readFileSync('path/to/privkey.pem/chain.pem').toString()
    }).context
};
var sites = {
    "my.subdomain1.com": subdomain1,"my.subdomain2.com": subdomain2
};

var exp = express();
for (let s in sites) {
  exp.use(vhost(s,sites[s].app));
}

// Redirect du http to https
http.createServer(function (req,{ "LOCATIOn": "https://" + req.headers['host'] + req.url });
    res.end();
}).listen(80);

var secureOpts = {
    SNICallBACk: function (domain,cb) {
        if (typeof sites[domain] === "undefined") {
            cb(new Error("domain not found"),null);
            console.log("Error: domain not found: " + domain);
        } else {
            cb(null,sites[domain].context);
        }
    },key: fs.readFileSync('path/to/privkey.pem/privkey.pem').toString(),cert: fs.readFileSync('path/to/privkey.pem/fullchain.pem').toString()
};

// Création du serveur https
var httpsServer = https.createServer(secureOpts,exp);
httpsServer.listen(443);

现在我的subdomain1 / app.js与以前相同

解决方法

你试过 req.ip财产吗?

快递文件说:

要调试@L_618_4@,请在将任何应用程序添加到express对象之前添加日志记录中间件:

var exp = express();

// ip logging middleware
app.use(function (req,res,next) {
    console.log(req.ip);
    next();
});

for (let s in sites) {
  exp.use(vhost(s,sites[s].app));
}

然后,添加与子应用程序的第一个中间件相同的中间件.通过这种方式,您可以确定问题是由vhost模块引起的.

大佬总结

以上是大佬教程为你收集整理的node.js – vhost ip filter express节点全部内容,希望文章能够帮你解决node.js – vhost ip filter express节点所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。