phpMyAdmin 是一个以 PHP 为基础,以 Web-Base 方式架构在网站主机上的 MySQL 的数据库管理工具,让管理者可用 Web 接口管理 MySQL 数据库。

通过 Nginx auth_basic 验证功能,可以为 phpMyAdmin 目录增加用户名,密码验证机制。防止任意用户访问 phpMyAdmin(0day 我怕怕!)。

nginx_auth.gif

 

一,使用 htpasswd 命令生成密码文件,支持语法如下:

/usr/bin/htpasswd -c passwordfile username

二,举例:生成一个 reistlin.passwd 密码文件,用户名 admin,密码 ***

/usr/bin/htpasswd -c /etc/nginx/conf/reistlin.passwd admin

New password: ***
Re-type new password: ***

Adding password for user admin

# 注意,密码文件访问权限
chmod 600 reistlin.passwd

三,编辑 nginx.conf,配置 auth_basic,配置 reistlin.passwd 密码文件路径,配置访问策略和请求限制

配置说明:

1,需要在 http {} 中启用 HttpLimitReqModule,定义一个 zone(admin),session state 设置为 1M(仅用于 auth_basic 安全验证),每分钟 3 次(20 秒一次),“来源 IP 地址”为判断条件。

2,需要在 location {} 中定义 auth_basic 安全验证提示信息,指定密码文件路径。启用 admin zone,设置安全验证请求限制(每分钟 20 次,每次最多接受 3 个请求,超过的请求将被 503 Service Unavailable)。

# http
limit_req_zone  $binary_remote_addr  zone=admin:1m  rate=3r/m;

# location
location /phpMyAdmin/ {
        root   /home/reistlin/htdocs;
        index  index.html  index.htm  index.php;
        auth_basic  "Administrator Login";
        auth_basic_user_file  /etc/nginx/conf/reistlin.passwd;

        limit_req  zone=admin  burst=3;
}

四,验证配置文件,Reload Nginx 配置

/etc/nginx/sbin/nginx -t

the configuration file /etc/nginx/conf/nginx.conf syntax is ok
configuration file /etc/nginx/conf/nginx.conf test is successful

kill -HUP `cat /etc/nginx/logs/nginx.pid`
 
 
猜你喜欢: