1. 程式人生 > >部署Nginx網站服務實現訪問狀態統計以及訪問控制功能

部署Nginx網站服務實現訪問狀態統計以及訪問控制功能

軟件包 檢查 web 進程 ext control kcon error .org

Nginx專為性能優化而開發,最知名的優點是它的穩定性和低系統資源消耗,以及對HTTP並發連接的高處理能力,單個物理服務器可支持30000-50000個並發請求。

Nginx的安裝文件可以從官方網站http://www.nginx.org/下載,下面以Nginx1.12版本為例,基於CentOS7,部署Nginx網站服務。

  • 安裝Nginx

第一步源碼編譯安裝

1. 安裝支持軟件

Nginx的配置及運行需要gcc 、 gcc-c++ 、 make 、 pcre、pcre-devel、zlib-devel軟件包的支持,以便提供相應的庫和頭文件,確保Nginx安裝順利。

創建yum倉庫的步驟詳細步驟請參考 Linux下通過rdesktop遠程登陸Windows系統

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

如果是在有網絡的情況下,CentOS7無需創建yum倉庫,直接執行yum list命令更新一下yum源,稍微等待一會兒。

 yum list    //更新yum源
 yum install gcc gcc-c++ make pcre pcre-devel zlib-devel -y

2. 創建運行用戶、組

Nginx服務程序默認以nobody身份運行,建議為其創建專門的用戶賬號,以便更準確的控制其訪問權限,增加靈活性,降低安全風險。

useradd -M -s /sbin/nologin nginx    //創建一個名為nginx用戶,不建立宿主文件夾,禁止登錄到shell環境

3. 編譯安裝

tar xzvf nginx-1.12.0.tar.gz -C /opt  //解壓Nginx軟件至opt目錄下
cd /opt/nginx-1.12.0/  //切換到Nginx目錄下

根據實際需要配置Nginx的具體選項,配置前可參考“./configure --help”給出的說明。

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

  • --prefix:指定Nginx的安裝目錄
  • --user:指定Nginx的運行用戶
  • --group:指定Nginx的運行組
  • --with-http_stub_status_module:啟用http_stub_status_module模塊以支持狀態統計,便於查看服務器的連接信息
    make                //生成二進制文件
    make install        //編譯安裝

    4. 為主程序Nginx創建鏈接文件

    創建Nginx主程序的鏈接文件是為了方便管理員直接“nginx”命令就可以調用Nginx的主程序。

    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

    第二步檢查配置文件並啟動Nginx服務

1.檢查配置文件

Nginx的主程序提供了“-t”選項來對配置文件進行檢查,以便找出不當或錯誤的配置。

[root@centos7-1 nginx-1.12.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

2.啟動Nginx

直接運行Nginx即可啟動Nginx服務器

[root@centos7-1 nginx-1.12.0]# nginx 
[root@centos7-1 nginx-1.12.0]# killall -1 nginx       //重啟nginx服務
[root@centos7-1 nginx-1.12.0]# killall -3 nginx      //停止nginx服務

3.使用Nginx服務腳本

為了使nginx服務的啟動、停止、重載等操作更加方便,可以編寫nginx服務腳本,並使用chkconfig和systemctl工具來進行管理,這更加符合系統的管理習慣。

[root@centos7-1 nginx-1.12.0]# vim /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"           //PID存放路徑
case "$1" in
  start)
    $PROG
    ;;
  stop)
    kill -s QUIT $(cat $PIDF)              //根據PID中止nginx進程
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  reload)
    kill -s HUP $(cat $PIDF)              //根據進程號重載配置
    ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
exit 0
[root@centos7-1 nginx-1.12.0]# chmod +x /etc/init.d/nginx
[root@centos7-1 nginx-1.12.0]# chkconfig --add nginx                 //添加為系統服務
[root@centos7-1 nginx-1.12.0]# systemctl start nginx.service

第三步確認Nginx服務是否正常運行

通過檢查Nginx程序的監聽狀態,或者在瀏覽器中訪問此Web服務,默認頁面將顯示“Welcome to nginx!”

[root@centos7-1 nginx-1.12.0]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      54386/nginx: master 
[root@centos7-1 nginx-1.12.0]# yum install elinks -y
[root@centos7-1 nginx-1.12.0]# elinks http://localhost //使用elinks瀏覽器

