# php-fpm (+ Nginx) closing connections



## Beeblebrox (Sep 9, 2013)

Has anyone come across this error in Nginx + php-fpm? My php files are not being served to the browser (localhost or client), and the nginx-error.log is showing  (server's IP is 192.168.1.10):

```
FOR LOCALHOST
[info] 961#0: *9 kevent() reported that client 192.168.1.10 closed keepalive connection
FOR CLIENT:
[info] 17499#0: *11 client closed connection while waiting for request, client: 192.168.1.30, server: 192.168.1.10:80
```
Static files are served normally by nginx, so problem source is not a general networking setting like hosts.allow.


----------



## quintessence (Sep 10, 2013)

Hi,

Paste your /usr/local/etc/nginx.conf, /etc/sysctl.conf, the output of `php -v`, `nginx -v`, OS version and PHP modules. And if you meet the same behaviour if you test with index.php with content:

```
<?php phpinfo(); ?>
```


----------



## Beeblebrox (Sep 10, 2013)

Sorry, I forgot to post most of the relevant info. 

I should first comment that my kernel is self-built (9-STABLE/amd64) with IPv6 disabled; that shouldn't matter though.
Ngnix: 
	
	



```
nginx version: nginx/1.5.4
```
PHP:  
	
	



```
PHP 5.5.3 (cli) (built: Aug 30 2013)
```
sockstat lists 21 instances (with different PID's of course) as:

```
www      php-fpm    1019  0  stream /var/run/php/php-fpm.sock
www      php-fpm    1019  8  stream (not connected)
```
I have stripped-down my nginx.conf for debugging and followed the initial setup here: http://wiki.nginx.org/PHPFcgiExample. I'm using a file specified there but get no output. test.php:

```
<pre><?php var_export($_SERVER)?></pre>
```
nginx.conf:

```
user  www www;
worker_processes  2;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;   # 0
    tcp_nodelay on;
    gzip  on;
    access_log  /var/log/www/nginx-access.log;
    error_log  /var/log/www/nginx-error.log  debug;

    server {
        listen       192.168.1.10:80;
        server_name  myserver;
	root /pathto/root;
#	fastcgi_index  index.php;
#       include fastcgi_params;
        index  index.html index.htm index.php;

        location ~ [^/]\.php(/|$) {
                fastcgi_split_path_info ^(.+?\.php)(/.*)$;
                if (!-f $document_root$fastcgi_script_name) {
                        return 404;
                }
 
	        fastcgi_pass unix:/var/run/php/php-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }       }       }
```
/usr/loacl/etc/php-fpm.conf is also very simple (I'll be lowering the start_servers value later). No error messages in /var/log/www/php.log:

```
[global]
error_log = /var/log/www/php.log
daemonize = yes

[www]
listen = /var/run/php/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0666

user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
php_admin_value[error_log] = /var/log/www/php.log
```
/etc/sysctl.conf has no setting related to nginx nor php-fpm nor socket related settings.

Another php-fpm oddity is when I try to stop it as a service; however the open sockets above should mean php is running. Could it be that php-fpm fails to start but the sockets linger around? `# service php-fpm onestop`:

```
php_fpm not running? (check /var/run/php-fpm.pid)
```

php also complains about some settings, but that AFAIK, is easily fixed in one of the PHP config files (which I have not gotten around to mulling through):

```
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/20121212-zts/pcre.so' - Cannot open "/usr/local/lib/php/20121212-zts/pcre.so" in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/20121212-zts/apc.so' - Cannot open "/usr/local/lib/php/20121212-zts/apc.so" in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/20121212-zts/pcntl.so' - Cannot open "/usr/local/lib/php/20121212-zts/pcntl.so" in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/20121212-zts/pdf.so' - Cannot open "/usr/local/lib/php/20121212-zts/pdf.so" in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/20121212-zts/apc.so' - Cannot open "/usr/local/lib/php/20121212-zts/apc.so" in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/20121212-zts/curl.so' - Cannot open "/usr/local/lib/php/20121212-zts/curl.so" in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/20121212-zts/ftp.so' - Cannot open "/usr/local/lib/php/20121212-zts/ftp.so" in Unknown on line 0
```
Regards.


----------



## quintessence (Sep 10, 2013)

Hi,

The "oddity" is because you "cut" the default php-fpm.conf.  You should not remove the line about pid, because you will not able to start/stop/restart the service in a normal way. Try to use default php-fpm.conf with your changes, but without removed pid line.

If you are going to use way from the example from Nginx's page, you should replace your fastcgi_params with the one from the example, because of missing SCRIPT_FILENAME and PATH_INFO (it is normal to get blank page with the config from example without these lines). Or better, if you don't like to replace the default's one from ports, to save configuration from Nginx's page in file fastcgi_params.nginx_org for example, and include it in nginx.conf instead of fastcgi_params.


----------



## Beeblebrox (Sep 10, 2013)

Thanks for answering.

I "cut" php-fpm.conf just to post on the thread - otherwise the file is unscathed. The pid parameter had given me some problems in the beginning, so I decided to let php-fpm go to its default value. Otherwise it had been set to

```
pid = /var/run/php/php-fpm.pid
```
I can't stop the service, so I'll have to wait for a reboot to post the error I was getting when that setting was activated.

I have changed the php related part in nginx.conf as:

```
location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_index  index.php;
        include        fastcgi_params; 
        }
```
And restarted Nginx. Now I get:

"404 Not Found" for http://192.168.1.10/test.php/
"File not found" for http://192.168.1.10/test.php
And the same "File not found" for index.php which has

```
<? phpinfo(); ?>
```

Also, I had manually added into fastcgi_params:

```
fastcgi_param  PATH_INFO          $fastcgi_path_info;
```
EDIT: I get same result if I change in nginx.conf

```
fastcgi_param SCRIPT_FILENAME $fastcgi_path_info$fastcgi_script_name;
```


----------



## quintessence (Sep 10, 2013)

Hi,

Do not make any changes in your nginx.conf - it is ok, because it is as from the example. Just only make sure you have to correct fastcgi_params from Nginx's site.

And you should have something like:
nginx.conf:

```
server {
        listen      your_ip;
        server_name your_server_name;
        root your_document_root;
        index  index.html index.htm index.php;

        location ~ [^/]\.php(/|$) {
                fastcgi_split_path_info ^(.+?\.php)(/.*)$;
                if (!-f $document_root$fastcgi_script_name) {
                        return 404;
                }

                fastcgi_pass unix:/var/run/php/php-fpm.sock;
                fastcgi_index index.php;
                include [B][file]fastcgi_params.nginx[/file];[/B]
        }
    }
}
```
,
fastcgi_params.nginx (copy/paste):

```
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_FILENAME         $document_root$fastcgi_script_name;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
fastcgi_param   PATH_INFO               $fastcgi_path_info;
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/1.1;
fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;

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   HTTPS                   $https;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param   REDIRECT_STATUS         200;
```
.

You can stop a service by killing its processes. If you comment the line with document root, you will get 404 not found.


----------



## Beeblebrox (Sep 10, 2013)

Thanks for your help, I have it working. I had placed in fastcgi_params

```
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name
```
But for some reason (probably debugging) had commented that out - but it's working now.


```
pid = /var/run/php/php-fpm.pid
```
Is enabled, but I still get a "php_fpm not running?" error when trying to stop it as a service because the service is looking for /var/run/php-fpm.pid while I have specified pid differently. If I change it in php-fpm.conf to location that it expects, it then works normally.

I have two small config problems remaining (I'm migrating from lighttpd):


1.alias gives me "404", where xyz-long-foldername is located as a sub-folder of document_root:

```
location /xyz/ {  
         alias /xyz-long-foldername/;
```
2.A PHP program has as location $document_root/nm, but the index file is located in a sub-folder named nm-admin. I thought this would be correct entry for that:

```
location /nm/ {
	  fastcgi_param SCRIPT_FILENAME $fastcgi_path_info/nm-admin$fastcgi_script_name;
          fastcgi_pass unix:/var/run/php/php-fpm.sock;
```
But it does not work and gives me blank screen.

I would appreciate it if you could tell me how to correct these entries.

P.S. The "Thanks Button" does not work on your last post for some reason...


----------



## quintessence (Sep 10, 2013)

Hi,

It is because /var/run/php-fpm.pid is placed "staticly" in /usr/local/etc/rc.d/php-fpm and when you try to stop the service via the script, it will look in the directory from the rc script for the PID. You can change directory in the rc script, but if you going to update your packages in the future, your changes in the rc scripts will be lost, so it is not a bad idea to keep defaults.

For alias you have to place the whole physical path to the directory:


```
location /xyz/ {  
         alias /your-document-root/xyz-long-foldername/;
```

For 2nd[/del the second one - you already configured for *.php, which is read by the server and it is mixing with the settings for the second location (which most probably are not so correct). It is not correct to make such a mix - location + proxy(fastcgi)_pass with performing global configuration for the same files with some extension, which in your case is *.php.

What are you trying to achieve, I mean what type of application you are going to migrate to Nginx + PHP-FPM? Placing such type of alias is a bit strange, and 2nd second location configuration is also strange. For applications like some frameworks that for example if you open in your browser http://example.com/directory/sometest the request should be proceed by some internal controller which determines what content to display, in Nginx is usually done (most of the times) by the try_files directive, not by aliases or creating multiple locations. But it depends on the application.


----------



## Beeblebrox (Sep 12, 2013)

> For alias you have to place the whole physical path to the directory:
> location /xyz/ {
> alias /your-document-root/xyz-long-foldername/;


Not working unfortunately, is there a module that needs to be enabled for alias?

The "index file in sub-folder" problem is for a program called neuralmesh. Not in the ports, and maybe somewhat ancient since no recent code upgrades. It's not my code, pure php written and I just wanted to try it. As strange as it is, the coder has done exactly as I have described, and linking the server to the sub-folder index.php disables administrative functions (which are located in the upper folder).

I also have this beauty: I have installed an upgraded and locally maintained version of www/sitebar (3.4). When I try to access the site through nginx, the error log shows:

```
[error] 30915#0: *66 FastCGI sent in stderr: "Access to the script '/usr/www/sitebar/skins/System/sitebar.css' has been denied (see security.limit_extensions)" while reading response header from upstream, client: 192.168.1.10, request: "GET /sitebar/skins/System/sitebar.css?version=3.4 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php-fpm.sock:", host: "192.168.1.10", referrer: "http://192.168.1.10/sitebar/"
```
The error messages repeat for 20 or 30 more file-names. I have read about security.limit_extensions, but they were all in Linux context and I could not get that setting accepted in FreeBSD.


----------



## quintessence (Sep 12, 2013)

Hi,

No, there isn't. Alias works in that way (you can test with random directories with _static_ files), it may not work with your configuration, because your configuration is not suitable in this case and/or there is something wrong with it.

If I understand you right, you have let's say /home/www for your document root. In that folder you have subfolder nm, in which you have sub-folder (sub-sub-folder of the document root) called nm-admin or you have only nm-admin folder in the document root which you would like to open in your browser with the name nm with alias? Or other maybe?

If it is the first case, i.e physical directories:

```
/home/www - document root
/home/www/nm - subfolder in the document root 
/home/www/nm/nm-admin - sub-sub-folder in the document root
```
you don't need to make any changes in the configuration, if you open in your browser http://example.com/nm/nm-admin/ it will work, if you open http://example.com/nm/ it will also work.
But if it is the second case, i.e physical directories:

```
/home/www - document root
/home/www/nm-admin - subfolder in document root, which when you try to open http://example.com/nm/ should be proceeded
```
usually this is done by symlinks, instead of alias or just by renaming the folder with the name you would like.

Of course in some cases where your folder is not in the document root, you will have to link it with alias. And of course, if the folder is located in the document root with name nm-admin and you want to be named just nm in server side, and you don't want to make symlink, rename folder for any reason, you can perform this by the next example and very basic config:

(Physical directory structure is case 2, one sub-folder called nm-admin in the document root, which should be "renamed" by the server to the name nm.)

```
server
{
listen your_ip;
server_name server_name;
root /home/www
index some_indexes;

// link your static files from subfolder to the new name
location /nm/ // (1) when you try to open in the browser any static file: http://example.com/nm/somefile.jpg
{
alias /home/www/nm-admin/; // (2) point (1) to physical directory on the filesystem (inside or outside document root, no matter)
}
//

// link your dynamic files from subfolder to the new name
location ~  ^/nm/(.+\.php)$ // (3) when you try to open in the browser: http://example.com/nm/somefile.php
{
alias /home/www/nm-admin/$1; // (4) point (3) to physical directory on the filesystem (inside or outside document root, no matter)
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass your_socket;
}
//

// link your dynamic files from document root (physically placed there) 
location ~ \.php$ // (5) matches otherfiles.php in the document root except files from (4), for example: /home/www/index.php
{
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass your_socket;
}
//
}
```
However, I may be don't get what you really try to achieve, so please paste your physical directories and what would you like to open in your browser with simple words (links ). I downloaded first program and took a look in its code and structure, no any special requirements needed to configure it to run on any web server, no .htaccess-es, just one small folder which you have to place in your document root.


----------

