Fastcgi_pass / error page /error code in HTTP rsp

Hi, I would like nginx to map a fastcgi error response a static error
page,
and include the HTTP error code in its HTTP response header; e.g.

  1. have nginx return the proper error code in its header to the client.
  2. have nginx return the proper error page based on the fastcgi_pass
    server’s response error code.
    For example, if the fastcgi server returns ‘400 Bad Request’, I would
    like
    NGINX to return “Status code: 400” along with the bad request html
    static
    error page.

Is #2 feasible when fastcgi_pass is used? I was not able to do it,
unless I
used error code 302, a redirect. In other words, the only way I got
nginx to
return a specific error page was to have the fastcgi server respond with
a
redirect
“Status: 302 Found\r\n”
“Location: //badRequest.html\r\n”
The problem with this method was that the error code (400 for example)
did
not appear in the HTTP response of the error page (requirement #1 was
not
met).

How can I have nginx/fastcgi_pass return an error page with the HTTP
error
code (400 for example) appear in the HTTP header? My fastcgi server
response
did include ‘Status’ in the fastcgi response.
I tried not using the redirect 302 method but my attempts failed; I had
the
following inside the server block or inside the location/fastcgi_pass
block
of nginx.conf:

    error_page 400 = /bad_request.html;

    location = /bad_request.html {
       try_files /<path>/bad_request.html 50x.html;
    }

I tried the ‘internal’ directive also though I was not sure of its usage
as
the path of the html error page was not specified.
Any help on how to get nginx to return the error code in the HTTP header
response and an error page when fastcgi is used would be greatly
appreciated, thank you!

Posted at Nginx Forum:

Hello!

On Mon, Jun 08, 2015 at 03:14:00PM -0400, nginxuser100 wrote:

used error code 302, a redirect. In other words, the only way I got nginx to
did include ‘Status’ in the fastcgi response.
the path of the html error page was not specified.
Any help on how to get nginx to return the error code in the HTTP header
response and an error page when fastcgi is used would be greatly
appreciated, thank you!

http://nginx.org/r/fastcgi_intercept_errors


Maxim D.
http://nginx.org/

Thank you Maxim, that was what I was looking for. However, it is still
not
returning the static error page. Does nginx expect a certain response
format
from the fcgi server? I tried:
“HTTP/1.1 400 Bad Request\r\nStatus: 400 Bad Request\r\n”;
and
“HTTP/1.1 400 Bad Request”;

The nginx.conf has:
root …;
location xxx {
include fastcgi_params;
fastcgi_pass …;
error_page 400 /my_bad_request; ← inside or outside this
location block didn’t make a difference
fastcgi_intercept_errors on;
}

   location /my_bad_request {
        try_files /bad_request.html = 400;
    }

Thank you!

Posted at Nginx Forum:

Hi, I expected fastcgi_intercept_errors to return a static error page
AND to
have include the HTTP error code (e.g. 400) in the HTTP response header.
From what I see, it returns the static error page but with 200 OK.
Is it the expected behavior?
If yes, is there a way to have nginx return the error page and the error
code to the client?
Thank you!

Posted at Nginx Forum:

On Mon, Jun 08, 2015 at 08:13:03PM -0400, nginxuser100 wrote:

Hi there,

Hi, I expected fastcgi_intercept_errors to return a static error page AND to
have include the HTTP error code (e.g. 400) in the HTTP response header.

That’s what it does, unless you break it by doing unnecessary things in
your error_page directive.

From what I see, it returns the static error page but with 200 OK.

error_page 400 = /bad_request.html;

What do you think the “=” means?

What does the documentation say that the “=”
means? (Module ngx_http_core_module)

Can you think of a way of rephrasing the documentation so that it would
have been clearer to you, so that the next person will not have the
same problem?

What happens when you leave out the “=” and reload the config?

Is it the expected behavior?

It is what you have configured nginx to do, so yes.

If yes, is there a way to have nginx return the error page and the error
code to the client?

Configure it according to the “common” examples in the documentation,
not according to the special-case examples.

Good luck with it,

f

Francis D. [email protected]

I also tried
fastcgi_pass_header Status;
along with the fastcgi_intercept_errors directive. NGINX still returned
200
OK instead of the 400 sent by the fastcgi server.

Posted at Nginx Forum: