Nginx服務構建及訪問狀態統計

Nginx專爲性能優化而開發,其最知名的有點是它的穩定性和低系統資源消耗,以及對HTTP併發連接的高處理能力(單臺物理服務器可支持30000~50000個併發請求)。正因如此,大量提供社交網絡、新聞資訊、電子商務及虛擬主機等服務的企業紛紛選擇Nginx來提供Web服務。

Nginx構建環境:

RHEL6-5(IP地址爲192.168.100.110)

一.構建步驟:

1. 安裝依賴包及編譯環境:

# yum -y install pcre-devel zlib-devel gcc gcc-c++

2. 創建管理用戶:

# useradd -M -s /sbin/nologin nginx     //-M表示不讓nginx在本地創建家目錄,同時也禁止登錄到shell環境

3. 解壓準備好的安裝包:

# tar xzvf nginx-1.6.0.tar.gz -C /opt

4. 在configure所在目錄下配置編譯安裝

# cd /opt/nginx-1.6.0/

./configure \
--prefix=/usr/local/nginx \ //安裝目錄
--user=nginx \ //指定用戶
--group=nginx \ //指定組
--with-http_stub_status_module //開啓stub_status狀態統計模塊

#make && make install
#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/     //讓系統識別nginx命令

5. 檢查、啓動、重啓、停止nginix

# nginx -t         //檢查
# nginx            //啓動
# killall -1 nginx     //重啓
# killall -3 nginx     //停止

6. 使用Nginx服務腳本

# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
    $PROG
    ;;
  stop)
    kill -s QUIT $(cat $PIDF)
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  reload)
    kill -s HUP $(cat $PIDF)
    ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
exit 0
# chmod +x /etc/init.d/nginx        //給予執行權限
# chkconfig --add nginx         //添加nginx服務項

二.構建統計頁面

# cd /usr/local/nginx/conf          
# mv nginx.conf nginx.conf.back         //創建nginx的副本文件
# grep -v "#" nginx.conf.back > nginx.conf      //去除#註釋內容,方便管理
# vi nginx.conf         //編輯nginx主配置文件
server {
        listen       80;
        server_name  localhost;
    charset utf-8;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~ /status {            //訪問位置爲/status
        stub_status   on;               //打開狀態統計功能
        access_log off;             //關閉此位置的日誌記錄
        }                           //在"server"這裏插入的這4行的

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       }
}

Nginx服務構建及訪問狀態統計
Nginx服務構建及訪問狀態統計

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章