Search code examples
nginxproxyhttp-status-code-404load-balancingappserver

nginx as load balancer server out 404 page based on HTTP response from app server


I'm using Nginx as a load balancer with app servers behind it. If the app servers return a response of 404 not found, I would like nginx to then server out a 404 page that is local on that server. I would like to do this so my app servers don't get tied up serving a static HTML file and can instead just quickly return a response and let nginx handle serving the static stuff. Does anyone know how I can accomplish this? Basically I need some kind of conditional check based on the HTTP response. Is this possible? Thanks!


Solution

  • Simply set the proxy_intercept_errors option to on and create an nginx error page for it.

    error_page 404 /404.html;
    
    proxy_intercept_errors on;
    

    To ensure that nginx will serve the static file from it’s document root you also have to specify the following:

    location /404.html {
        internal;
    }
    

    I'm assuming here that nginx is configured to talk with your app servers as proxy, because that is the usual way and your question does not mention any of this.