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
下一篇 2018-06-29

相關推薦

  • 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
  • shell腳本編程示例

    #!/usr/bin/expectspawn scp /etc/fstab 192.168.8.100:/appexpect {“yes/no” { send “yes\n”;exp_continue }“password” { send “magedu\n” }}expec…

    2018-05-12
  • liunx 系統筆記

    服務器的硬件組成 ?如圖: 服務器配置示例:

    Linux筆記 2018-04-01
  • N31第五周

    1、簡述rpm與yum命令的常見選項,并舉例 一.rpm包管理 CentOS系統上使用rpm命令管理程序包:安裝、卸載、升級、查詢、校驗、數據庫維護安裝 (1)安裝[install-options] rpm {-i | –install} [install-options] PACKAGE_FILE … -h:使用”#&…

    Linux筆記 2018-07-30
  • Linux系統上命令的使用格式

    詳細介紹ifconfig、echo、tty、startx、export、pwd、history、shutdown、poweroff、reboot、hwclock、 ifconfig ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>] [add <address…

    Linux筆記 2018-05-11
  • hell腳本基礎知識詳細介紹

    Shell本身是一個用C語言編寫的程序,它是用戶使用Linux的橋梁。Shell既是一種命令語言,又是一種程序設計語言。作為命令語言,它交互式地解釋和執行用戶輸入的命令;作為程序設計語言,它定義了各種變量和參數,并提供了許多在高級語言中才具有的控制結構,包括循環和分支。 它雖然不是Linux系統核心的一部分,但它調用了系統核心的大部分功能來執行程序、建立文件…

    Linux筆記 2018-05-14
欧美性久久久久