Nginx – Nagios+NGiNX “Primary script unknown” Error

debian-squeezenagiosnginxphp-fpm

I'm trying to get Nagios setup on NGiNX using PHP-FPM.

I installed fcgiwrap and and I'm able to execute a Perl script from the cgi-bin folder, but when I try to access Nagios, I get the following error in the NGiNX error log:

2012/08/23 16:40:21 [error] 8319#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.1.1.1, server: my.server.tld, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "my.server.tld"

I got this same error using the default locations, so I copied everything to different folders in order to keep the original installation files in-tact.

cp -R /usr/lib/cgi-bin/nagios3 /var/www/cgi-bin

cp -R /usr/share/nagios3/htdocs /var/www/html

This is what I have set in /etc/nginx/sites-available/default

server {

        listen   80;

        server_name  my.domain.tld;

        access_log  /var/log/nginx/my.domain.tld.access.log;
        error_log   /var/log/nginx/my.domain.tld.error.log;

        index index.php index.html index.htm;

        location / {
                root /var/www/html/;
                auth_basic "Restricted";
                auth_basic_user_file /etc/nagios3/.htpasswd-users;
        }

        # Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                fastcgi_pass   localhost:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_intercept_errors on;
        }

        # FastCGI Wrapper
        location /cgi-bin/ {
                gzip off;
                root  /var/www/;
                fastcgi_pass  unix:/var/run/fcgiwrap.socket;
                include /etc/nginx/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        }

}

This is what I have set in my /etc/nagios3/cgi.cfg

physical_html_path=/var/www/html
url_html_path=/

This is what I'm running, PHP wise:

PHP 5.3.16-1~dotdeb.0 with Suhosin-Patch (cli) (built: Aug 17 2012 22:03:18)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with the ionCube PHP Loader v4.2.2, Copyright (c) 2002-2012, by ionCube Ltd.
    with Suhosin v0.9.33, Copyright (c) 2007-2012, by SektionEins GmbH

Any ideas on what would cause this and/or how to fix it?

Best Answer

To clarify ErikA's answer:

It is saying "Primary script unknown" because NGiNX does not support running CGI directly. A helper program must be installed to process the CGI files and return the output to NGiNX.

This is why I had installed fcgiwrap, which processes the CGI files and passes the output to PHP-FPM, which is then processed by NGiNX, AFAIUI.

Although I thought I had started the init.d script for fcgiwrap, I had not.

I was able to determine this by running netstat -alnp | grep cgi, which returned no output until I started the script; /etc/init.d/fcgiwrap start

After I started it, I ran netstat -alnp | grep cgi again, and got the following output:

unix  2      [ ACC ]     STREAM     LISTENING     18955    11251/fcgiwrap      /var/run/fcgiwrap.socket

Once the script was running, and CGI was working, I no longer received this error, and Nagios worked.

Yay for migraines that attribute to cloudy thinking and stupid little mistakes!