为 Nginx 添加 HTTP 基本认证(HTTP Basic Authentication)


针对sudo apt-get install命令安装的nginx(默认会有/etc/nginx/conf.d)

sudo apt-get install nginx

生成密码

printf "your_username:$(openssl passwd -crypt your_password)\n" >> conf.d/passwd

如果没有conf.d/passwd这个文件,就自行创建

配置nginx

修改配置文件:

vim /etc/nginx/sites-enabled/default

修改内容如下:

location /
{
    auth_basic "网站名称";
    auth_basic_user_file conf.d/passwd; 
    autoindex on;
}

重启nginx

/etc/init.d/nginx restart

访问效果如下

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM