監控nginx狀態腳本
1. 如何查看nginx的運行狀態
查看nginx的運行狀態具體操作步驟如下:
以win7系統電腦為例:
1、首先打開電腦,點擊選擇左下角「開始」圖標按鈕。
2. 請問怎樣用shell實現nginx日誌2xx請求最大值和最小值監控,我剛接觸腳本,不太懂,請大神解惑,謝謝
其實蠻簡單的,就是從文件中取出需要的值,判斷一下就可以了
你給個詳細的一行數據出來,看下你到底要取哪幾項數據
3. najios 怎麼監控nginx日誌
1.在被控端執行操作:把check_nginx插件放進/usr/local/nagios/libexec,並且授權屬主和屬組為nagios(這點非常關鍵)
[root@Jiechao libexec]# ll check_nginx
-rwxr-xr-x 1 nagios nagios 7636 Oct 23 22:48 check_nginx
2.vi /usr/local/nagios/etc/nrpe.cfg
添加這行:command[check_nginx]=/usr/local/nagios/libexec/check_nginx -w 15000 -c 20000
3.重啟Nrpe:
killall -9 nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
4.被控端執行操作測試:
[root@Jiechao libexec]# ./check_nginx -H 192.168.0.100 -P 80 -p /var/run/ -n nginx.pid -s nginx_status -o /tmp/ -w 15000 -c 20000
OK - nginx is running. 1 requests per second, 1 connections per second (1.00 requests per connection) | 'reqpsec'=1 'conpsec'=1 'conpreq'=1.00 ]
--------------------------------------------------------------------------------------------------------------------------------------------------
下面在Nagios Server端操作:
1.vi /usr/local/nagios/etc/objects/service.cfg
添加:
define service{
use generic-service
host_name Nagios-server,Nagios-client
service_description check_nginx
check_command check_nrpe!check_nginx
max_check_attempts 3
normal_check_interval 3
retry_check_interval 3
check_period 24x7
notification_interval 5
notification_period 24x7
notification_options w,u,c,r
contact_groups admins
process_perf_data 1
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
}
添加完,保存退出,重啟Nagios即可。
/etc/init.d/nagios relaod
/etc/init.d/nagios restart
2.測試:/usr/local/nagios/libexec/check_nrpe -H 192.168.0.100 -c check_nginx
OK - nginx is running. 1 requests per second, 1 connections per second (1.00 requests per connection) | 'reqpsec'=1 'conpsec'=1 'conpreq'=1.00 ]
#---------------------192.168.0.100是我被控端的ip地址------------------------------------------------------
下面是一些注意的地方--
1、關於nginx.pid
--在nginx.conf一定要加上,如:pid /opt/nginx.pid
2、如果出現以下提示.
--UNKNOWN - Local /copies of nginx_status is empty.
--可能就是在你的nginx.conf沒有配置狀態監控,如
server
{
listen 80;
server_name IP | 域名;
location /nginx_status {
stub_status on;
access_log off;
}
}
3.需要更改check_nginx 一個地方:
把hostname="localhost"改為:你需要監控的主機ip。
如:hostname="192.168.0.100"
check_nginx 腳本
#!/bin/sh
PROGNAME=`basename $0`
VERSION="Version 1.0,"
AUTHOR="2009, Mike Adolphs (http://www.matejunkie.com/)"
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
hostname="192.168.0.100"
port=80
path_pid=/var/run
name_pid="nginx.pid"
status_page="nginx_status"
output_dir=/tmp
pid_check=1
secure=0
print_version() {
echo "$VERSION $AUTHOR"
}
print_help() {
print_version $PROGNAME $VERSION
echo ""
echo "$PROGNAME is a Nagios plugin to check whether nginx is running."
echo "It also parses the nginx's status page to get requests and"
echo "connections per second as well as requests per connection. You"
echo "may have to alter your nginx configuration so that the plugin"
echo "can access the server's status page."
echo "The plugin is highly configurable for this reason. See below for"
echo "available options."
echo ""
echo "$PROGNAME -H localhost -P 80 -p /var/run -n nginx.pid "
echo " -s nginx_statut -o /tmp [-w INT] [-c INT] [-S] [-N]"
echo ""
echo "Options:"
echo " -H/--hostname)"
echo " Defines the hostname. Default is: localhost"
echo " -P/--port)"
echo " Defines the port. Default is: 80"
echo " -p/--path-pid)"
echo " Path where nginx's pid file is being stored. You might need"
echo " to alter this path according to your distribution. Default"
echo " is: /var/run"
echo " -n/--name_pid)"
echo " Name of the pid file. Default is: nginx.pid"
echo " -N/--no-pid-check)"
echo " Turn this on, if you don't want to check for a pid file"
echo " whether nginx is running, e.g. when you're checking a"
echo " remote server. Default is: off"
echo " -s/--status-page)"
echo " Name of the server's status page defined in the location"
echo " directive of your nginx configuration. Default is:"
echo " nginx_status"
echo " -o/--output-directory)"
echo " Specifies where to write the tmp-file that the check creates."
echo " Default is: /tmp"
echo " -S/--secure)"
echo " In case your server is only reachable via SSL, use this"
echo " this switch to use HTTPS instead of HTTP. Default is: off"
echo " -w/--warning)"
echo " Sets a warning level for requests per second. Default is: off"
echo " -c/--critical)"
echo " Sets a critical level for requests per second. Default is:"
echo " off"
exit $ST_UK
}
while test -n "$1"; do
case "$1" in
-help|-h)
print_help
exit $ST_UK
;;
--version|-v)
print_version $PROGNAME $VERSION
exit $ST_UK
;;
--hostname|-H)
hostname=$2
shift
;;
--port|-P)
port=$2
shift
;;
--path-pid|-p)
path_pid=$2
shift
;;
--name-pid|-n)
name_pid=$2
shift
;;
--no-pid-check|-N)
pid_check=0
;;
--status-page|-s)
status_page=$2
shift
;;
--output-directory|-o)
output_dir=$2
shift
;;
--secure|-S)
secure=1
;;
--warning|-w)
warning=$2
shift
;;
--critical|-c)
critical=$2
shift
;;
*)
echo "Unknown argument: $1"
print_help
exit $ST_UK
;;
esac
shift
done
get_wcdiff() {
if [ ! -z "$warning" -a ! -z "$critical" ]
then
wclvls=1
if [ ${warning} -gt ${critical} ]
then
wcdiff=1
fi
elif [ ! -z "$warning" -a -z "$critical" ]
then
wcdiff=2
elif [ -z "$warning" -a ! -z "$critical" ]
then
wcdiff=3
fi
}
val_wcdiff() {
if [ "$wcdiff" = 1 ]
then
echo "Please adjust your warning/critical thresholds. The warning \
must be lower than the critical level!"
exit $ST_UK
elif [ "$wcdiff" = 2 ]
then
echo "Please also set a critical value when you want to use \
warning/critical thresholds!"
exit $ST_UK
elif [ "$wcdiff" = 3 ]
then
echo "Please also set a warning value when you want to use \
warning/critical thresholds!"
exit $ST_UK
fi
}
check_pid() {
if [ -f "$path_pid/$name_pid" ]
then
retval=0
else
retval=1
fi
}
get_status() {
if [ "$secure" = 1 ]
then
wget --no-check-certificate -q -t 3 -T 3 \
http://${hostname}:${port}/${status_page} -O ${output_dir}/nginx-status.1
sleep 1
wget --no-check-certificate -q -t 3 -T 3 \
http://${hostname}:${port}/${status_page} -O ${output_dir}/nginx-status.2
else
wget -q -t 3 -T 3 http://${hostname}:${port}/${status_page} \
-O ${output_dir}/nginx-status.1
sleep 1
wget -q -t 3 -T 3 http://${hostname}:${port}/${status_page} \
-O ${output_dir}/nginx-status.2
fi
stat_output1=`stat -c %s ${output_dir}/nginx-status.1`
stat_output2=`stat -c %s ${output_dir}/nginx-status.2`
if [ "$stat_output1" = 0 -o "$stat_output2" = 0 ]
then
echo "UNKNOWN - Local /copies of $status_page is empty."
exit $ST_UK
fi
}
get_vals() {
tmp1_reqpsec=`grep '^ ' ${output_dir}/nginx-status.1|awk '{print $3}'`
tmp2_reqpsec=`grep '^ ' ${output_dir}/nginx-status.2|awk '{print $3}'`
reqpsec=`expr $tmp2_reqpsec - $tmp1_reqpsec`
tmp1_conpsec=`grep '^ ' ${output_dir}/nginx-status.1|awk '{print $2}'`
tmp2_conpsec=`grep '^ ' ${output_dir}/nginx-status.2|awk '{print $2}'`
conpsec=`expr $tmp2_conpsec - $tmp1_conpsec`
reqpcon=`echo "scale=2; $reqpsec / $conpsec" | bc -l`
if [ "$reqpcon" = ".99" ]
then
reqpcon="1.00"
fi
}
do_output() {
output="nginx is running. $reqpsec requests per second, $conpsec \
connections per second ($reqpcon requests per connection)"
}
do_perfdata() {
perfdata="'reqpsec'=$reqpsec 'conpsec'=$conpsec 'conpreq'=$reqpcon"
}
# Here we go!
get_wcdiff
val_wcdiff
if [ ${pid_check} = 1 ]
then
check_pid
if [ "$retval" = 1 ]
then
echo "There's no pid file for nginx. Is nginx running? Please \
also make sure whether your pid path and name is correct."
exit $ST_CR
fi
fi
get_status
get_vals
do_output
do_perfdata
if [ -n "$warning" -a -n "$critical" ]
then
if [ "$reqpsec" -ge "$warning" -a "$reqpsec" -lt "$critical" ]
then
echo "WARNING - ${output} | ${perfdata}"
exit $ST_WR
elif [ "$reqpsec" -ge "$critical" ]
then
echo "CRITICAL - ${output} | ${perfdata}"
exit $ST_CR
else
echo "OK - ${output} | ${perfdata} ]"
exit $ST_OK
fi
else
echo "OK - ${output} | ${perfdata}"
exit $ST_OK
fi
4. zabbix怎麼監控nginx
實現監控需要三個步驟:
1、自己創建或是導入模版。<附件>
2、nginx需要配置status。如:
java">server{
listen80;
server_namexxx.xxx.xxx.xxx;
indexindex.htmllogin.jsp;
root/www/freetrade;
access_logoff;
error_logoff;
location/nginx{
stub_statuson;
access_logoff;
allow127.0.0.1;
allowxxx.xxx.xxx.xxx;
denyall;
}
}
3、改客戶端配置文件,使用腳本。
在客戶端機器上任意位置放這個腳本,不過還是建議規范的放在一個地方。
#!/bin/bash
#
#Author:[email protected]
#License:GPLv2
#SetVariables
HOST=`/sbin/ifconfigeth0|sed-n'/inet/{s/.*addr://;s/.*//;p}'`
PORT="80"
#Functionstoreturnnginxstats
functionactive{
/usr/bin/curl"http://$HOST:$PORT/nginx"2>/dev/null|grep'Active'|awk'{print$NF}'
}
functionreading{
/usr/bin/curl"http://$HOST:$PORT/nginx"2>/dev/null|grep'Reading'|awk'{print$2}'
}
functionwriting{
/usr/bin/curl"http://$HOST:$PORT/nginx"2>/dev/null|grep'Writing'|awk'{print$4}'
}
functionwaiting{
/usr/bin/curl"http://$HOST:$PORT/nginx"2>/dev/null|grep'Waiting'|awk'{print$6}'
}
functionaccepts{
/usr/bin/curl"http://$HOST:$PORT/nginx"2>/dev/null|awkNR==3|awk'{print$1}'
}
functionhandled{
/usr/bin/curl"http://$HOST:$PORT/nginx"2>/dev/null|awkNR==3|awk'{print$2}'
}
functionrequests{
/usr/bin/curl"http://$HOST:$PORT/nginx"2>/dev/null|awkNR==3|awk'{print$3}'
}
#Runtherequestedfunction
$1
修改客戶端/etc/zabbix/zabbix_agentd.conf 環境不同,文件位置不同。
#monitornginx
UserParameter=nginx.accepts,/etc/zabbix/scripts/nginx_statusaccepts
UserParameter=nginx.handled,/etc/zabbix/scripts/nginx_statushandled
UserParameter=nginx.requests,/etc/zabbix/scripts/nginx_statusrequests
UserParameter=nginx.connections.active,/etc/zabbix/scripts/nginx_statusactive
UserParameter=nginx.connections.reading,/etc/zabbix/scripts/nginx_statusreading
UserParameter=nginx.connections.writing,/etc/zabbix/scripts/nginx_statuswriting
UserParameter=nginx.connections.waiting,/etc/zabbix/scripts/nginx_statuswaiting
5. 檢查NGINX的conf目錄,如果添加或刪除了目錄或文件,自動reload nginx腳本,有哪位大俠會
linux系統下可以使用 Inotify,監控nginx conf目錄及下面的文件變動,一旦文件變動,系統會立即獲知,然後運行 nginx -s reload重新載入腳本就可以了。
inotify參照這篇文章:
http://www.infoq.com/cn/articles/inotify-linux-file-system-event-monitoring
6. yum安裝的nginx怎麼使用zabbix
安裝操作系統
系統安裝很簡單,省略了。
操作系統:CentOS release 6.4 (Final)
IP地址:192.168.250.249
zabbix版本:i686-2.4.4-1.el6
可以參考這個Cacti安裝視頻里的centos安裝部分。
建議關閉SELinux
SELinux是linux一項很嚴格的安全措施,其設置比較復雜,如果您不會設置,建議您參考下面的方法將其關閉。
可以通過下面命令檢測其是否處於開啟狀態,Enforcing為開啟狀態,Permissive為關閉狀態。
1
getenforce
命令行執行下面命令後SELinux立即關閉,操作系統重啟後會被重新打開。
1
setenforce 0
編輯/etc/selinux/config配置文件,將SELINUX項更改,重啟後會按照此配置文件中設定的狀態決定是否啟動。
1
SELINUX=disabled
添加zabbix倉庫
首先登錄到centos上去。
添加zabbix倉庫,其中地址部分請參考zabbix下載頁中的地址,以便獲取最新版本。
1
rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm
安裝zabbix
如果是安裝zabbix伺服器,安裝下面兩個包,zabbix等需要安裝的包會作為依賴被安裝。Apache和php也會被安裝。
1
yum install zabbix-server-mysql zabbix-web-mysql
如果只是安裝zabbix agent,則只需要安裝agent即可。
1
yum install zabbix-agent
安裝MySQL
同樣是使用yum安裝。
1
2
yum groupinstall mysql
service mysql start
創建MySQL資料庫及用戶和許可權
下面是創建了一個zabbix資料庫,一個zabbix用戶,其密碼為zabbix。zabbix用戶在本地對zabbix資料庫擁有所有許可權。
1
2
3
4
shell> mysql -uroot -p
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';
mysql> flush privileges;
初始化zabbix資料庫
之前安裝的zabbix-server-mysql為我們提供了資料庫初始化腳本。
這些sql腳本位於/usr/share/doc/zabbix-server-mysql-2.4.4/create/目錄,不同的版本號需要注意目錄中的版本號部分。
通過下面方式導入數據中。
1
2
3
4
shell> cd /usr/share/doc/zabbix-server-mysql-2.4.4/create/
shell> mysql -uroot zabbix < schema.sql
shell> mysql -uroot zabbix < images.sql
shell> mysql -uroot zabbix < data.sql
資料庫初始化成功之後還要編輯zabbix的配置文件使其能訪問資料庫。
vim /etc/zabbix/zabbix_server.conf
大概在65行到100行中,有資料庫信息,按照之前設置的用戶名和密碼設置即可。
參考下面圖片。
啟動zabbix server
1
2
service zabbix-server start
service httpd start
PHP時區設置
關於zabbix的Apache和php配置文件位於:/etc/httpd/conf.d/zabbix.conf
請修改此文件,將date.timezone設置為Asia/Shanghai。
參考下面最後一行。
1
2
3
4
5
6
php_value max_execution_time 300
php_value memory_limit 128M
php_value post_max_size 16M
php_value upload_max_filesize 2M
php_value max_input_time 300
php_value date.timezone Asia/Shanghai
這個值位於此文件的第18行。
訪問zabbix
zabbix的訪問地址為:http://192.168.250.249/zabbix
如果您無法訪問,您還需要檢查您的iptables配置。
如果您不知道如何設置iptables,可以將iptables關閉(不建議)。
1
2
service iptables stop
chkconfig iptables off
使用向導設置zabbix
第一次訪問會進入設置向導,根據提示點擊『next』即可。
到這個界面的時候,需要填寫之前設置的zabbix資料庫信息。填寫後點擊『Test Connection』測試連接是否成功。
第四步需要填寫Host和Port,如果您不知道這是什麼意思,請保持默認,Name請自定義填寫。
登錄到Zabbix
使用下面默認的用戶名和密碼登錄即可,注意大小寫。
Admin\zabbix
更改顯示語言
點擊右上角Profile,更改Language選項。
到這里Zabbix Server就安裝結束了。
7. nginx日誌切割腳本怎麼運行
第一步就是重命名日誌文件,不用擔心重命名後nginx找不到日誌文件而丟失日誌。在你未重新打開原名字的日誌文件前,nginx還是會向你重命名的文件寫日誌,linux是靠文件描述符而不是文件名定位文件。
第二步向nginx主進程發送USR1信號。
nginx主進程接到信號後會從配置文件中讀取日誌文件名稱,重新打開日誌文件(以配置文件中的日誌名稱命名),並以工作進程的用戶作為日誌文件的所有者。
重新打開日誌文件後,nginx主進程會關閉重名的日誌文件並通知工作進程使用新打開的日誌文件。
工作進程立刻打開新的日誌文件並關閉重名名的日誌文件。
然後你就可以處理舊的日誌文件了。
二、腳本實現
nginx日誌按日期自動切割腳本如下:
復制代碼代碼如下:
#nginx日誌切割腳本
#!/bin/bash
#設置日誌文件存放目錄
logs_path="/usr/local/nginx/logs/"
#設置pid文件
pid_path="/usr/local/nginx/nginx.pid"
#重命名日誌文件
mv
${logs_path}access.log
${logs_path}access_$(date
-d
"yesterday"
+"%Y%m%d").log
#向nginx主進程發信號重新打開日誌
kill
-USR1
`cat
${pid_path}`
保存以上腳本nginx_log.sh,並設置定時切割任務
8. 怎麼檢查nginx伺服器安裝好
要編譯安裝Nginx,首先我們要安裝依賴包 pcre-devel 和 zlib-devel:
# yum install pcre-devel zlib-devel -y
程序默認是使用 nobody 身份運行的,我們建議使用 nginx 用戶來運行,首先添加Nginx組和用戶,不創建家目錄,不允許登陸系統
# groupadd nginx
# useradd -M -s /sbin/nologin -g nginx nginx
2
准備工作完成後就是下載編譯安裝Nginx了,可以從我提供的網盤下載,也可以去Nginx的官網下載。
首先解壓源碼包:
# tar xf nginx-1.4.4.tar.gz
然後 cd 到解壓後的目錄就可以執行 ./configure 了
# cd nginx-1.4.4
指定安裝目錄和運行時用的屬主和屬組,並啟用狀態監控模塊等
# ./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_mole \
--with-http_flv_mole \
--with-http_stub_status_mole \
--with-http_gzip_static_mole \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
等配置完成後就可以 make && make install 了
# make && make install
# mkdir /var/tmp/nginx/client/ -pv
3
等編譯安裝完成後在 /usr/local 下就會出現 Nginx 這個目錄了,進入這個目錄後發現目錄非常簡單。它的配置文件存放在 conf 目錄中,網頁文件存放在 html 中,日誌文件存放在 logs 中,sbin 目錄下只有一個可執行程序 "nginx"
接下來我們簡單的為它提供一個服務腳本吧!
# vim /etc/init.d/nginx
新建文件/etc/rc.d/init.d/nginx,內容如下:
#!/bin/bash
# chkconfig:235 85 15
# description: Nginx is an HTTP server
. /etc/rc.d/init.d/functions
start() {
echo "Start..."
/usr/local/nginx/sbin/nginx &> /dev/null
if [ $? -eq 0 ];then
echo "Start successful!"
else
echo "Start failed!"
fi
}
stop() {
if killproc nginx -QUIT ;then
echo "Stopping..."
fi
}
restart() {
stop
sleep 1
start
}
reload() {
killproc nginx -HUP
echo "Reloading..."
}
configtest() {
/usr/local/nginx/sbin/nginx -t
}
case $1 in
start)
start ;;
stop)
stop ;;
restart)
restart ;;
reload)
reload ;;
configtest)
configtest ;;
*)
echo "Usage: nginx {start|stop|restart|reload|configtest}"
;;
esac
之後給這個文件可執行許可權:
# chmod +x /etc/init.d/nginx
好了,現在可以使用 start,stop 這些參數控制Nginx服務了
由於腳本是我自己寫的,還有許多不盡人意的地方,歡迎大家修改和完善!
現在我們就試試啟動服務看看效果吧:
# service nginx start
記得關閉 SElinux 和 iptables 防火牆哦,
# service iptables stop
# setenforce 0
接下來就在瀏覽器中訪問你服務的IP看看效果吧!是不是出項了歡迎的字樣呢
接下來就研究下 Nginx 的配置文件吧!
# vim /usr/local/nginx/conf/nginx.conf
各項參數的意義如下:
worker_processes 1; 工作進程數量
error_log logs/error.log; 日誌文件位置
pid logs/nginx.pid; pid文件位置
worker_connections 1024; 沒進程的連接數
listen 80; 監聽埠
server_name localhost; 主機名
root html; 網站根目錄
index index.html index.htm; 網站索引頁
error_page 500 502 503 504 /50x.html; 訪問錯誤頁面
剩下的其他被注釋掉的代碼塊:
location ~ \.php$ { . . . . . . } 對PHP的支持,需要安裝PHP
server { . . . . . . } 添加server代碼塊能添加虛擬主機
剩下還有監聽443埠的超文本傳輸安全協議 HTTPS server 需要在編譯Nginx時添加ssl的支持
接下來我們試著添加一台虛擬主機吧,虛擬主機的添加可以基於埠,可以基於IP,也可以基於主機名,我們挨個來看看:
基於埠:
首先編輯配置文件,添加server代碼塊,記得要寫到http{ . . . . . . }這個大的代碼塊中。
server {
listen 8080;
server_name localhost;
location / {
root /var/www/html;
index index.html index.htm;
}
}
這樣就添加了一個監聽8080埠的服務,你也可以定義自己喜歡的埠哦。
接下來檢查下配置文件有沒有問題,如果最後一個單詞顯示successful就代表沒問題了,可以重新啟動Nginx了
# service nginx configtest
# service nginx restart
接下來就給第二個虛擬主機寫一個index吧!首先創建目錄
# mkdir -pv /var/www/html
# echo '<h1>Hi! This is 8080!</h1>' > /var/www/html/index.html
好了 接下來試著在瀏覽器中訪問訪問,記得第二個主機要加上埠訪問哦
現在試著用不同的IP建立虛擬主機吧!我們可以在一塊網卡上綁定多個IP地址的方式來實現
# ifconfig eth0:0 10.0.0.4/8
記得把IP換成你自己的哦!然後ifconfig看看是不是多出來一個網卡IP了呢
讓後繼續修改配置文件,這回要修改兩個地方,一個是原本自帶的站點的 listen 項,一個是自己添加的站點的 listen 項。
基於IP:
server {
listen 10.0.0.3:80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 10.0.0.4:80;
server_name localhost;
location / {
root /var/www/html;
index index.html index.htm;
}
}
讓他們只監聽不同的IP,用相同的埠
接下來再瀏覽器上用不同的IP來訪問試試吧,及的還得重啟Nginx,先檢查一下,出現錯誤了看看哪裡配置的不對,然後就可以重啟了。
# service nginx congiftest
# service nginx restart
如果配置給網卡的第二個IP不想要了,把它停掉就可以了
# ifconfig eth0:0 down
再 ifconfig 看看是不是沒有了呢
現在試試用不同的主機名吧!也是企業用的最多的方式。我們把兩個站點的listen項都改為80,然後修改service_name項為定義的主機名
基於主機名:
server {
listen 80;
server_name ybmq.com;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name zhzz.com;
location / {
root /var/www/html;
index index.html index.htm;
}
}
然後重啟Nginx吧!
可是我們在瀏覽器上怎麼通過域名訪問呢?要知道我們訪問 啊,qq 啊之類的是通過DNS伺服器的,難道我們還要配置一台DNS伺服器?其實不然,我們通過修改客戶機的 hosts 文件就可以了。hosts文件是一個本地的域名解析文件,我們要解析哪些域名只要把域名和對應的IP寫到一起就可以了。在Windows XP之後的系統中,這個文件位於:
C:\Windows\System32\drivers\etc\hosts
我們用文本編輯器打開,添加兩個相同的IP對應的兩個不同的主機名就可以了。
如下圖所示
如果你打開這個文件發現已經有很多IP地址了,可以直接在最後加入這兩行,也可以直接清空這個文件,不會有什麼問題的。這個文件的用途還可以屏蔽一些網站哦,只需要把網址對於的IP改為 127.0.0.1 也就是本地回環地址,瀏覽器查詢域名對應的IP時時先通過查詢這個文件的,如果查詢到了,不管對錯都不會訪問DNS伺服器了,所以我們給它一個錯誤的地址,那它一輩子也打不開被屏蔽掉的網站了。
好了 接下來就在瀏覽器中試試用用域名訪問你的兩個站點吧。
如果大家還用IP訪問會是什麼情況呢?我不說了,大家還是自己測試吧 哈哈o(^▽^)o
Nginx的如何新建虛擬主機就到這里了,還記不記得在編譯安裝的時候的
--with-http_stub_status_mole 這個參數?它的作用是啟用狀態統計模塊,下面我們就開啟這個模塊看看吧!
還是編輯Nginx的配置文件,要監控哪一個站點,就在那個站點的server中添加:
location ~ /status {
stub_status on;
access_log off;
}
接著重啟Nginx
我們可以通過在域名或IP後添加 " /status " 來訪問狀態統計:
http:// ybmq.com/status
它會顯示活動的連接和響應的次數,還有其他更多的統計選項這里就不再列舉了,感興趣的話可以問度娘查閱更多資料
9. keepalived 監控腳本不執行 是不是一個bug
在keepalived.conf中的vrrp_script配置相信你已經配置完成了,但是在日誌中看到執行情況始終是類似如下的情況:
chk_nginxnomatch,ignoring...
翻譯過來是「chk_nginx沒有匹配,忽略…」
但是腳本什麼的怎麼檢查都是正確的,單獨運行可以生效,exit 0和exit 1都設置了。
這時候我的做法是換了一台伺服器用同樣的腳本做測試,結果發現腳本運行正常。
具體是什麼原因我一直沒有檢查出來。
10. nginx + keepalived 實現高可用性
網上抄的嗎。
針對上面的腳本
1、這個腳本是針對keepalived監控高可用軟體的存活,進行高可用的切換。
起keeplived服務後,在系統日誌message可以看到keepalived啟動日誌:檢查master,backup權值,檢查監控腳本是否存在,後台起監控腳本,發送arp探針
2、keepalived實現高可用原理:keepalived監控腳本定期檢測(檢測時間間隔需配置)應用存活狀態,若master上的應用宕掉,腳本首先嘗試重啟應用服務;間隔2秒重新檢測,若應用進程還是不存在,kill keepalived進程。
slave端發送apr探針不可達,確認master宕掉。搶占為master,vip漂移到slave主機,slave主機提供業務服務
3、監控腳本master、slave端都需要配置
4、最後一個問題,不知道你想問啥。麻煩描述清楚,清晰