Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have nginx as a http server with tomcat as a backend (using proxy_pass). It works great but I want to define my own error pages (404, 500, etc.) and that they are served by nginx and not tomcat. For example I have the following resource:

https://domain.com/resource

which doesn't exist. If I [GET] that URL then I get a Not Found message from Tomcat and not from nginx.

What I want is that every time Tomcat responds with a 404 (or any other error message) nginx sends itself a message to the user: some html file accessible by nginx.

The way I have my nginx server configured is very easy, just:

location / {
    proxy_pass   http://localhost:8080/<webapp-name>/;
}

And I've configured port 8080, which is tomcat, as not accessible from outside this machine.

I don't think that using different location directives in nginx configuration will work, because there are some resources that depend on the URL:

https://domain.com/customer/<non-existent-customer-name>/[GET]

Will always return 404 (or any other error message), while:

https://domain.com/customer/<existent-customer>/[GET]

Will return anything different from 404 (the customer exists).

Is there any way of serving Tomcat (Application Server) error messages with Nginx (http Server)? To check the message sent by the proxy_pass directive and act upon it?

share|improve this question

migrated from webmasters.stackexchange.com Dec 27 '10 at 19:26

This question came from our site for pro webmasters.

1 Answer 1

up vote 0 down vote accepted

Ok, I found it. In the server section of the nginx site configuration I'm using:

error_page 404 /404.html;
location = /404.html {
    root /var/www/nginx;
}

location / {
    proxy_pass http://localhost:8080/api/;
    proxy_intercept_errors on;
}

You need to add proxy_intercept_errors on for nginx to intercept them. Nginx will only intercept errors you defined with error_page.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.