通过IP地址限制对基于Node.js的HTTP服务器的访问 [英] Restrict access to Node.js-based HTTP server by IP address
本文介绍了通过IP地址限制对基于Node.js的HTTP服务器的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Node.js HTTP服务器应用程序中通过IP地址限制访问?
How can I restrict access by IP address in a Node.js HTTP server application?
我正在寻找这样的东西:
I'm looking for something like this:
Deny from all
Allow from ..
我只需要允许访问该站点的几个IP地址.我该怎么办?
I need to allow access to the site for only a few IP addresses. How can I do this?
推荐答案
我不确定这种方法的防弹性能如何,但是这里是从网上答案中收集的:
I'm not sure how bulletproof is this approach, but here it is, collected from answers around the web:
var http = require('http');
http.createServer(function (req, res)
{
var ip = req.ip || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
if (ip == '127.0.0.1') // exit if it's a particular ip
res.end();
...
请,一个精通节点的人-纠正我
Please, someone more proficient in node - correct me
这篇关于通过IP地址限制对基于Node.js的HTTP服务器的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文