# Custom 502 error page is ignored

**URL:** <https://www.ruby-forum.com/t/custom-502-error-page-is-ignored/224765>\
**Category:** NGINX\
**Created:** [November 25, 2012, 3:53pm UTC](https://www.ruby-forum.com/t/custom-502-error-page-is-ignored/224765 "2012-11-25T15:53:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 25, 2012, 3:53pm UTC](https://www.ruby-forum.com/t/custom-502-error-page-is-ignored/224765/1 "2012-11-25T15:53:10Z")

</div>

Dear all,

I’m currently wondering why nginx ignores my custom 502 Bad Gateway page  
and  
instead serves the internal 502 error page.  
I’m using nginx version 1.2.4 and php-fpm 5.4.8

The behaivior occures every time and is reproducible:  
If I stop php-fpm and request an existing php file my custom 502 error  
page  
is shown, so everything is fine.  
But if I request a non existing php file nginx shows me the nginx  
internal  
502 error page.

I don’t know why my custom 502 error page is ignored, because it’s a  
static  
html file.  
Do you have any tipps for me?

Part from the debug log:  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http script var: “/index2.php”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 trying to use file:  
“/index2.php”  
“/usr/share/nginx/html/index2.php”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 trying to use file: “=404”  
“/usr/share/nginx/html=404”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http finalize request: 404,  
“/index2.php?” a:1, c:1  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http special response: 404,  
“/index2.php?”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 internal redirect: “/404.html?”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 rewrite phase: 0  
2012/11/25 15:29:09 [debug] 10777#0: \*26 test location: “/”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 test location: “502.html”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 test location: “404.html”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 using configuration  
“=/404.html”  
…  
http run request: “/404.html?”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http upstream check client,  
write  
event:1, “/404.html”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http upstream recv(): -1 (11:  
Resource temporarily unavailable)  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http upstream request:  
“/404.html?”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http upstream process header  
2012/11/25 15:29:09 [error] 10777#0: \*26 connect() failed (111:  
Connection  
refused) while connecting to upstream, client: 10.0.2.2, server:  
localhost,  
request: “GET /index2.php HTTP/1.1”, upstream:  
“fastcgi://127.0.0.1:9000”,  
host: “127.0.0.1”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http next upstream, 2  
2012/11/25 15:29:09 [debug] 10777#0: \*26 free rr peer 1 4  
2012/11/25 15:29:09 [debug] 10777#0: \*26 finalize http upstream request:  
502  
2012/11/25 15:29:09 [debug] 10777#0: \*26 finalize http fastcgi request  
2012/11/25 15:29:09 [debug] 10777#0: \*26 free rr peer 0 0  
2012/11/25 15:29:09 [debug] 10777#0: \*26 close http upstream connection:  
8  
2012/11/25 15:29:09 [debug] 10777#0: \*26 free: 0000000001F7C560, unused:  
48  
2012/11/25 15:29:09 [debug] 10777#0: \*26 event timer del: 8:  
1353853809503  
2012/11/25 15:29:09 [debug] 10777#0: \*26 reusable connection: 0  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http finalize request: 502,  
“/404.html?” a:1, c:1  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http special response: 502,  
“/404.html?”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http set discard body  
2012/11/25 15:29:09 [debug] 10777#0: \*26 HTTP/1.1 502 Bad Gateway  
Server: nginx/1.2.4  
Date: Sun, 25 Nov 2012 14:29:09 GMT  
Content-Type: text/html  
Content-Length: 172  
Connection: keep-alive

