Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict access to Node.js-based HTTP server by IP address

Tags:

node.js

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 .. 

I need to allow access to the site for only a few IP addresses. How can I do this?

like image 606
Vitalii Maslianok Avatar asked Sep 10 '12 09:09

Vitalii Maslianok


People also ask

How do you block IP address in Nodejs?

If you are using Express in your app, then you can use express-ipfilter or similar package in order to block access by ip. You can store the list of ips in memory or some kind of storage like Redis, and add ips dynamically (ex.


1 Answers

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

like image 78
Alex K Avatar answered Sep 23 '22 15:09

Alex K