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.178linux.com/101940

(0)
net31_行者无疆net31_行者无疆
上一篇 2018-06-28
下一篇 2018-06-29

相关推荐

  • 浅析计算机与Linux

    在介绍Linux之前,我们先来了解装载并运行Linux的计算机。 计算机由CPU、存储器及I/O设备组成。其中,CPU包括运算器和控制器,分别负责数据计算并指挥计算机各部分协调地工作;存储器即内存,用于存储程序及各种数据信息,需注意的是,存储的数据均为二进制格式,意即计算机CPU只能处理二进制数据; I/O设备即输入设备(Input Device)和输出设备…

    2018-05-11
  • 数组,字符串切片,高级变量

    数组 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合 数组名和索引 索引:编号从0开始,属于数值索引 注意:索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引 bash4.0版本之后开始支持 bash –version   查看bash版本 bash的数组支持稀疏格式(索引不连续) 声明数组: de…

    Linux笔记 2018-05-13
  • 实现cobbler自动安装(基于网络)

    本节索引: 一、cobbler简介 二、实验:模拟搭建cobbler服务器,实现cobbler自动化安装系统 三、实验:实现基于web的cobbler   一、cobbler简介: cobbler 功能:用来快速建立 Linux 网络安装环境 特性: 1.基于PXE的二次封装,将多种安装参数封装到一个菜单,更加方便,自动化 2.Python编写 3…

    Linux笔记 2018-05-27
  • Linux的tail 与head 命令

    head命令是用来查看具体文件的前面几行的内容,具体格式如下: head <filename>: 你可以通过head命令查看具体文件最初的几行内容,该命令默认是前10行内容,如果你想查看前面更多内容,你可以通过一个数字选项来设置,例如 head -20 install.log 通过上面命令你可以查看install.log这个文件前面20行的内容 …

    2018-04-16
  • N31Linux第三周

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]#who |cut -d ‘ ‘ -f1 |sort -u 2、列出最后登录到当前用户系统的用户相关信息。 [root@localhost ~]#w |cut -d ‘ ‘ -f1 |…

    2018-07-16
  • 计算机扫盲闲谈—番外篇(基础知识补充)

    Ken Thompson:一切为了游戏。没错就是为了玩这款为Multics系统开发的游戏“star travel”他编写了一套简化的系统——Unix

    2018-03-27