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 22:24
下一篇 2018-06-29 11:18

相关推荐

  • Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示

    文件管理工具:cp,mv,rm cp:命令:copy:复制文件的数据    源文件;目标文件       单源复制 cp [OPTION]… [-T] SOURCE DEST   如果DEST不存在,则事先创建此文件,并复制原文件的数据流至DEST中:   如果DEST存在:        如果DEST是非目录文件:则覆盖目标文件        …

    Linux笔记 2018-05-20
  • fstab、init文件创建,内核启动管理

    /etc/下的fstab文件和/sbin/下的init文件的创建,内核启动列表管理简述

    2018-05-15
  • mysq数据库五

    主从级联复制;主主服务器的搭建;半同步和过滤;主从复制加密;mah高可用性;5实现galeracluster

    2018-06-15
  • 新开始,新航程

           每一个夏天都是变动的季节,这个夏季我毕业了。大学最后的时光是在实验室度过的,每天都被瓶瓶罐罐所包围,鼻子里不时的飘进乙酸乙酯的香味,研究完了生物柴油的催化,是时候该为自己的未来找一条出路了。         大二的时候出于对计算机的着迷,一不小心点进了51cto,从此就走上了不归路。从计算机网络到linux,再到mysql,我在这个世界里乐此不…

    Linux笔记 2018-07-21
  • LInux系统误将chmod 权限改成了000如何恢复?

    需要解决方法的可直接跳跃至最后“解决方法目”;
    解决方法用到了新学的灵活权限设置 ACL,正文对Linux权限,和ACL用法做了详细解释。

    2018-04-06
  • 文件系统挂载

    一、mount、umount、mount -o remount、findmnt、lsof、fuser
    二、挂载设置ACL权限、模拟U盘功能、losetup
    三、UUID故障处理、分区迁移与恢复、永久挂载光盘分区、挂载USB、永久挂载文件夹
    四、SWAP、挂载光盘、制作ISO镜像、制作Centos6Everthing
    五、df、du、dd备份/恢复/修复硬盘

    2018-05-26