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

无人久伴 提交于 2020-01-25 04:55:11

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/17244250/nginx-as-load-balancer-server-out-404-page-based-on-http-response-from-app-serve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!