How to Simulate 502 and 504 Errors in Nginx for Testing Custom Error Pages


10 views

html

When developing custom error pages for Nginx, it's crucial to test how they render under real-world conditions. Simulating 502 (Bad Gateway) and 504 (Gateway Timeout) errors helps ensure your custom error pages display correctly across different virtual hosts.

return Directive /h2>

The simplest way to force these errors is by adding temporary rules to your Nginx configuration:


# For 502 error simulation
location /test502 {
    return 502;
}

# For 504 error simulation
location /test504 {
    return 504;
}

After adding these, restart Nginx and visit yourdomain.com/test502 or yourdomain.com/test504 to see your custom error pages.

For more realistic testing of proxy-related errors:


# 502 Simulation (bad backend)
location /simulate502 {
    proxy_pass http://localhost:9999; # Non-existent port
    proxy_intercept_errors on;
}

# 504 Simulation (timeout)
location /simulate504 {
    proxy_pass http://slow_backend;
    proxy_connect_timeout 1s;
    proxy_read_timeout 1s;
}

To test across multiple vhosts, create separate test locations in each server block:


server {
    server_name site1.example.com;
    error_page 502 /custom_502.html;
    
    location /test502 {
        return 502;
    }
}

server {
    server_name site2.example.com;
    error_page 502 /different_502.html;
    
    location /test502 {
        return 502;
    }
}

For CI/CD pipelines, you can verify error pages programmatically:


# Check 502 page content
curl -I http://site1.example.com/test502

# Verify custom 504 page
curl -v http://site2.example.com/test504
  • Always remove test routes before deploying to production
  • Test both the HTTP status code and page content
  • Verify mobile responsiveness of error pages
  • Check for proper caching headers on error pages

Testing custom error pages for 502 and 504 status codes is crucial when you're running production servers. Unlike standard HTTP errors that can be easily triggered (like 404 by requesting a non-existent URL), gateway errors require more sophisticated simulation techniques since they originate from backend communication failures.

The most realistic way is to create temporary proxy configurations that force these errors:


# For 502 Simulation
location /test502 {
    proxy_pass http://127.0.0.1:9999; # Non-existent port
    proxy_next_upstream off;
}

# For 504 Simulation
location /test504 {
    proxy_pass http://backend_that_doesnt_respond;
    proxy_read_timeout 1s; # Force timeout
    proxy_connect_timeout 1s;
}

For simpler cases where you just need to test the error page rendering:


# Direct return method
location /simulate502 {
    return 502;
}

location /simulate504 {
    return 504;
}

After setting up the simulation routes, configure your custom error pages:


error_page 502 /custom_502.html;
error_page 504 /custom_504.html;

# Important for proper testing
location = /custom_502.html {
    internal;
    root /path/to/error/pages;
}

location = /custom_504.html {
    internal;
    root /path/to/error/pages;
}

Test your setup with curl commands:


# Test 502
curl -I http://yourserver.com/test502

# Test 504 (might take a few seconds)
curl -I http://yourserver.com/test504

For developers needing frequent testing, consider creating a simple Lua script:


location /dynamic_error {
    content_by_lua_block {
        local status = ngx.var.arg_status
        if status == "502" then
            ngx.status = 502
        elseif status == "504" then
            ngx.status = 504
        end
        ngx.say("Simulated error: "..status)
    }
}

Then test with: http://yourserver.com/dynamic_error?status=502