當前位置:首頁 » 編程軟體 » nginx監控腳本

nginx監控腳本

發布時間: 2022-06-20 01:32:24

㈠ 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
它會顯示活動的連接和響應的次數,還有其他更多的統計選項這里就不再列舉了,感興趣的話可以問度娘查閱更多資料

熱點內容
電腦我的世界伺服器游戲幣 發布:2025-05-16 05:27:25 瀏覽:487
索尼手機為什麼不能用安卓10 發布:2025-05-16 05:18:46 瀏覽:784
蔚來es6選擇哪些配置實用 發布:2025-05-16 05:18:05 瀏覽:130
小米如何掃碼wifi密碼 發布:2025-05-16 05:13:38 瀏覽:807
樓層密碼是什麼意思 發布:2025-05-16 05:13:37 瀏覽:13
創建文件夾失敗 發布:2025-05-16 05:12:59 瀏覽:396
電腦上如何查詢自己的配置 發布:2025-05-16 05:06:36 瀏覽:105
sql中去重 發布:2025-05-16 04:55:06 瀏覽:893
dwr上傳圖片 發布:2025-05-16 04:49:46 瀏覽:122
base64加密的圖片 發布:2025-05-16 04:35:46 瀏覽:356