深度阅读

How to add basic authentication to an nginx site?

作者
作者
2023年08月22日
更新时间
8.56 分钟
阅读时间
0
阅读量

To add Basic Authentication to an Nginx site, you can follow these steps:

  1. Create a password file that contains the username and password for each user that needs access to the site:
sudo htpasswd -c /etc/nginx/.htpasswd username

Replace “username” with the desired username. Htpasswd will prompt you to enter a password.
2. Open the Nginx configuration file for your site:

sudo nano /etc/nginx/sites-available/example.com
  1. Inside the server block, add the following directives to the location that needs to be protected:
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
  1. Reload the Nginx configuration for the changes to take effect:
sudo service nginx reload

With these steps, users will now need to enter a username and password to access the protected location of your site.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。