Nginx添加htpasswd认证

Nginx添加htpasswd认证

 参考:

http://blog.csdn.net/longxibendi/article/details/23954017

http://tool.oschina.net/htpasswd

http://www.letuknowit.com/post/12.html

 

在Linux上编写脚本(注意权限,对脚本做了一些修改)

vi htpasswd.sh

 

 #!/bin/sh

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

export PATH

 

 

 

echo "====================================="

echo "# A tool like htpasswd for Nginx    #"

echo "#-----------------------------------#"

echo "# Author:Licess http://blog.csdn.net/longxibendi #"

echo "====================================="

 

workdir="/nginx/user/conf"

 

#set UserName

 

 

        username=""

        read -p "Please input UserName:" username

        if [ "$username" = "" ]; then

                echo "Error:UserName can't be NULL!"

                exit 1

        fi

        echo "==========================="

        echo "UserName was: $username"

        echo "==========================="

 

 

#set password

 

 

        unpassword=""

        read -p "Please input the Password:" unpassword

        if [ "$unpassword" = "" ]; then

                echo "Error:Password can't be NULL!"

                exit 1

        fi

        echo "==========================="

        echo "Password was: $unpassword"

        echo "==========================="

password=$(perl -e 'print crypt($ARGV[0], "pwdsalt")' $unpassword)

 

 

#set htpasswd file

 

 

        htfile=""

        #read -p "Please input Auth filename:" htfile

        if [ "$htfile" = "" ]; then

                echo "Error:Auth filename can't be NULL!"

                exit 1

        fi

        echo "==========================="

        echo "Auth File: $workdir/$htfile"

        echo "==========================="

 

 

        get_char()

        {

        SAVEDSTTY=`stty -g`

        stty -echo

        stty cbreak

        dd if=/dev/tty bs=1 count=1 2> /dev/null

        stty -raw

        stty echo

        stty $SAVEDSTTY

        }

        echo ""

        echo "Press any key to Creat...or Press Ctrl+c to cancel"

        char=`get_char`

 

 

if [ ! -f $workdir/$htfile.conf ]; then

  make -p $workdir/$htfile.conf

 

  echo "Create Auth file......"

fi

cat >>$workdir/$htfile.conf<<eof

$username:$password

eof

echo "Create Auth file successful,auth file path:$workdir/$htfile.conf."

cat $workdir/$htfile.conf

 

生成用户名密码信息

 

./htpasswd.sh

 

修改Nginx.conf配置

添加如下内容:

auth_basic "Authorized users only";

auth_basic_user_file /nginx/conf/longxibendi.auth.conf ; #这里写前面脚本返回的文件路径;

 

重启nginx

nginx -t

nginx -s reload

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值