# How to count number of connections from nginx workers to php-cgi unix socket?



## cmyker (Feb 8, 2011)

I need to count number of connections to php's cgi unix socket (created with spawn-fci). When nginx initiates a connection to cgi socket one of spawned php processes accepts this connection, processes input and outputs data. But number of processes is limited and i want to be able to monitor amount of free processes. I tried all available tools (netstat, sockstat even lsof) but it seems there is no way to determine how many active connections from nginx to unix socket. Please advise.


----------



## DutchDaemon (Feb 9, 2011)

See if fstat(1) (FreeBSD's own lsof which has been around since dinosaurs) has useful flags. Untested.


----------



## cmyker (Feb 9, 2011)

Same result. Actually it shows connections from nginx to local unix socket (and other tools like netstat too) but amount of this connections is always 16 on my server. It looks like nginx is always connected to this socket and do not initiates new connections. But this isn't true as i mentioned in my 1st post. I was wondering may be nginx is using a sort of persistent socket connection model and reproduced same nginx+php-cgi stack on linux. Netstat and lsof under linux are clearly showing new connections to php's socket e.g. amount of connections is changing after php file is requested and nginx is connecting to php-cgi socket.


----------



## bmimatt (Dec 1, 2012)

There's a 'keepalive' config directive that can be used in upstream config block.  It  is not the same as keepalive for client side of the connection.  This should give you more detail:

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
Good Luck!


----------

