我使用nginx作为http服务器,tomcat作为后端(使用proxy_pass)。它很好用,但是我想定义我自己的错误页面(404,500等等)。他们是由nginx而不是tomcat提供的。例如,我有以下资源:
https://domain.com/resource它根本不存在。如果我到达那个URL,那么我会从Tomcat那里得到一个未找到的消息,而不是从nginx那里得到一个消息。
我想要的是,每次Tomcat用404 (或任何其他错误消息)进行响应时,nginx都会给用户发送一条消息: nginx可以访问的一些html文件。
配置nginx服务器的方式非常简单,只是:
location / {
proxy_pass http://localhost:8080/<webapp-name>/;
}我已经配置了端口8080,它是tomcat,在这台机器之外是不可访问的。
我认为在nginx配置中使用不同的location指令是行不通的,因为有一些资源依赖于URL:
https://domain.com/customer/<non-existent-customer-name>/[GET]将始终返回404 (或任何其他错误消息),同时:
https://domain.com/customer/<existent-customer>/[GET]将返回与404 (客户存在)不同的任何内容。
是否有任何方式向Nginx (http服务器)提供Tomcat (应用服务器)错误消息?检查proxy_pass指令发送的消息并对其采取行动?
发布于 2010-12-27 20:26:56
好吧,我找到了。在我使用的nginx站点配置的server部分中:
error_page 404 /404.html;
location = /404.html {
root /var/www/nginx;
}
location / {
proxy_pass http://localhost:8080/api/;
proxy_intercept_errors on;
}需要将nginx的proxy_intercept_errors on添加到拦截他们中。Nginx只会拦截您用error_page定义的错误。
https://serverfault.com/questions/216512
复制相似问题