3,上文理解了,就好辦了。我們的目的就是讓nginx支持執行我們nagios下的cgi。nginx基于安全性等考慮不讓直接執行cgi,但支持fastcgi,所以我們要用到一個fastcig的warp來封裝cgi
github上開源的項目
fcgiwarp https://github.com/gnosek/fcgiwrap
git clone https://github.com/gnosek/fcgiwrap.git
autoreconf -i
./configure
make
make instal
ps:
如果aotoreconf執行不了,請自行安裝autoreconf。
然后就是怎么使用fcgiwarp ,作者提到了2種使用方法(針對這2種方法在nginx配置稍微不同):
usage
Most probably you will want fcgiwrap be launched by www-servers/spawn-fcgi. Or you could use the author's Perl launcher - see the
homepage for that.
第1種是作者自己寫的perl 的啟動器:作者說在他的
主頁呢。。。。copy一下放在下面
#!/usr/bin/perl
use strict;
use warnings FATAL => qw( all );
use IO::Socket::UNIX;
my $bin_path = '/usr/local/bin/fcgiwrap';
my $socket_path = $ARGV[0] || '/tmp/cgi.sock';
my $num_children = $ARGV[1] || 1;
close STDIN;
unlink $socket_path;
my $socket = IO::Socket::UNIX->new(
Local => $socket_path,
Listen => 100,
);
die "Cannot create socket at $socket_path: $!\n" unless $socket;
for (1 .. $num_children) {
my $pid = fork;
die "Cannot fork: $!" unless defined $pid;
next if $pid;
exec $bin_path;
die "Failed to exec $bin_path: $!\n";
}
我們把這個文件保存成 /etc/init.d/fcgiwrap 做成服務執行即可。
第二種方法是用fastcgi的
進程管理器來啟動。
還是GitHub上開源的牛逼項目spawn-fcgi https://github.com/lighttpd/spawn-fcgi
我們寫一個啟動腳本
spawn-fcgi -f /usr/local/sbin/fcgiwrap -p 9009 這個端口自己根據機器的端口使用情況自己來寫
最后貼一個nginx簡單的配置。
vim /etc/nginx/conf.d/nagios.conf #根據自己的nginx啟動位置自行調整
server {
server_name nagios.tony.com; #自己的域名
access_log /var/log/nginx/nagios-access.log;
error_log /var/log/nginx/nagios-error.log; #日志位置,發現nagios不能在瀏覽器展示,請看日志,看日志,
# auth_basic "Private";
# auth_basic_user_file /etc/nagios/htpasswd.users; #把認證先去掉。跑起來在說在。 要把 /usr/local/nagios/etc/cgi.cfg 中的use_ssl_authentication=0
root /usr/local/nagios/share; #/usr/local/nagios nagios安裝目錄
index index.php index.html;
#php 的配置,請自行去解決。
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000; #php-fpm
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
location /nagios {
alias /usr/local/nagios/share;
}
location ~ \.cgi$ {
root /usr/local/nagios/sbin;
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/local/nagios/sbin/$fastcgi_script_name;
fastcgi_pass unix:/tmp/cgi.sock; #這是上面第一種方式的配置。
#fastcgi_pass 127.0.0.1:9009; #這是上面第二種方式的配置。
}
}