跳转至

nginx 为服务增加基本认证

概要: 在 Debian 11 系统上利用 htpasswd 工具对 nginx 代理的网络服务增加基本认证

创建时间: 2023.11.15 22:29:56

更新时间: 2023.11.15 22:42:59

htpasswd is used to create and update the flat-files used to store usernames and password for basic authentication of HTTP users.

安装 htpasswd 工具

Bash
sudo apt update
sudo apt-get install apache2-utils

生成密码

首次使用

Bash
htpasswd -c ./mypws username

添加用户

Bash
htpasswd ./mypws username2

删除用户

Bash
htpasswd -D ./mypws username2

其它命令

Bash
htpasswd --help
image.png

nginx 增加认证

在 nginx.conf 中,加入如下的信息即可

Bash
server {
    listen 80;
    server_name example.com;

    location / {
        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/mypws;

        # 其他配置项...
    }
}

参考