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!
Asked
Active
Viewed 2,070 times
1

srchulo
- 5,143
- 4
- 43
- 72
1 Answers
2
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.

srchulo
- 5,143
- 4
- 43
- 72

Fleshgrinder
- 15,703
- 4
- 47
- 56
-
I am using nginx as a proxy server with my app servers. However, when I make the change that you suggest, if it receives a 404 from the app server, it will request 404.html from the app server instead of serving it locally. Then since my app server does not have 404.html, the second time around it will just server the generic nginx 404 page. Do you know how I could serve the 404 document locally from the server where nginx is being used as a proxy? – srchulo Jun 24 '13 at 22:57
-
1This answer in combination with yours lead me to the correct answer: http://serverfault.com/questions/426347/nginx-reverse-proxy-error-page – srchulo Jun 24 '13 at 23:05