JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

Nginx 动态配置 HTTP Basic Authentication 并实时推送到钉钉

wys521 2024-10-12 23:12:52 精选教程 18 ℃ 0 评论

? 部署 Nginx

1、安装 Nginx

yum install -y nginx

2、创建必要文件夹

mkdir -p /data/log/nginx

3、启动 Nginx

nginx

? 配置 Nginx Basic Authentication

1、安装 `httpd-tools`

yum install -y httpd-tools

2、生成账号、密码

htpasswd -cb /etc/nginx/.htpasswd anoyi password

3、修改 Nginx 配置文件 `/etc/nginx/conf.d/default.conf`

server {
 ...
 auth_basic "Administrator’s Area";
 auth_basic_user_file /etc/nginx/.htpasswd;
 ...
}

4、重启 Nginx

nginx -s reload

?动态密码发送到钉钉

1、设置钉钉群机器人

  • 参考文档:https://open-doc.dingtalk.com/microapp/serverapi2

2、动态密码推送脚本 nginx_pwd.sh

#!/bin/bash

# 账号
NGINX_USERNAME=anoyi

# UUID 动态密码
NGINX_PASSWORD=`uuidgen`

# 更新 Nginx 密码
htpasswd -cb /etc/nginx/.htpasswd $NGINX_USERNAME ${NGINX_PASSWORD//'-'/}

# 推送到钉钉
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxx' \
 -H 'Content-Type: application/json' \
 -d "{\"msgtype\": \"text\", \"text\": {\"content\": \"账号:${NGINX_USERNAME}\n密码:${NGINX_PASSWORD//'-'/}\"}}"

# 重新加载 nginx
nginx -s reload

修改脚本权限

chmod a+x nginx_pwd.sh

推送动态密码

./nginx_pwd.sh

?定时更新密码

1、添加任务,执行命令 `crontab -e`,输入如下内容

30 9 * * 1 sh ~/nginx_pwd.sh

代表每周一 9:30 执行脚本 `nginx_pwd.sh`

2、删除任务

crontab -r

? 相关文档

  • Nginx Http Basic Authtication:https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/
  • Linux Crontab 定时任务:https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表