Nginx配置-开启Http认证basic authentication


生成密码

可以使用htpasswd,或者使用openssl,比如

$ htpasswd -nb admin 123456
admin:$apr1$Bfg9VkNO$qRsedMTQNvx1pG5tTAMMc.

或者
$ printf "admin:$(openssl passwd -crypt 123456)\n"
admin:P0MVcGzusBjns

将密放置于配置文件

比如:/home/testuser/nginx/conf/passwd, 可修改为你自己希望放置的路径

####
这里你将/home/testuser/nginx/conf/passwd 可修改为你自己希望放置的路径
cat > /home/testuser/nginx/ssl/httppasswd << EOF
admin:$apr1$Bfg9VkNO$qRsedMTQNvx1pG5tTAMMc.
EOF

修改nginx.conf

vi nginx.conf,增加以下配置:

auth_basic "please login with user and password";
auth_basic_user_file /home/testuser/nginx/ssl/httppasswd;

可放在server里面或location里面比如:

location /
{
    
    
    auth_basic "please login with user and password";
    auth_basic_user_file conf.d/passwd;
    autoindex on;
}

重新加载nginx配置生效

认证测试

猜你喜欢

转载自blog.csdn.net/jxlhljh/article/details/126270400