Adding an IP Firewall to NestJS Without Cloudflare

NestJS sits on top of Express or Fastify. Either way, the SecureNow preload adds an IP-reputation firewall in one line — no DNS changes.

May 9, 2026·5 min read

Adding an IP Firewall to NestJS Without Cloudflare

NestJS apps run on Node.js with an HTTP adapter — Express by default, Fastify if you've configured it. Either way, the SecureNow preload adds a managed IP firewall in one line, no DNS changes required.

For broader context see the SecureNow Firewall page.

The one-line setup

npm install securenow

# In your start command:
node -r securenow/firewall-only dist/main.js

In a Dockerfile:

CMD ["node", "-r", "securenow/firewall-only", "dist/main.js"]

In a process manager (PM2):

// ecosystem.config.cjs
module.exports = {
  apps: [{
    name: 'nestjs-app',
    script: 'dist/main.js',
    node_args: ['-r', require.resolve('securenow/firewall-only')],
  }],
};

The firewall sits at the HTTP server level — below NestJS, below Express/Fastify. Bad IPs get a 403 before any NestJS code runs.

What you get out of the box

  • A known-bad IP feed of up to 10,000 IPs (SecureNow AI IPDB feed)
  • Hourly refresh
  • Sub-millisecond per-request overhead
  • Fail-open on backend unreachability
  • 14-day free trial, then $99 per app per month

Hand-rolled NestJS Middleware (alternative)

If you want application-layer control:

// src/firewall/firewall.middleware.ts
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

const blocklist = new Set<string>();
// ... load from file or feed

@Injectable()
export class FirewallMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    const ip = (req.headers['x-forwarded-for'] as string)?.split(',')[0]?.trim()
      || req.socket.remoteAddress;
    if (ip && blocklist.has(ip)) {
      return res.status(403).send('Forbidden');
    }
    next();
  }
}

Apply globally:

// src/app.module.ts
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { FirewallMiddleware } from './firewall/firewall.middleware';

@Module({ /* ... */ })
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(FirewallMiddleware).forRoutes('*');
  }
}

The catch: maintaining a 50k-entry blocklist as code is impractical. Either pull from a feed periodically or use the SecureNow preload, which handles the feed for you.

Verifying

npx securenow firewall status

Returns the current blocklist size and last refresh time. To check a specific IP:

npx securenow firewall test-ip 185.220.101.42

Returns whether that IP would be blocked or allowed by the current ruleset.

Combining with NestJS Throttler

Run them together — different layers:

  • SecureNow firewall: blocks known-bad IPs (reputation-based)
  • NestJS Throttler: rate limits all IPs (rate-based)

Both run automatically; one is a preload, the other is a Module. No conflict.

When to use a CDN-level firewall instead

If you have:

  • Layer-3/4 attacks (SYN floods, UDP amplification)
  • Sophisticated bot traffic that mimics browsers perfectly
  • DDoS protection requirements (uptime-critical traffic)

Move the IP firewall to your CDN (Cloudflare, AWS Shield, Imperva). Application-layer firewall handles 80% of attacks but not all.

Frequently Asked Questions

Does NestJS need special configuration for the firewall?

No. NestJS runs on Node.js with an Express (default) or Fastify HTTP adapter. The SecureNow preload sits below both, so the firewall works without NestJS-specific setup.

What if I'm running NestJS in a Docker container?

Add the preload to your CMD or ENTRYPOINT. For example: `CMD ["node", "-r", "securenow/firewall-only", "dist/main.js"]`.

Will this work with NestJS microservices (gRPC, NATS)?

Yes for HTTP transports. For pure gRPC/NATS microservices, the IP-firewall layer doesn't apply — those don't have an HTTP server. Use the application-layer security observability instead.

Can I customize the blocklist?

Yes. Add per-app rules from the SecureNow dashboard, CLI (`securenow blocklist add <ip>`), or API. The known-bad base feed refreshes hourly; your custom additions sync within ~10 seconds.

Recommended reading

Block an IP, a Range or One Route Without a Deploy (Node.js)

Block one IP, a CIDR range, or a single route and method in a Node.js app within seconds, with no code change: setup, scoping, CLI, MCP, verification and undo.

Oct 27
11 Signs Your AI-Built App Needs a Security Audit

Built your app with a coding agent? 11 warning signs it needs a security audit, each checkable in five minutes, and how to run the full audit yourself.

Oct 26
AI Security Audit Part 1: Set Up Your Agent to Audit Code

Part 1 of the AI security audit series: set up an AI coding agent to audit your app locally, connect SecureNow, and run your first threat-model pass.

Jul 24