htpasswd的使用
1.安装htpasswd
centos yum -y install httpd ubuntu sudo apt-get install apache2-utils
2.nginx 文件配置
server { listen 80; server_name game-xxxx-system.xxx.com; index index.html; access_log /data/logs/nginx/game-manage-system.liquidnetwork.com/access.log; error_log /data/logs/nginx/game-manage-system.liquidnetwork.com/error.log; #root /opt/apps/game-manage-system/dist/; root /home/op/game-manage-system/dist/; ##白名单设置,只允许下面几个来源ip的客户端以及本地能访问该站。主要是下面这三行 if ($remote_addr !~ ^(1.1.145|127.0.0.1|127.0.0.1)) { return 403; } location / { autoindex on; auth_basic "Please input password"; #这里是验证时的提示信息 auth_basic_user_file /home/op/passwd-dlgame; } location = /favicon.ico { return 200; } #禁止访问的文件或目录 location ~ ^/(\.git|LICENSE|README.md) { return 404; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 1d; error_log off; access_log /dev/null; } location ~ .*\.(js|css)?$ { expires 12h; error_log off; access_log /dev/null; } }
3.通过htpasswd命令生成用户名及对应密码。
[root@localhost /]#vi /home/op/passwd-dlgame [root@localhost /]# htpasswd -c /home/op/passwd-dlgame admin //创建认证信息,admin 为认证用户名 New password: ******* //输入认证密码 Re-type new password: ******** //再次输入认证密码 Adding password for user admin 到通过htpasswd生成的密码为加密格式 admin:x/J1o94NgIV2k
4.重启nginx即可
sudo /usr/local/nginx/sbin/nginx -s reload sudo /usr/local/nginx/sbin/nginx -t