# Nginx recycles workers too often

**URL:** <https://www.ruby-forum.com/t/nginx-recycles-workers-too-often/184373>\
**Category:** NGINX\
**Created:** [March 1, 2010, 6:14pm UTC](https://www.ruby-forum.com/t/nginx-recycles-workers-too-often/184373 "2010-03-01T18:14:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [March 1, 2010, 6:14pm UTC](https://www.ruby-forum.com/t/nginx-recycles-workers-too-often/184373/1 "2010-03-01T18:14:27Z")

</div>

A couple of months ago I posted a topic because I thought my php-cgi  
processes were recycling too often (you can view it here  
[http://forum.nginx.org/read.php?2,45516](http://forum.nginx.org/read.php?2,45516)). After trying to troubleshoot  
this issue for months and not getting anywhere, I now think its nginx  
that is stalling or recycling, not php-cgi. I test this with a static  
html page and kept refreshing it. At the same time my vbulletin forum  
would take a long time to load, the same thing happened with the html  
page. If I’m watching the top command, when this delay happens both  
nginx and php-cgi will disappear from the list then reappear a few  
seconds later.

When this happens, there are delays when browsing the sites of anywhere  
from 3 to 5+ seconds while the workers seem to reload. Normally, nginx  
with php-cgi is extremely fast for me, so when you’re used to \<1 second  
load times and it seems to delay with 5+ seconds sometimes, its very  
noticeable. I could understand if this happens once every 5 or 10  
minutes, but its happening 2-3 times per minute, sometimes even more  
than that. We average about 1,000 concurrent users at any given time.  
Usually anywhere from 800 to 1200+. We are running a quad core  
processor with about 4gb of RAM on a dedicated server. We have nginx,  
php-cgi and MySQL running on this machine. The PHP sites this server  
runs are a Wordpress blog (for news), a vBulletin forum, and a rarely  
used MediaWiki.

I have php-cgi running with 8 children right now, which seems to be  
fine. I’ve tried 16 and even 32 and it didn’t change anything.

I usually leave my error\_log to crit only, but I did turn it on debug  
today. There’s nothing out of the ordinary going on in the logs  
whenever this happens. It’ll show normal KeepAlive connections closed,  
or connection to upstream closed by client, etc.

Any ideas are helpful. Here’s my nginx.conf file. I’ve included all of  
my vhosts in the one file so its easier to read while I work on this  
issue.

user nobody;  
worker\_processes 4;  
error\_log logs/error.log crit;  
worker\_rlimit\_nofile 16384;

events {  
worker\_connections 1024; # you might need to increase this setting for  
busy servers  
#use rtsig; # Linux kernels 2.6.x change to epoll  
use epoll; # Linux kernels 2.6.x change to epoll  
multi\_accept off;  
}

http {  
include mime.types;  
#default\_type application/octet-stream;

server\_tokens off;  
sendfile on;  
tcp\_nopush on;  
tcp\_nodelay on;  
client\_max\_body\_size 50m; # More if you need up upload files larger  
than 50mb  
client\_header\_buffer\_size 8k;

keepalive\_timeout 5;  
#keepalive\_requests 0;

gzip on;  
gzip\_min\_length 1100;  
gzip\_buffers 16 8k;  
gzip\_http\_version 1.0;  
gzip\_vary on;  
gzip\_comp\_level 1;  
gzip\_proxied any;  
gzip\_types text/plain text/html text/css application/x-javascript  
text/xml text/javascript;  
#Disable gzip for certain browsers.  
gzip\_disable â€œMSIE [1-6].(?!.\*SV1)â€;  
ignore\_invalid\_headers on;

server\_names\_hash\_max\_size 4096;  
server\_names\_hash\_bucket\_size 128;

limit\_zone gulag $binary\_remote\_addr 5m;

client\_header\_timeout 2m;  
client\_body\_timeout 2m;  
client\_body\_buffer\_size 128k;  
send\_timeout 2m;  
connection\_pool\_size 256;  
large\_client\_header\_buffers 4 8k;  
request\_pool\_size 4k;  
output\_buffers 1 32k;  
postpone\_output 1460;

fastcgi\_buffers 64 4k;  
fastcgi\_buffer\_size 64k;  
fastcgi\_read\_timeout 240s;

#################### [www.domain.com](http://www.domain.com) ####################  
server {  
access\_log off;  
limit\_conn gulag 50;  
#error\_log logs/vhost-error\_log warn;  
listen 80 default;  
server\_name [www.domain.com](http://www.domain.com) [domain.com](http://domain.com);  
root /home/domain/public\_html;  
index index.php index.html index.htm;  
#rewrite ^(.\*)$ $scheme://www.domain.com$1 permanent;

# pass the PHP scripts to FastCGI server

```
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:8888;
   #fastcgi_pass unix:/var/run/nginx-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME 

```

/home/domain/public\_html$fastcgi\_script\_name;  
include /usr/local/nginx/conf/fastcgi\_params;  
fastcgi\_intercept\_errors on;  
}

location / {  
if (!-e $request\_filename) {  
rewrite ^.\*$ /index.php last;  
}  
}  
}

#################### [forum.domain.com](http://forum.domain.com) ####################  
server {  
access\_log off;  
limit\_conn gulag 50;  
#error\_log logs/vhost-error\_log warn;  
listen 80;  
server\_name [forum.domain.com](http://forum.domain.com) [www.forum.domain.com](http://www.forum.domain.com);  
root /home/domain/public\_html/forums;  
index index.php index.html index.htm;  
#rewrite ^(.\*)$ $scheme://forum.domain.com$1 permanent;

location /archive/ {  
if ($request\_filename ~ “.html$”) {  
rewrite ^/(._)/f-([0-9]+).html$ /$1?f-$2.html last;  
rewrite ^/(._)/t-([0-9]+).html$ /showthread.php?t=$2 permanent;  
}  
}

# pass the PHP scripts to FastCGI server

```
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:8888;
   #fastcgi_pass unix:/var/run/nginx-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME 

```

/home/domain/public\_html/forums$fastcgi\_script\_name;  
include /usr/local/nginx/conf/fastcgi\_params;  
fastcgi\_intercept\_errors on;  
}  
}

#################### [wiki.domain.com](http://wiki.domain.com) ####################  
server {  
access\_log off;  
limit\_conn gulag 50;  
#error\_log logs/vhost-error\_log warn;  
listen 80;  
server\_name [wiki.domain.com](http://wiki.domain.com) [www.wiki.domain.com](http://www.wiki.domain.com);  
root /home/domain/public\_html/wiki;  
index index.php index.html index.htm;  
#rewrite ^(.\*)$ $scheme://forum.domain.com$1 permanent;

# pass the PHP scripts to FastCGI server

```
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:8888;
   #fastcgi_pass unix:/var/run/nginx-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME 

```

/home/domain/public\_html/wiki$fastcgi\_script\_name;  
include /usr/local/nginx/conf/fastcgi\_params;  
fastcgi\_intercept\_errors on;  
}  
}

}

Thanks in advance!

Posted at Nginx Forum:

> **[nginx recycles workers too often](https://forum.nginx.org/read.php?2,58646,58646#msg-58646)**
>
> A couple of months ago I posted a topic because I thought my php-cgi processes were recycling too often (you can view it here http://forum.nginx.org/read.php?2,45516). After trying to troubleshoot this issue for months and not getting anywhere, I now...

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [March 2, 2010, 4:04am UTC](https://www.ruby-forum.com/t/nginx-recycles-workers-too-often/184373/2 "2010-03-02T04:04:31Z")

</div>

> I usually leave my error\_log to crit only, but I did turn it on debug  
> today. There’s nothing out of the ordinary going on in the logs whenever  
> this happens. It’ll show normal KeepAlive connections closed, or  
> connection to upstream closed by client, etc.

Do you see “worker process exited on signal XX” in your error logs  
(visible  
even on critical level)? If you do then please post your debug logs from  
that child prior to its crash. If you don’t then blame PHP 😛

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [March 2, 2010, 5:09am UTC](https://www.ruby-forum.com/t/nginx-recycles-workers-too-often/184373/3 "2010-03-02T05:09:30Z")

</div>

## Piotr S. Wrote:

> even on critical level)? If you do then please  
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

I do not show that in any of my logs. If I have it set to crit, nothing  
ever shows in the log except when I start the process. The reason I  
don’t think its PHP is because the delay happens when I refresh a  
standard HTML page.

I’m completely lost as to what’s going on. If I’m viewing top then  
nginx and php-cgi disappears from the list, but that’s because its  
cpu/ram usage goes down. If I view the running processes from within my  
control panel (WHM), nginx and php-cgi never actually goes away. So its  
almost like the requests are being held up somewhere for 5-10 seconds  
and nginx resources goes down because its not actually getting the  
requests. Any idea if this is possible and what the cause would be?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,58646,58884#msg-58884](http://forum.nginx.org/read.php?2,58646,58884#msg-58884)

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [March 2, 2010, 7:03am UTC](https://www.ruby-forum.com/t/nginx-recycles-workers-too-often/184373/4 "2010-03-02T07:03:36Z")

</div>

> So its almost like the requests are being held up somewhere for 5-10  
> seconds and nginx resources goes down because its not actually getting the  
> requests. Any idea if this is possible and what the cause would be?

Check debug logs, they’ve got timestamps, so if there is 5-10s gap in  
the  
request processing then you should notice it. Following your “stuck”  
request  
shouldn’t be hard.

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [March 2, 2010, 10:46pm UTC](https://www.ruby-forum.com/t/nginx-recycles-workers-too-often/184373/5 "2010-03-02T22:46:04Z")

</div>

On Tue, Mar 2, 2010 at 5:14 PM, mindfrost82 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Apparently that’s not the issue either. The longest difference in my debug logs is 2 seconds, otherwise there’s an entry for every second. Any other ideas? I’m completely lost on this issue.

tcpdump is your friend. Try to capture all the traffic from your nginx  
machine and your PHP backend. That must reveal what exactly is going  
on.

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [March 2, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/nginx-recycles-workers-too-often/184373/6 "2010-03-02T17:14:43Z")

</div>

## Piotr S. Wrote:

> Following your “stuck” request  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)  
> Apparently that’s not the issue either. The longest difference in my  
> debug logs is 2 seconds, otherwise there’s an entry for every second.  
> Any other ideas? I’m completely lost on this issue.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,58646,59089#msg-59089](http://forum.nginx.org/read.php?2,58646,59089#msg-59089)

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [March 30, 2010, 4:17am UTC](https://www.ruby-forum.com/t/nginx-recycles-workers-too-often/184373/7 "2010-03-30T04:17:59Z")

</div>

I believe you should tweak PHP\_FCGI\_MAX\_REQUESTS. You can try 5000 for  
instance. In order to get this working you need to kill all (stop nginx,  
pcp\_cgi and killall nginx, the same with php\_cgi or whatever you called  
the init.d script) the processes running and start php\_cgi and nginx  
again. I’m saying this because I just read your other thread where it  
does nothing PHP\_FCGI\_MAX\_REQUESTS on your server.

I also run a wp blog and vb forum on my site. I’m getting over 170k  
uniques per day. I run mysqld on a separate server. I have 8 workers. 15  
children. I’m currently tweaking PHP\_FCGI\_MAX\_REQUESTS.1000 seems to be  
too low for me since php-cgi processes get restarted too often.

Posted at Nginx Forum:

> **[Re: nginx recycles workers too often](https://forum.nginx.org/read.php?2,58646,69289#msg-69289)**
>
> A couple of months ago I posted a topic because I thought my php-cgi processes were recycling too often (you can view it here http://forum.nginx.org/read.php?2,45516). After trying to troubleshoot this issue for months and not getting anywhere, I now...
