How do you configure remote image domains in Next.js?

Add allowed sources to images.remotePatterns in next.config.ts, specifying protocol, hostname, and optionally port, pathname and search. Since Next.js 15.3 you can also pass new URL("https://cdn.example.com/**"). The older images.domains option is deprecated in Next.js 16.

Why it is required

The image optimizer fetches the remote URL on your server. Without an allowlist, anyone could use your server to process arbitrary images (cost and SSRF risk). Unlisted URLs return 400.

Pattern details

  • protocol: "https" (avoid allowing http).
  • hostname: exact (cdn.acme.com) or wildcard: *.acme.com matches one subdomain level, **.acme.com matches any depth.
  • pathname: /uploads/** limits the path; ** matches any depth, * one segment.
  • port: usually empty.
  • search: exact query string, if required.

Be as specific as possible: allowing ** on a shared host like s3.amazonaws.com lets anyone's bucket through.

Related Next.js 16 settings

  • images.localPatterns to allow local images with query strings.
  • images.dangerouslyAllowLocalIP only for private networks where images resolve to internal IPs.
  • images.maximumRedirects (default 3).

Alternatives

  • unoptimized on a single image skips the optimizer, so no pattern is needed.
  • A custom loader sends the image straight to an external CDN that does the resizing.

Code example

tsx
1// next.config.ts
2import type { NextConfig } from "next";
3
4const nextConfig: NextConfig = {
5 images: {
6 remotePatterns: [
7 // Shorthand (15.3+)
8 new URL("https://images.unsplash.com/**"),
9 // Object form with tight limits
10 {
11 protocol: "https",
12 hostname: "**.acme-cdn.com",
13 pathname: "/uploads/**",
14 },
15 {
16 protocol: "https",
17 hostname: "avatars.githubusercontent.com",
18 pathname: "/u/**",
19 },
20 ],
21 // domains: ["images.unsplash.com"], // deprecated in Next.js 16
22 },
23};
24
25export default nextConfig;

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app