xiahe999 发表于 2019-1-14 07:24:53

在LNMP下用nginx 1.4.7配置nagios监控

  因为网上的教程nginx的版本比较old,所以用新版本的nginx配置教程多数有错误,下面整理,希望能帮到你
  

  LNMP环境已经搭建好,nginx版本是1.4.7,php版本是5.3.17,
  
cat /etc/redhat-release
CentOS release 6.4 (Final)/usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.4.7/usr/local/php/bin/php -v
PHP 5.3.17 (cli) (built: Jan 23 2014 09:47:42)  安装配置nagios见 http://bbotte.blog.运维网.com/6205307/1403997
  yum install perl perl-develfcgi-devel
  在服务端进行配置

  为nginx添加fcgi支持,下载并安装 http://pan.baidu.com/s/1sXpGj
  IO-1.25.tar.gz
  IO-All-0.41.tar.gz
  FCGI-ProcManager-0.18.tar.gz
  FCGI-0.70.tar.gz
  

  安装时候都一样,参考下面
  tar -xzf IO-1.25.tar.gz
  cd IO-1.25
  perl Makefile.PL
make && make install
  

  vim /usr/local/nginx/start_perl_cgi.sh
#!/bin/bash
#set -x
dir=/usr/local/nginx
stop ()
{
#pkill-f$dir/perl-fcgi.pl
kill $(cat $dir/logs/perl-fcgi.pid)
rm $dir/logs/perl-fcgi.pid 2>/dev/null
rm $dir/logs/perl-fcgi.sock 2>/dev/null
echo "stop perl-fcgi done"
}
start ()
{
rm $dir/now_start_perl_fcgi.sh 2>/dev/null
chown nagios.root $dir/logs
echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh
chown nagios.nagcmd $dir/now_start_perl_fcgi.sh
chmod u+x $dir/now_start_perl_fcgi.sh
sudo -u nagios $dir/now_start_perl_fcgi.sh
echo "start perl-fcgi done"
}
case $1 in
stop)
stop
;;
start)
start
;;
restart)
stop
start
;;
esac  

  nginx.conf
server {
listen       80;
server_name192.168.22.240;
root /usr/local/nagios/share;
index index.html index.php;
auth_basic "nagiosadmin";
auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
location ~ .*\.php?$
{
fastcgi_pass    unix:/tmp/php-cgi.sock;
fastcgi_index   index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/share$fastcgi_script_name;
}
location /nagios/
{
gzip off;
alias /usr/local/nagios/share/;
index index.html index.htm index.php;
}
location ~ \.cgi$ {
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_pass unix:/usr/local/nginx/logs/perl-fcgi.sock;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;
include fastcgi_params;
}
}
##下面是其他的web服务器
server
{
listen       80;
server_name abc.com;
index index.html index.htm index.php;
root/data;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_passunix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log   off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires      30d;
}
location ~ .*\.(js|css)?$
{
expires      12h;
}
access_log/home/wwwlogs/access.logaccess;
}  nginx里面/usr/local/nagios/etc/htpasswd.users 的密码生成,yum安装一个httpd,生成一个密码,或者写 nagiosadmin:50QHC5y6Y1r7k用户名是nagiosadmin 密码是123456
  

  执行start_perl_cgi.sh,生成perl-fcgi.sock
/usr/local/nginx # ./start_perl_cgi.sh start  这个脚本会生成一个now_start_perl_fcgi.sh
   ps aux|grep fcgi
  nagios   175770.00.4 1389525000 pts/3    S    17:49   0:00 /usr/bin/perl /usr/local/nginx/perl-fcgi.pl -l /usr/local/nginx/logs/perl-fcgi.log -pid /usr/local/nginx/logs/perl-fcgi.pid -S /usr/local/nginx/logs/perl-fcgi.sock
  

  下面是php-fpm的配置文件

pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice

listen = /tmp/php-cgi.sock
user = www
group = www
#listen = 127.0.0.1:9000
#pm = dynamic
pm = static
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 60  

  如果在nagios页面出现502错误,则修改权限 /usr/local/nginx # chmod 777 logs/perl-fcgi.sock
http://s3.运维网.com/wyfs02/M01/25/54/wKiom1NeJePAO7YXAADi88Kpucw779.jpg
  

  总结:

  php-fpm会生成/tmp/php-cgi.sock (参考上面配置),start_perl_cgi.sh 这个脚本会生成 /usr/local/nginx/logs/perl-fcgi.sock
  nginx处理php页面用php-cgi.sock,nagios/sbin/下面的cgi用perl-fcgi.sock解析,(有些文章添加了.pl支持,也是perl-fcgi.sock解析)
  所以只需要安装FCGI支持就可以的
  

  http://s3.运维网.com/wyfs02/M01/25/54/wKiom1NeJs_yfoJcAAL_TBWNGUM505.jpg
  

  如果出现下面问题
  
  It appears as though you do not have permission to view information for any of the services you requested...

  

If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI
and check the authorization options in your CGI configuration file
  
请把/nagios/etc/cgi.cfg 里面use_authentication=1 改为use_authentication=0,因为nginx里面已经包含认证的文件和密码,



页: [1]
查看完整版本: 在LNMP下用nginx 1.4.7配置nagios监控