监控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、最后一个问题,不知道你想问啥。麻烦描述清楚,清晰