現在apache用得越來越少了,大家都改用nginx。但有些東西還是比較依賴apache,如nagios。

想讓nagios在nginx上運行,必需先讓nginx支持perl和cgi解析的功能,需要用到fcgi-perl,安裝方式參見我的上一篇blog:

在nginx上配置使用bugzilla

在這里就不多說了。

現在帖上perl的fast-cgi起來后,nginx的配置:

   server {
        listen       80;
        server_name  monitor.xxxx.com;

        root   /data1/www/monitor.xxxx.com;
        index  index.php index.html index.htm;

        access_log /data1/app/log/nginx/monitor.xxxx.com.log  combined;
        error_log  /data1/app/log/nginx/error-monitor.xxxx.com.log notice;

        allow 10.0.0.0/8;
        deny all;

        location ~ \.php$ {

            root  /data1/www/monitor.xxxx.com;

            fastcgi_pass   unix:/data1/app/tmp/php-cgi.sock;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }

        location /nagios/ {

            alias /usr/share/nagios/;
            index index.html index.htm index.php;

            auth_basic "Nagios Access";
            auth_basic_user_file htpasswd.users;

            location ~ \.php$ {
                root /usr/share;
                fastcgi_pass   unix:/data1/app/tmp/php-cgi.sock;
                fastcgi_index  index.php;
                include        fastcgi.conf;
            }

        }

        location ~ .*\.(pl|cgi)$ {
            rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;

            auth_basic "Nagios Access";
            auth_basic_user_file htpasswd.users;

            gzip off;
            include fastcgi_params;
            fastcgi_pass  127.0.0.1:8999;
            fastcgi_index index.cgi;
            fastcgi_param SCRIPT_FILENAME  /usr/lib64/nagios/cgi$fastcgi_script_name;
            fastcgi_param AUTH_USER $remote_user;
            fastcgi_param REMOTE_USER $remote_user;

        }


   }

 

然后重新生成認證文件htpasswd.users放在nginx的conf目錄。重啟nginx服務便可。

生成認證文件使用:

htpasswd -c htpasswd.users nagiosadmin

特別注意下面兩個參數,一定要加上:

            fastcgi_param AUTH_USER $remote_user;
            fastcgi_param REMOTE_USER $remote_user;

否則進入nagios會提示沒有認證。