Nginx認證模塊與狀態監測模塊使用示例

Nginx認證模塊與狀態監測模塊使用示例

一、環境準備

1. 軟件版本

  • CentOS7.4

  • httpd-tools.x86_64??????? 2.4.6-67.el7.centos.6

  • nginx.x86_64????????????????? 1:1.12.2-2.el7

2. 軟件安裝

  • nginx的安裝需要epel源,沒有配置epel可以直接yum安裝一下:

    [root@wind ~]# yum install epel-release.noarch -y
    [root@wind ~]# yum clean all && yum makecache
  • 安裝nginx

    [root@wind ~]# yum install nginx -y

  • httpd-tools?

    CentOS7.4已經預裝了httpd服務,同時也默認安裝了此工具。沒有的話也可以yum裝一下。

3. 實驗環境檢查

  • 因為系統里已經安裝了httpd服務,所以要提前檢查80端口是否被占用。

    [root@wind ~]# ss -tnl |grep 80
    LISTEN ? ? 0 ? ? ?128 ? ?192.168.7.138:80 ? ? ? ? ? ? ? ? ? ? ? *:*
    [root@wind ~]# systemctl stop httpd
    [root@wind ~]# systemctl disable httpd
    Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
  • 確定一下yum安裝的nginx配置文件路徑

    [root@wind ~]# rpm -ql nginx |grep nginx.conf
    /etc/nginx/nginx.conf
    /etc/nginx/nginx.conf.default
  • 先啟動nginx服務測試下是否正常

    [root@wind ~]# systemctl start nginx
    [root@wind ~]# ss -tnl |grep 80
    LISTEN ? ? 0 ? ? ?128 ? ? ? ?  *:80 ? ? ? ? ? ? ? ? ? ? ? *:* ? ? ? ? ? ? ? ? ?
    LISTEN ? ? 0 ? ? ?128 ? ? ? ? :::80 ? ? ? ? ? ? ? ? ? ?  :::* #默認同時監聽了ipv6
    [root@wind ~]# curl -I -m 10 -o /dev/null -s -w %{http_code} http://localhost
    200 ? ? ? ? ? ? ? ? ? ? ? ? ? #此方法常用于腳本中測試web服務
    [root@wind ~]# curl -I -s -l  http://localhost |sed -n '1p'
    HTTP/1.1 200 OK
    [root@wind ~]# curl -I -s -l  http://localhost |grep 200
    HTTP/1.1 200 OK

二、測試認證功能

1. 使用htpasswd配置認證訪問的用戶

  • 添加認證用戶jerry,注意此用戶與系統內的用戶無關。

    [root@wind ~]# htpasswd -c /etc/nginx/.ngxpasswd jerry
    New password: 
    Re-type new password: 
    Adding password for user jerry

2. 修改nginx配置文件啟用認證訪問。

  location  /secret/ {
 ? ? ? ?  auth_basic "It's a secret!";
 ? ? ? ?  auth_basic_user_file /etc/nginx/.ngxpasswd;
 ? ? ? ?  }

3. 在nginx目錄下添加測試頁面。并重載配置文件

  [root@wind ~]# rpm -ql nginx |grep index
  /usr/share/nginx/html/index.html
  [root@wind ~]# mkdir /usr/share/nginx/html/secret
  [root@wind ~]# echo "I don't have any secret." > /usr/share/nginx/html/secret/main.html
  [root@wind ~]# /usr/sbin/nginx -s reload

4. 測試效果

  [root@wind ~]# curl -u jerry http://192.168.7.138/secret/main.html
  Enter host password for user 'jerry':
  I don't have any secret.

三、添加狀態監測頁面并開啟認證

1.修改nginx配置文件

location = /status {
 ? ? ?  allow 192.168.7.0/24; ? #為了安全可以設定ip范圍
 ? ? ?  deny all;
 ? ? ?  auth_basic "";      #不想提示為何認證留空就好
 ? ? ?  auth_basic_user_file /etc/nginx/.ngxpasswd;
 ? ? ?  stub_status;
 ? ? ?  }

2. 查看效果

[root@wind ~]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@wind ~]# /usr/sbin/nginx -s reload
[root@wind ~]# curl -u jerry http://192.168.7.138/status
Enter host password for user 'jerry':
Active connections: 1 
server accepts handled requests
 72 72 83 
Reading: 0 Writing: 1 Waiting: 0 

3. stub參數說明

?Active connections: 活動狀態的連接數;???????????????????

accepts:已經接受的客戶端請求的總數;???????????????????

handled:已經處理完成的客戶端請求的總數;???????????????????

requests:客戶端發來的總的請求數;???????????????????

Reading:處于讀取客戶端請求報文首部的連接的連接數;???????????????????

Writing:處于向客戶端發送響應報文過程中的連接數;???????????????????

Waiting:處于等待客戶端發出請求的空閑連接數;

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/101940

(0)
net31_行者無疆net31_行者無疆
上一篇 2018-06-28 22:24
下一篇 2018-06-29 11:18

相關推薦

  • dns看圖來配,思路一下明了

      1.子域授權: 每個域的名稱服務器,都是通過其上級名稱服務在解析庫中進行授權。 如:根域授權頂級域是怎么做的? 說明.com.這個域?? 它的ns服務器是主機名ns1.com.;2.com. .com.???? IN? NS??? ns1.com. .com.???? IN? NS??? ns2.com. ns1.com.?? IN?? A??…

    Linux筆記 2018-06-03
  • Linux用戶和組管理

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 2、取出最后登錄到當前系統的用戶的相關信息。 3、取出當前系統上被用戶當作其默認shell的最多的那個shell。 4、將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保存至/tmp/maxusers.txt文件中。 5、取出當前主機的…

    Linux筆記 2018-07-13
  • 第八周筆記

    awk 文本三劍客之一,文本處理工具 awk分為Gawk Pawk Dawk Centos用的是Gawk模式掃描處理的語言/user/bin/awk awk的路徑 awk的使用語法awk [選項] `program` var=value file…..(變量賦值)awk [options] ‘program’ var=value file…awk…

    Linux筆記 2018-05-20
  • 計算機基礎

    計算機基礎與linux的常用命令

    2018-04-01
  • Mysql之備份及還原相關實驗

    本節索引 實驗一:冷備份數據庫并還原 實驗二:結合LVM邏輯卷實現幾乎熱備 實驗三:數據庫數據文件損壞后,利用mysqldump還原至最新狀態 實驗四:誤刪除表后,利用mysqldump還原至最新狀態 實驗五:Xtrabackup完全備份及還原 實驗六:Xtrabackup完全,增量備份及還原 實驗七:Xtrabackup單表導出和導入   實驗準…

    2018-06-14
  • Linux基礎介紹、系統組成和獲取命令幫助及系統目錄說明

    一、描述計算機的組成及其功能 計算機由運算器、控制器、存儲器、輸入設備、輸出設備組成1.運算器-主要是對數據進行各種運算(加、減、乘、除)2.控制器-計算機系統的控制中心,分配和協調整個計算機系統工作3.存儲器-存儲各種數據信息,以二進制形式存儲,分為內存和外存4.輸入設備-將數據、程序等信息輸入到計算機中,例如鍵盤5.輸出設備-將計算機的運算結果顯示出來,…

    2018-06-23
欧美性久久久久