為 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