# AWS HAProxy latency to Nginx?

**URL:** <https://www.ruby-forum.com/t/aws-haproxy-latency-to-nginx/209325>\
**Category:** NGINX\
**Created:** [July 11, 2011, 8:02pm UTC](https://www.ruby-forum.com/t/aws-haproxy-latency-to-nginx/209325 "2011-07-11T20:02:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 11, 2011, 8:02pm UTC](https://www.ruby-forum.com/t/aws-haproxy-latency-to-nginx/209325/1 "2011-07-11T20:02:09Z")

</div>

I’m new to nginx and have been migrating my organization’s web server  
away from apache/mod-php and to nginx 1.0 /php-fpm (php 5.3.6)  
We use Amazon Web Services and pipe requests to the nginx servers from  
the AWS load balancers (HAProxy).  
On the HAProxy built-in admin stats, I see ~200 current sessions for the  
server, but I only see ~80 php-fpm processes running (ps -ef, and also  
checking the php-fpm.log).

Here’s what I have configured in…

nginx.conf:

work\_processes 8;  
worker\_connections 1024;

php-fpm.conf:

pm.max\_children = 200  
pm.start\_servers = 10  
pm.max\_spare\_servers = 15  
pm.max\_requests = 500

Given these #s, I would think that php-fpm should be getting more  
requests, but I’m not certain if

1. Nginx is a bottleneck due to my configuration (maybe some other  
settings I’m not aware of)
2. There’s an innate latency from AWS to Nginx when proxying incoming  
requests and out of the nginx/php control.
3. There isn’t a direct 1-1 correlation between php-fpm process to  
request.

I just want to make sure that I’m not introducing a bottleneck due to my  
configuration and want to make sure we’re handling all the requests we  
should be.

Thoughts?

Thanks in advance.

Posted at Nginx Forum:

> **[AWS HAProxy latency to Nginx?](https://forum.nginx.org/read.php?2,212221,212221#msg-212221)**
>
> I'm new to nginx and have been migrating my organization's web server away from apache/mod-php and to nginx 1.0 /php-fpm (php 5.3.6) We use Amazon Web Services and pipe requests to the nginx servers from the AWS load balancers (HAProxy). On the...

---

<div class="post-metadata">

**Author:** ![barkloud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@barkloud](https://www.ruby-forum.com/u/barkloud)\
**Post date:** [July 11, 2011, 8:25pm UTC](https://www.ruby-forum.com/t/aws-haproxy-latency-to-nginx/209325/2 "2011-07-11T20:25:40Z")

</div>

> On the HAProxy built-in admin stats, I see ~200 current sessions for the  
> server, but I only see ~80 php-fpm processes running (ps -ef, and also  
> checking the php-fpm.log).

A session doesn’t really mean that everything is php related - there  
might be a static content fetches (for example if the same  
nginx/backend serves also images) and/or keepalive connections. Besides  
a single php child if the application code is optimal can  
manage to complete way more than 1 request per a given time period (1  
second).  
Personally I feel that 80 php process for 200 req/sec is somewhat too  
much - I try to to keep like 200-300 req/sec per 10-20 childs  
and bash the programmers for every page which doesn’t load in 0.0x secs.

You didn’t post the FPM manager line (eg something like pm = dynamic)  
that way the php-fpm master process spawns extra childs when  
there is a need (also informs in the log file if the max setting is too  
low) and kills the unneeded ones.

> I just want to make sure that I’m not introducing a bottleneck due to my  
> configuration and want to make sure we’re handling all  
> the requests we should be.

For sure nginx won’t be the bottleneck.

The php part depends on the specific application - eg there easily can  
be code that completes in nano/micro-seconds while the same  
time some people easily manage to write endless loops etc (btw php-fpm  
has a nice feature to monitor/backtrace such scripts and  
forcibly kill if they take too long ).

But to make 100% sure HaProxy offers a neat status page where you can  
see the connection/request error counts that way indicating if  
there is a problem at the backends.

For extra debug you always enable logging on both instances (frontend /  
backend) and compare the incoming / forwarded and served  
requests.

rr

---

<div class="post-metadata">

**Author:** ![barkloud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@barkloud](https://www.ruby-forum.com/u/barkloud)\
**Post date:** [July 11, 2011, 11:25pm UTC](https://www.ruby-forum.com/t/aws-haproxy-latency-to-nginx/209325/3 "2011-07-11T23:25:03Z")

</div>

> A session doesn’t really mean that everything is  
> php related - there might be a static content  
> fetches (for example if the same  
> nginx/backend serves also images) and/or keepalive  
> connections.

Ah yes, our nginx only handles php requests, but we do get random  
requests for non-existent js and image files what are rejected (in the  
nginx error.log), so those must contribute to the 200+ session listed on  
the HAProxy but that don’t get assigned to a php process.  
So a more accurate #s of valid php requests would be to look be (#  
concurrent session - # of error) at any given moment on the HAProxy  
stats page, right?

I’m still not quite certain how the 8 work\_processes value comes into  
play when optimizing nginx… I know you should set it to the # of  
cores, but with AWS, their calculation of cores doesn’t directly pertain  
to the # of physical cores on the box. But beyond that, is it just a  
matter of experimenting with values to see what results are better?  
But maybe that’s out of the scope of this thread for now.

> Besides a single php child if the  
> application code is optimal can  
> manage to complete way more than 1 request per a  
> given time period (1 second).  
> Personally I feel that 80 php process for 200  
> req/sec is somewhat too much - I try to to keep  
> like 200-300 req/sec per 10-20 childs  
> and bash the programmers for every page which  
> doesn’t load in 0.0x secs.

I don’t think HAProxy is calculating 200 req/sec, but rather 200  
concurrent requests at that given moment when the stats page refreshes.  
I ran a quick benchmark on the cli using “ab -n 200 -c 100  
[http://localhost/](http://localhost/)…” and see we’re doing about 400+ req/sec,  
given my conf file.  
I’m assuming by having 200 max\_children in the conf file, that’s setting  
a max of 200 php requests at any given moment (child-to-request ratio is  
1:1).  
But anyways, what #s should I be tweaking in the conf files, if  
possible, to achieve better #s? Isn’t the spawning of 80 processes done  
automatically and the only thing I can control is capping it (with  
max\_children)?

> You didn’t post the FPM manager line (eg something  
> like pm = dynamic) that way the php-fpm master  
> process spawns extra childs when  
> there is a need (also informs in the log file if  
> the max setting is too low) and kills the unneeded  
> ones.

I have it set as dynamic.

> error counts that way indicating if
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

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