# Random Instant 502 Bad Gateway Errors

**URL:** <https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356>\
**Category:** NGINX\
**Created:** [May 29, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356 "2010-05-29T15:03:56Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/1 "2010-05-29T15:03:56Z")

</div>

Hi,

I’m French and I’ve some problem with nginx 0.8.38 and PHP-FPM 5.3.0  
with Suhosin patch 😕  
I’ve got random instant 502 Bad Gateway errors, generaly a refresh make  
the page works again but it’s really anoying.

Nginx Config :

```auto
user www;
worker_processes 1;
daemon on;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

worker_rlimit_nofile 32000;

events {
    worker_connections 4096;
    use epoll;
}

http {

    include mime.types;
    default_type text/plain;

    limit_zone http $binary_remote_addr 1m;
    limit_zone https $binary_remote_addr 1m;

    client_body_timeout 8;
    client_header_timeout 5;
    keepalive_timeout 5;
    keepalive_requests 10;
    send_timeout 8;

    client_max_body_size 512k;

    access_log off;
    error_log off;

    client_header_buffer_size 16k;
    large_client_header_buffers 16 16k;

    output_buffers 2 32k;
    postpone_output 1460;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    reset_timedout_connection on;

    ignore_invalid_headers on;
    autoindex on;
    msie_padding on;

    gzip on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain text/css application/json 
application/x-javascript text/xml application/xml application/xml+rss 
text/javascript;
    gzip_buffers 16 16k;
    gzip_disable â€œMSIE [1-6].(?!.*SV1)â€;

    server
    {
      listen 80 default rcvbuf=8192 sndbuf=16384 backlog=65536;
      index index.html index.htm index.php;
      root /home/www/;

      limit_conn http 10;

      access_log off;

     location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
      expires 3600;
      break;
     }

     location ~ \.php(/|$) {
         include fastcgi_params;
     }

     location ~ \.php5(/|$) {
         include fastcgi_params;
     }

     location ~ \.php4(/|$) {
         include fastcgi_params;
     }

     location /cache/status {
         stub_status on;
         access_log off;
     }
    }
```

Fastcgi params :

```auto
fastcgi_connect_timeout 5;
fastcgi_send_timeout 8;
fastcgi_buffer_size 512k;
fastcgi_buffers 64 64k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_ignore_client_abort on;
fastcgi_intercept_errors on;
fastcgi_read_timeout 45;

fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI;
fastcgi_param SERVER_SOFTWARE RD;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REDIRECT_STATUS 200;
```

PHP-FPM conf :

```auto
<?xml version="1.0" ?>
<configuration>
  <section name="global_options">
    <value name="pid_file">/var/run/php5-fpm.pid</value>
    <value name="error_log">/var/log/php-fpm.log</value>
    <value name="log_level">warn</value>
    <value name="emergency_restart_threshold">60</value>
    <value name="emergency_restart_interval">1m</value>
    <value name="process_control_timeout">4s</value>
    <value name="daemonize">yes</value>
  </section>

  <workers>
    <section name="pool">
      <value name="name">RD</value>
      <value name="listen_address">/tmp/php.socket</value>
      <value name="listen_options">
        <value name="backlog">-1</value>
        <value name="owner">www</value>
        <value name="group">www</value>
        <value name="mode">0666</value>
      </value>

      <value name="php_defines">
      </value>

      <value name="user">www</value>
      <value name="group">www</value>

      <value name="pm">
        <value name="style">static</value>
        <value name="max_children">150</value>
        <value name="apache_like">
          <value name="StartServers">25</value>
          <value name="MinSpareServers">5</value>
          <value name="MaxSpareServers">35</value>
        </value>
      </value>

      <value name="request_terminate_timeout">0s</value>
      <value name="request_slowlog_timeout">0s</value>
      <value name="slowlog">/var/log/php-fpm.log.slow</value>
      <value name="rlimit_files">1024</value>
      <value name="rlimit_core">0</value>
      <value name="chroot"></value>
      <value name="chdir"></value>
      <value name="catch_workers_output">yes</value>
      <value name="max_requests">65536</value>
      <value name="allowed_clients">127.0.0.1</value>

      <value name="environment">
        <value name="HOSTNAME">$HOSTNAME</value>
        <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
        <value name="TMP">/tmp</value>
        <value name="TMPDIR">/tmp</value>
        <value name="TEMP">/tmp</value>
        <value name="OSTYPE">$OSTYPE</value>
        <value name="MACHTYPE">$MACHTYPE</value>
        <value name="MALLOC_CHECK_">2</value>
      </value>
    </section>
  </workers>
</configuration>
```

Server config :

```auto
Intel Corei5 (Lynnfield) 4x 2.66+ GHz 8 Mo L2 - FSB 1333 MHz
64 bits
8 Go DDR3
Intel SSD X25-M 2x 80 Go RAID 0/1
GigaEthernet
```

If anyone have an idea 🙂

It’s very strange because it’s instant and random whereas it would be  
timedout all the time 😕

Thank’s in advance.

Posted at Nginx Forum:

> **[Nginx Forum :: Nginx Mailing List - English](https://forum.nginx.org/read.php?2,91854,91854#msg-91854)**
>
> A portal to and from the mailing list.

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/2 "2010-05-29T15:10:57Z")

</div>

This should give you some answers:

$ cat /var/log/nginx/error.log

Best regards,  
Piotr S. \< removed\_email\_address@domain.invalid \>

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 7:34pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/3 "2010-05-29T19:34:55Z")