2012/11/25 15:29:09 [debug] 10777#0: \*26 write new buf t:1 f:0  
0000000001F385A8, pos 0000000001F385A8, size: 156 file: 0, size: 0  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http write filter: l:0 f:0  
s:156  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http output filter “/404.html?”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http copy filter: “/404.html?”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http postpone filter  
“/404.html?”  
0000000001F38778  
2012/11/25 15:29:09 [debug] 10777#0: \*26 write old buf t:1 f:0  
0000000001F385A8, pos 0000000001F385A8, size: 156 file: 0, size: 0  
2012/11/25 15:29:09 [debug] 10777#0: \*26 write new buf t:0 f:0  
0000000000000000, pos 000000000069B3E0, size: 120 file: 0, size: 0  
2012/11/25 15:29:09 [debug] 10777#0: \*26 write new buf t:0 f:0  
0000000000000000, pos 000000000069A1A0, size: 52 file: 0, size: 0  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http write filter: l:1 f:0  
s:328  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http write filter limit 0  
2012/11/25 15:29:09 [debug] 10777#0: \*26 writev: 328  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http write filter  
0000000000000000  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http copy filter: 0  
“/404.html?”  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http finalize request: 0,  
“/404.html?” a:1, c:1  
2012/11/25 15:29:09 [debug] 10777#0: \*26 set http keepalive handler  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http close request  
2012/11/25 15:29:09 [debug] 10777#0: \*26 http log handler

Example configuration:

upstream php\_fpm {  
server 127.0.0.1:9000;  
}

server {  
listen 80;  
server\_name localhost;  
root /usr/share/nginx/html;

error\_log /var/log/nginx/default.log debug;

location / {  
index index.php index.html index.htm;  
}

error\_page 404 /404.html;  
error\_page 502 /502.html;  
location = /404.html {  
fastcgi\_intercept\_errors on;  
include /etc/nginx/fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root/404.php;  
fastcgi\_index index.php;  
fastcgi\_pass php\_fpm;  
}  
location = /502.html {  
}  
location ~\* .php$ {  
try\_files $uri =404;  
fastcgi\_intercept\_errors on;  
include /etc/nginx/fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_index index.php;  
fastcgi\_pass php\_fpm;  
}  
}

Best regards,  
Gus

Posted at Nginx Forum:

> **[custom 502 error page is ignored](https://forum.nginx.org/read.php?2,233209,233209#msg-233209)**
>
> Dear all, I'm currently wondering why nginx ignores my custom 502 Bad Gateway page and instead serves the internal 502 error page. I'm using nginx version 1.2.4 and php-fpm 5.4.8 The behaivior occures every time and is reproducible: If I stop php-fpm...

---

<div class="post-metadata">

**Author:** ![gus0253](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@gus0253](https://www.ruby-forum.com/u/gus0253)\
**Post date:** [November 26, 2012, 9:36am UTC](https://www.ruby-forum.com/t/custom-502-error-page-is-ignored/224765/2 "2012-11-26T09:36:47Z")

</div>

Hello!

On Sun, Nov 25, 2012 at 09:52:43AM -0500, gus0253 wrote:

> 502 error page.
> 
> I don’t know why my custom 502 error page is ignored, because it’s a static  
> html file.  
> Do you have any tipps for me?

[…]

The 502 error happens while processing 404 error, and by default  
nginx won’t do another error\_page redirection after first one. If  
you really want nginx to handle multiple error\_page redirections,  
use the “recursive\_error\_pages” directive, see here:

[http://nginx.org/r/recursive\_error\_pages](http://nginx.org/r/recursive_error_pages)

You should be careful to avoid loops though.

–  
Maxim D.

> **[Technical Support for NGINX and NGINX Plus Software](https://www.nginx.com/support/)**
>
> Get the help you need with support directly from the authors and maintainers of NGINX Open Source and NGINX Plus.

---

<div class="post-metadata">

**Author:** ![gus0253](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@gus0253](https://www.ruby-forum.com/u/gus0253)\
**Post date:** [November 26, 2012, 9:42am UTC](https://www.ruby-forum.com/t/custom-502-error-page-is-ignored/224765/3 "2012-11-26T09:42:03Z")

</div>

On Sun, Nov 25, 2012 at 09:52:43AM -0500, gus0253 wrote:

> 502 error page.
> 
> I don’t know why my custom 502 error page is ignored, because it’s a static  
> html file.  
> Do you have any tipps for me?

Querying index2.php results in error 404 handled by location “/404.html”  
which in turn results in (recursive) error 502 if php-fpm is stopped.

[http://nginx.org/r/recursive\_error\_pages](http://nginx.org/r/recursive_error_pages)

(Turn it on in the location “~\* .php$”.)
