nginx监控脚本
㈠ 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、最后一个问题,不知道你想问啥。麻烦描述清楚,清晰
㈡ 检查NGINX的conf目录,如果添加或删除了目录或文件,自动reload nginx脚本,有哪位大侠会
linux系统下可以使用 Inotify,监控nginx conf目录及下面的文件变动,一旦文件变动,系统会立即获知,然后运行 nginx -s reload重新载入脚本就可以了。
inotify参照这篇文章:
http://www.infoq.com/cn/articles/inotify-linux-file-system-event-monitoring
㈢ 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
㈣ 如何查看nginx的运行状态
查看nginx的运行状态具体操作步骤如下:
以win7系统电脑为例:
1、首先打开电脑,点击选择左下角“开始”图标按钮。

㈤ 如何使用 Datadog 监控 NGINX
指标收集:NGINX(开源版)
开源版的 NGINX 会在一个简单的状态页面上显示几个与服务器状态有关的基本指标,它们由你启用的 HTTP stub status mole 所提供。要检查该模块是否已启用,运行以下命令:
nginx -V 2>&1| grep -o with-http_stub_status_mole
如果你看到终端输出了 httpstubstatus_mole,说明该状态模块已启用。
如果该命令没有输出,你需要启用该状态模块。你可以在从源代码构建 NGINX 时使用 --with-http_stub_status_mole 配置参数:
./configure \
… \
--with-http_stub_status_mole
make
sudo make install
在验证该模块已经启用或你自己启用它后,你还需要修改 NGINX 配置文件,来给状态页面设置一个本地可访问的 URL(例如: /nginx_status):
server {
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
注:nginx 配置中的 server 块通常并不放在主配置文件中(例如:/etc/nginx/nginx.conf),而是放在主配置会加载的辅助配置文件中。要找到主配置文件,首先运行以下命令:
nginx -t
打开列出的主配置文件,在以 http 块结尾的附近查找以 include 开头的行,如:
include /etc/nginx/conf.d/*.conf;
在其中一个包含的配置文件中,你应该会找到主 server 块,你可以如上所示配置 NGINX 的指标输出。更改任何配置后,通过执行以下命令重新加载配置文件:
nginx -s reload
现在,你可以浏览状态页看到你的指标:
Active connections:24
server accepts handled requests
115695811569584491319
Reading:0Writing:18Waiting:6
请注意,如果你希望从远程计算机访问该状态页面,则需要将远程计算机的 IP 地址添加到你的状态配置文件的白名单中,在上面的配置文件中的白名单仅有 127.0.0.1。
NGINX 的状态页面是一种快速查看指标状况的简单方法,但当连续监测时,你需要按照标准间隔自动记录该数据。监控工具箱 Nagios 或者 Datadog,以及收集统计信息的服务 collectD 已经可以解析 NGINX 的状态信息了。
㈥ 如何用nginx实时监控接口访问流量
ngx_req_status用来展示nginx请求状态信息,类似于apache的status,nginx自带的模块只能显示连接数等等信息,我们并不能知道到底有哪些请求、以及各url域名所消耗的带宽是多少。ngx_req_status提供了这些功能.
功能特性
按域名、url、ip等等统计信息
统计总流量
统计当前带宽峰值带宽
统计总请求数量
1.安装
#cd/usr/local/src/
#wget"http://nginx.org/download/nginx-1.4.2.tar.gz"
#tar-xzvfnginx-1.4.2.tar.gz
#wgethttps://github.com/zls0424/ngx_req_status/archive/master.zip-Ongx_req_status.zip
#unzipngx_req_status.zip
#cdnginx-1.4.2/
#patch-p1<../ngx_req_status-master/write_filter.patch
#./configure--prefix=/usr/local/nginx-1.4.2--add-mole=../ngx_req_status-master
#make-j2
#makeinstall
2.配置
http{
req_status_zoneserver_name$server_name256k;
req_status_zoneserver_addr$server_addr256k;
req_status_zoneserver_url$server_name$uri256k;
req_statusserver_nameserver_addrserver_url;
server{
server_nametest.ttlsa.com;
location/ttlsa-req-status{
req_status_showon;
}
}
}3.指令
req_status_zone
语法:req_status_zonenamestringsize
默认值:None
配置块:http
定义请求状态ZONE,请求按照string分组来排列,例如:
req_status_zoneserver_url$server_name$uri256k;
域名+uri将会形成一条数据,可以看到所有url的带宽,流量,访问数
req_status
语法:req_statuszone1[zone2]
默认值:None
配置块:http,server,location
在location中启用请求状态,你可以指定更多zones。
req_status_show
语法:req_status_showon
默认值:None
配置块:location
展示数据
㈦ 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
㈧ keepalived 监控脚本不执行 是不是一个bug
在keepalived.conf中的vrrp_script配置相信你已经配置完成了,但是在日志中看到执行情况始终是类似如下的情况:
chk_nginxnomatch,ignoring...
翻译过来是“chk_nginx没有匹配,忽略…”
但是脚本什么的怎么检查都是正确的,单独运行可以生效,exit 0和exit 1都设置了。
这时候我的做法是换了一台服务器用同样的脚本做测试,结果发现脚本运行正常。
具体是什么原因我一直没有检查出来。
㈨ 如何监控nginx 负载是否均衡
Nginx负载均衡与可用性检查
在业界,一直流传这样一句话:Nginx抗并发能力强!为什么Nginx抗并发能力强?原因是使用了非阻塞、异步传输
阻塞:如apache代理tomcat时,apache开启10个进程,同时处理着10个请求,在tomcat没有返回给apache结果时,apache是不会处理用户发出的第11个请求
非阻塞:如nginx代理tomcat时,nginx开启1000个并发,同时处理着1000个请求,在tomcat没有返回给nginx结果时,nginx会依然处理后面用户发给的请求
㈩ 怎么检查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
它会显示活动的连接和响应的次数,还有其他更多的统计选项这里就不再列举了,感兴趣的话可以问度娘查阅更多资料