</div>

150 children is quite a lot. Sure you need that? 🙂

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/4 "2010-05-29T20:09:28Z")

</div>

Usually using 5 is quiet enough.

Regards,  
Joe

2010/5/30 Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 8:24pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/5 "2010-05-29T20:24:41Z")

</div>

2010/5/29 Joe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Usually using 5 is quiet enough.

either you have a well optimized website with a 99% cache ratio or you  
don’t have traffic. In the real world, there is so much web apps which  
consume a lot of resources and for thoses you’ll need much more than 5  
children to handle them.

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 8:47pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/6 "2010-05-29T20:47:30Z")

</div>

So yes, I agree with you Loyet.  
Currently using apc combined with the fpm spawn.  
Frequently fpm restarted, and might be raising up the children in the  
future.  
We should take care about it very well, based on daily usage experience,  
to  
get the good nginx and fpm tune configuration.

Regards,  
Joe

2010/5/30 JÃ©rÃ´me Loyet [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/7 "2010-05-29T15:32:02Z")

</div>

I’ve just allow error logs. I will wait a new 502 Bad Gateway.  
And no, I don’t get 150 children but generally about 10.

Thank’s !

Posted at Nginx Forum:

> **[Nginx Forum :: Nginx Mailing List - English](https://forum.nginx.org/read.php?2,91854,91870#msg-91870)**
>
> A portal to and from the mailing list.

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 8:52pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/8 "2010-05-29T20:52:11Z")

</div>

Yup. General rule of thumb is roughly how many concurrent php requests  
you expect at peak time. Hopefully your code runs fast enough to spend  
less than 1 second processing. Cause if the children are busy it will  
block until one opens up or until nginx’s configured timeout hits.

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/9 "2010-05-29T21:03:19Z")

</div>

Yes Mike, that’s a very logic things.

Regards,  
Joe

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 9:36pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/10 "2010-05-29T21:36:05Z")

</div>

Thank’s for your responses.

At peak time I have about 15 children exept if I’m getting attacked 😕  
My server is the object of a lot of DoS attacks and it is possible to  
get 100 children.  
But you have to know that this server is use for a website but it is  
also the infos server of my 30 servers of downloads.  
I have Suhosin with APC enabled and my website has a SQL cache created  
by me.

I should decrease the value of children ?

And sorry but after activating error logs I have not managed to  
reproduce the problem and logs are still empty.

Posted at Nginx Forum:

> **[Nginx Forum :: Nginx Mailing List - English](https://forum.nginx.org/read.php?2,91854,91998#msg-91998)**
>
> A portal to and from the mailing list.

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/11 "2010-05-29T21:40:11Z")

</div>

