Migrating Nagios from Apache to Nginx

Nagios is powerful monitoring software. And like OTRS in most cases it’s used with Apache. But sometimes you might want to use it with nginx. Here is a simple way to do that. Actually it’s very similar to launching OTRS with nginx – in both ways we use Perl FastCGI wrapper. I assume that you already have Nagios installed and use Fedora 15.  Download Perl FastCGI wrapper and put it to /usr/local/bin/fastcgi-wrapper.pl.  Modify it to use TCP socket:

$socket = FCGI::OpenSocket( "127.0.0.1:9002", 10 ); #use IP sockets

Put this systemd configuration file to /lib/systemd/system/fcgi-perl.service:

[Unit]
Description=fastcgi-wrapper.pl
[Service]
ExecStart=/usr/local/bin/fastcgi-wrapper.pl
Type=forking
User=apache
Group=apache
Restart=always
[Install]
WantedBy=multi-user.target

Enable it:

systemctl enable fcgi-perl.service

For using Nagios web interface you also need to have PHP processes pool, in our case it listens TCP socket 127.0.0.1:9005. So if you don’t have one, you need to launch it (convenient way to do that is to use spawn-fcgi tool).

Add next lines to your nginx server configuration file:

location /nagios {
alias /usr/share/nagios/html;
index index.php;
}

location ~ ^/nagios/(.*.php)$  {
root /usr/share/nagios/html/;
rewrite ^/nagios/(.*) /$1 break;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param  SCRIPT_FILENAME  /usr/share/nagios/html$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9005;
}

location ~ ^/nagios/cgi-bin/
{
root /usr/lib64/nagios/cgi-bin/;
rewrite ^/nagios/cgi-bin/(.*).cgi /$1.cgi break;
include /etc/nginx/fastcgi_params;
fastcgi_param  AUTH_USER $remote_user;
fastcgi_param  REMOTE_USER $remote_user;
fastcgi_param SCRIPT_FILENAME /usr/lib64/nagios/cgi-bin$fastcgi_script_name;
fastcgi_pass   127.0.0.1:9002;

}
service nginx restart

Now your Nagios web interface should be accessible via http://yourdomain.com/nagios