基于来自应用程序服务器的HTTP响应,nginx作为负载均衡器服务器输出404页面 [英] nginx as load balancer server out 404 page based on HTTP response from app server

查看:284
本文介绍了基于来自应用程序服务器的HTTP响应,nginx作为负载均衡器服务器输出404页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Nginx 作为负载均衡器,并在其后放置应用程序服务器.如果应用程序服务器返回未找到404的响应,我希望nginx然后将该服务器上本地的404页面服务器出去.我想这样做,这样我的应用程序服务器就不会为静态HTML文件提供服务,而是可以快速返回响应并让nginx处理为静态内容提供服务.有谁知道我该怎么做?基本上,我需要基于HTTP响应的某种条件检查.这可能吗?谢谢!

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!

推荐答案

只需设置 proxy_intercept_errors 选项打开,并为其创建一个nginx错误页面.

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;

要确保nginx从其文档根目录提供静态文件,您还必须指定以下内容:

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;
}

我在这里假设将nginx配置为与您的应用服务器作为代理进行通信,因为这是通常的方式,您的问题中没有提及任何这些.

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.

这篇关于基于来自应用程序服务器的HTTP响应,nginx作为负载均衡器服务器输出404页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