is you’re getting attacked you shouldn’t be allowing it to execute php  
you should be blocking it 🙂

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 9:44pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/12 "2010-05-29T21:44:52Z")

</div>

Nginx limit\_conn does that, no ?  
Look at my Nginx config 🙂

Posted at Nginx Forum:

> **[Nginx Forum :: Nginx Mailing List - English](https://forum.nginx.org/read.php?2,91854,92005#msg-92005)**
>
> A portal to and from the mailing list.

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/13 "2010-05-29T22:10:03Z")

</div>

I’ve already limit\_conn in my config file.

```auto
Active connections: 51
server accepts handled requests
 5634 5634 6587
Reading: 1 Writing: 1 Waiting: 49
```

Posted at Nginx Forum:

> **[Nginx Forum :: Nginx Mailing List - English](https://forum.nginx.org/read.php?2,91854,92016#msg-92016)**
>
> A portal to and from the mailing list.

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 29, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/14 "2010-05-29T21:46:03Z")

</div>

2010/5/29 TheCrach [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thank’s for your responses.
> 
> At peak time I have about 15 children exept if I’m getting attacked 😕  
> My server is the object of a lot of DoS attacks and it is possible to get 100 children.  
> But you have to know that this server is use for a website but it is also the infos server of my 30 servers of downloads.  
> I have Suhosin with APC enabled and my website has a SQL cache created by me.
> 
> I should decrease the value of children ?

it depends on your system. If your system is able to handle 150  
concurent request so you’re fine with it. When you’re under DOS  
attack, your server won’t crash. But if all children are working at  
same time, new clients won’t be able to connect. In this case, the DOS  
will be a success (from the point of view of the attacker).

You can look at some interesting nginx modules:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 30, 2010, 9:53am UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/15 "2010-05-30T09:53:24Z")

</div>

I’ve just get a 502 Bad gateway. It’s very random but instant ( the  
server do not take time to show the error message ).  
This is strange because any logs appear in Nginx and PHP-FPM. And I’ve  
decrease the children value to 64.  
I don’t understand why this error appear !? For me, it seems that the  
child is restarted at nginx loading 😕

Posted at Nginx Forum:

> **[Nginx Forum :: Nginx Mailing List - English](https://forum.nginx.org/read.php?2,91854,92161#msg-92161)**
>
> A portal to and from the mailing list.

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 30, 2010, 1:49pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/16 "2010-05-30T13:49:16Z")

</div>

Hummm … Always the same problem. Any ideas ?

Posted at Nginx Forum:

> **[Nginx Forum :: Nginx Mailing List - English](https://forum.nginx.org/read.php?2,91854,92203#msg-92203)**
>
> A portal to and from the mailing list.

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [June 1, 2012, 12:42pm UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/17 "2012-06-01T12:42:19Z")

</div>

2010/5/29 TheCrach [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> http {  
> keepalive\_requests 10;  
> output\_buffers 2 32k;
> 
> {  
> break;  
> location ~ .php4(/|$) {  
> Fastcgi params :  
> fastcgi\_read\_timeout 45;  
> fastcgi\_param DOCUMENT\_URI $document\_uri;  
> [/code]  
> 60  
> -1
> 
> ```
> <value name="pm">
> <value name="style">static</value>
> <value name="max_children">150</value>
> 
> ```

are your sure you don’t get 150 concurent connections to FPM ?

---

<div class="post-metadata">

**Author:** ![TheCrach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/41988e/32.png) [@TheCrach](https://www.ruby-forum.com/u/TheCrach)\
**Post date:** [May 30, 2010, 10:03am UTC](https://www.ruby-forum.com/t/random-instant-502-bad-gateway-errors/189356/18 "2010-05-30T10:03:55Z")

</div>

I’ve just edit my PHP-FPM conf with :

```auto
                 <value name="emergency_restart_threshold">30</value>
    <value name="emergency_restart_interval">0s</value>
    <value name="process_control_timeout">5s</value>
```

I hope it will correct my problem 🙂

Posted at Nginx Forum:

> **[Nginx Forum :: Nginx Mailing List - English](https://forum.nginx.org/read.php?2,91854,92164#msg-92164)**
>
> A portal to and from the mailing list.
