HTTP
421 - Misdirected Request
Getting a 421 Misdirected Request means the request reached a server that can't handle it for the given scheme and authority—HTTP/2 requests hitting a server configured for different virtual hosts, or requests sent to the wrong server instance. This client-side error (4xx) happens when servers reject requests they're not configured to handle. Most common in HTTP/2 environments where requests are misrouted, but also appears when virtual host configurations don't match, load balancers route incorrectly, or DNS points to wrong servers.
#Common Causes
- →Frontend: Request sent to wrong server instance. HTTP/2 connection reused for wrong authority. Client connects to misconfigured server. DNS resolution points to wrong server.
- →Backend: Server not configured for request's scheme/authority. Virtual host mismatch. HTTP/2 server can't handle authority. Server configuration error.
- →Infrastructure: Load balancer routes to wrong backend. HTTP/2 connection reuse issues. Virtual host misconfiguration. DNS points to incorrect server.
✓Solutions
- 1Step 1: Diagnose - Check DevTools Network tab—verify request URL and host. Review if HTTP/2 connection is reused incorrectly. Check DNS resolution. Verify server hostname.
- 2Step 2: Diagnose - Server logs show misdirected requests. Review virtual host configuration. Check load balancer routing rules. Verify HTTP/2 connection handling.
- 3Step 3: Fix - Client-side: Use correct server endpoint. Close and reopen HTTP/2 connections if needed. Verify DNS resolution. Update API endpoint configuration.
- 4Step 4: Fix - Server-side: Configure virtual hosts correctly. Handle HTTP/2 authority properly. Return 421 with correct server information. Update server configuration.
- 5Step 5: Fix - Infrastructure: Fix load balancer routing. Configure HTTP/2 connection handling. Update DNS records. Review virtual host settings.
</>Code Examples
Fetch API: Handle 421 Misdirected Request
1// Client-side: Handle 421 by retrying with correct endpoint
2async function fetchWithRetry(url) {
3 let response = await fetch(url);
4
5 if (response.status === 421) {
6 console.warn('Request misdirected - server cannot handle this authority');
7
8 // Extract alternative URL from response headers
9 const altUrl = response.headers.get('Location') ||
10 response.headers.get('X-Alternative-Url') ||
11 response.headers.get('X-Correct-Server');
12
13 if (altUrl) {
14 console.log(`Retrying with correct server: ${altUrl}`);
15 response = await fetch(altUrl);
16 } else {
17 // Try with different scheme or reconstruct URL
18 const urlObj = new URL(url);
19
20 // Try HTTPS if HTTP was used
21 if (urlObj.protocol === 'http:') {
22 urlObj.protocol = 'https:';
23 response = await fetch(urlObj.toString());
24 } else {
25 // Try different hostname (if known)
26 throw new Error('Request misdirected and no alternative server provided');
27 }
28 }
29 }
30
31 return response.json();
32}
33
34// For HTTP/2, close connection and retry
35async function fetchWithHTTP2Handling(url) {
36 try {
37 const response = await fetch(url);
38
39 if (response.status === 421) {
40 // HTTP/2 connection issue - may need to close connection
41 console.warn('HTTP/2 connection misdirected');
42 // Browser will handle connection reuse, but log for debugging
43 }
44
45 return response.json();
46 } catch (error) {
47 console.error('Request failed:', error);
48 throw error;
49 }
50}Express.js: Handle Misdirected Requests
1// Server-side: Return 421 for misdirected requests
2const express = require('express');
3const app = express();
4
5// Virtual host configuration
6const virtualHosts = {
7 'api.example.com': app,
8 'api-staging.example.com': require('./staging-app'),
9};
10
11// Middleware to check virtual host
12app.use((req, res, next) => {
13 const host = req.get('host');
14 const expectedHost = process.env.API_HOST || 'api.example.com';
15
16 // Check if request is for this server
17 if (host !== expectedHost && !host.includes('localhost')) {
18 return res.status(421)
19 .set('X-Correct-Server', `https://${expectedHost}${req.originalUrl}`)
20 .json({
21 error: 'Misdirected Request',
22 message: `This server cannot handle requests for ${host}`,
23 correctServer: expectedHost,
24 requestedHost: host,
25 });
26 }
27
28 next();
29});
30
31// HTTP/2 authority check (if using HTTP/2)
32app.use((req, res, next) => {
33 const authority = req.headers[':authority'] || req.get('host');
34 const scheme = req.protocol;
35
36 // Check if server is configured for this authority/scheme combination
37 if (!isConfiguredForAuthority(authority, scheme)) {
38 return res.status(421).json({
39 error: 'Misdirected Request',
40 message: 'Server not configured for this authority',
41 authority: authority,
42 scheme: scheme,
43 });
44 }
45
46 next();
47});
48
49function isConfiguredForAuthority(authority, scheme) {
50 // Check server configuration
51 const configuredAuthorities = ['api.example.com', 'api-staging.example.com'];
52 return configuredAuthorities.includes(authority);
53}Nginx: Virtual Host Configuration
1# Nginx: Configure virtual hosts to prevent 421
2server {
3 listen 443 ssl http2;
4 server_name api.example.com;
5
6 # SSL configuration
7 ssl_certificate /etc/ssl/certs/api.example.com.crt;
8 ssl_certificate_key /etc/ssl/private/api.example.com.key;
9
10 # Handle requests for this virtual host
11 location / {
12 proxy_pass http://backend;
13 proxy_set_header Host $host;
14 proxy_set_header X-Real-IP $remote_addr;
15 }
16}
17
18# Catch-all server for misdirected requests
19server {
20 listen 443 ssl http2 default_server;
21 server_name _;
22
23 # Return 421 for requests to wrong virtual host
24 return 421 '{"error":"Misdirected Request","message":"This server cannot handle requests for this hostname"}';
25 default_type application/json;
26}
27
28# Or redirect to correct server
29# server {
30# listen 443 ssl http2;
31# server_name wrong-host.example.com;
32# return 301 https://api.example.com$request_uri;
33# }↗Related Errors
Provider Information
This error code is specific to HTTP services. For more information, refer to the official HTTP documentation.