技術分享圖片

  • 配置訪問狀態統計頁面

    Nginx內置了HTTP_STUB_STATUS狀態統計模塊,用來反饋當前的Web訪問情況。要使用Nginx的狀態統計功能,除了啟用內建模塊以外,還需要修改nginx.conf配置文件,指定訪問位置並添加stub_status配置代碼。

    [root@centos7-1 nginx-1.12.0]# cd /usr/local/nginx/conf
    [root@centos7-1 conf]# mv nginx.conf nginx.conf.back
    [root@centos7-1 conf]# grep -v "#" nginx.conf.back > nginx.conf     //過濾配置文件#號註釋的信息
[root@centos7-1 conf]# vim nginx.conf

server {
        listen       80;
        server_name  localhost;
    charset utf-8;

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

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

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

       }
    }

新的配置生效後,在瀏覽器中訪問nginx服務器的/status網站位置,可以看到當前的狀態統計信息。

systemctl reload nginx.service                  //重新加載nginx服務
systemctl stop firewalld.service               //關閉防火墻
systemctl disable firewalld.service         //禁用防火墻

技術分享圖片
其中,“Active connections”表示當前的活動連接數;而“server accepts handled requests”表示已經處理的連接信息。三個數字依次表示已處理的連接數、成功的TCP握手次數、已處理的請求數。

  • 配置Nginx的訪問控制

    1.基於用戶授權的訪問控制

    (1).使用htpasswd生成用戶認證文件,如果沒有該命令,可使用yum安裝httpd-tools軟件包,用法與Apache認證時方式一樣,在/usr/local/nginx/目錄生成passwd.db文件,用戶名是test,密碼輸入2次。

    yum install httpd-tools -y    //安裝httpd-tools軟件包
    [root@centos7-1 ~]# htpasswd -c /usr/local/nginx/passwd.db test
    New password:                      //設置test用戶密碼
    Re-type new password: 
    Adding password for user test
    [root@centos7-1 ~]# cat /usr/local/nginx/passwd.db         //查看生成的用戶認證文件
    test:$apr1$WfkC0IdB$sMyjqJzg2tcqcIe1mJ8LI/

(2).修改密碼文件的權限為400,將所有者改為nginx,設置nginx的運行用戶能夠讀取。

[root@centos7-1 ~]# chmod 400 /usr/local/nginx/passwd.db 
[root@centos7-1 ~]# chown nginx /usr/local/nginx/passwd.db 
[root@centos7-1 ~]# ll -d /usr/local/nginx/passwd.db 
-r--------. 1 nginx root 43 6月  20 14:45 /usr/local/nginx/passwd.db

(3).修改主配置文件nginx.conf,添加相應認證配置項。

[root@centos7-1 ~]# vim /usr/local/nginx/conf/nginx.conf

location / {
            auth_basic "secret";       //添加認證配置
            auth_basic_user_file /usr/local/nginx/passwd.db;
            root   html;
            index  index.html index.htm;
        }

(4).檢測語法、重啟服務

[root@centos7-1 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos7-1 ~]# systemctl restart nginx.service

(5).用瀏覽器訪問網址,檢驗控制效果。

技術分享圖片
需要輸入用戶名和密碼進行訪問,驗證通過才能進行訪問。
技術分享圖片

2.基於客戶端的訪問控制

Nginx基於客戶端的訪問控制要比Apache的簡單,規則如下:

  • deny IP/IP段:拒絕某個IP或IP段的客戶端訪問
  • allow IP/IP段:允許某個IP或IP段的客戶端訪問。
  • 規則從上往下執行,如匹配規則停止,不在往下匹配。

(1).修改主配置文件nginx.conf,添加相應認證配置項。

[root@centos7-1 ~]# vim /usr/local/nginx/conf/nginx.conf

  location / {
            deny 192.168.113.132;          //客戶端IP
            allow all;
            root   html;
            index  index.html index.htm;
        }

deny 192.168.113.132表示這個ip地址訪問會被拒絕,其他IP客戶端正常訪問。
(2).重啟服務器訪問網址,頁面已經訪問不到。

[root@centos7-1 ~]# systemctl restart nginx.service 

技術分享圖片
技術分享圖片
要註意的是如果是用域名訪問網頁,需要配置DNS域名解析服務器,詳細步驟參考使用Bind部署DNS域名解析服務器之正向解析

部署Nginx網站服務實現訪問狀態統計以及訪問控制功能