Vinay Aggarwal
Vinay Aggarwal

Reputation: 1585

Nginx - How do I proxy all 404 requests to a different server

I'm trying to proxy all my 404 requests to a fallback server, also for 404s returned by php script of host server.

recursive_error_pages off;
error_page 404 = @missing;

location @missing {
    proxy_pass http://anotherserver;
    proxy_read_timeout 60s;
}

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ /index.php$is_args$args;
}

So whenever php application returns 404 it's gracefully falling back to anotherserver, but the problem is it's not able to pass URIs since it's inside a named block.

How do I configure nginx to proxy to anotherserver only when host server's application returns 404 ?

Upvotes: 2

Views: 1434

Answers (1)

Cemal
Cemal

Reputation: 1579

You can pass another server in location block using nginx upstream:

recursive_error_pages off;
error_page 404 = @missing;

upstream anotherserver404 {
    server anotherserver.only404.com:80;
}

location @missing {
    proxy_pass http://anotherserver404;
    proxy_read_timeout 60s;
}

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ /index.php$is_args$args;
}

Upvotes: 1

Related Questions