使用 Nginx 进行基本认证

在这个页面上可以了解到的信息

使用Nginx进行基本身份验证的配置方法

操作步骤

htpasswd的安装。

sudo apt-get install -y apache2-utils

使用htpasswd为特定的用户引入身份验证配置。
(这里为sample用户引入身份验证配置)

创建基本认证文件

htpasswd -c /etc/nginx/conf.d/.htpasswd sample
New password: 
Re-type new password: 
Adding password for user sample

使用`auth_basic`配置指令来指定身份验证名称(可选),并使用`auth_basic_user_file`指令来指定通过htpasswd创建的密码文件的路径。

nginx的配置

在nginx的配置文件中添加认证设置。

server {
    listen       80;
    server_name  localhost;

    auth_basic "Basic authentication";
    auth_basic_user_file /etc/nginx/conf.d/.htpasswd;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

由于在此处将身份验证配置写入了服务器根目录下,并且在80号端口进行监听,
所以对于所有的位置,都需要进行基本身份验证。
通过在位置下方写入身份验证配置,
也可以仅对特定位置进行基本身份验证的设置。

应用设定

service nginx reload

结局

Kobito.SbcLTk.png
Kobito.mhbSsC.png
广告
将在 10 秒后关闭
bannerAds