0
点赞
收藏
分享

微信扫一扫

nginx ngx_http_auth_basic_module配置详解:实现基于用户的http访问控制


ngx_http_auth_basic_module 是 Nginx 的一个模块,用于实现基于 HTTP 基本认证(HTTP Basic Authentication)的访问控制。这个模块允许你通过用户名和密码来保护特定的资源或路径。下面是对 ngx_http_auth_basic_module 中常用配置指令的详细解释。

极简示例:

auth_basic string | off;
auth_basic_user_file file;
location /admin/ {
    auth_basic "Admin Area";
    auth_basic_user_file /etc/nginx/.ngxpasswd;
}
用户口令:
1、明文文本:格式name:password:comment
2、加密文本:由htpasswd命令实现 httpd-tools所提供
htpasswd [-c第一次创建时使用] [-D删除用户] passwdfile   username

配置详解

1. auth_basic

启用或禁用 HTTP 基本认证,并设置提示信息。

  • 语法

auth_basic string | off;

  • 参数
  • string:认证提示信息,显示在浏览器的认证对话框中。
  • off:禁用 HTTP 基本认证。
  • 示例

auth_basic "Restricted Area";
auth_basic off;

2. auth_basic_user_file

指定包含用户名和密码的文件路径。这个文件通常由 htpasswd 工具生成。

  • 语法

auth_basic_user_file path;

  • 参数
  • path:包含用户名和密码的文件路径。
  • 示例

auth_basic_user_file /etc/nginx/htpasswd;

使用场景

  1. 保护特定目录
  • 例如,保护 /admin 目录,只有拥有正确凭据的用户才能访问。

location /admin {
    auth_basic "Admin Area";
    auth_basic_user_file /etc/nginx/htpasswd;
}

  1. 保护特定文件
  • 例如,保护特定的文件,如 secret.txt

location = /secret.txt {
    auth_basic "Restricted File";
    auth_basic_user_file /etc/nginx/htpasswd;
}

  1. 保护整个站点
  • 例如,保护整个站点,只有拥有正确凭据的用户才能访问。

server {
    listen 80;
    server_name example.com;

    auth_basic "Restricted Site";
    auth_basic_user_file /etc/nginx/htpasswd;

    location / {
        root /var/www/html;
        index index.html index.htm;
    }
}

创建用户文件

你可以使用 htpasswd 工具来创建和管理包含用户名和密码的文件。以下是一些常用的命令:

  1. 创建新的用户文件并添加用户

htpasswd -c /etc/nginx/htpasswd username

  1. 添加或修改现有用户

htpasswd /etc/nginx/htpasswd username

  1. 查看用户文件
    用户文件的内容类似于以下格式:

username:$apr1$randomsalt$hashedpassword

示例配置

以下是一个完整的 Nginx 配置示例,展示了如何使用 ngx_http_auth_basic_module 中的 auth_basicauth_basic_user_file 指令:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout  65;
    client_max_body_size 10m;

    server {
        listen 80;
        server_name example.com;
        root /var/www/html;
        index index.html index.htm;

        # 保护 /admin 目录
        location /admin {
            auth_basic "Admin Area";
            auth_basic_user_file /etc/nginx/htpasswd;
        }

        # 保护特定文件
        location = /secret.txt {
            auth_basic "Restricted File";
            auth_basic_user_file /etc/nginx/htpasswd;
        }

        # 保护整个站点
        location / {
            auth_basic "Restricted Site";
            auth_basic_user_file /etc/nginx/htpasswd;
        }
    }
}

注意事项

  1. 安全性
  • HTTP 基本认证将用户名和密码以 Base64 编码的形式传输,而不是加密形式。因此,建议在使用 HTTPS 时启用 HTTP 基本认证,以确保传输的安全性。

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    location /admin {
        auth_basic "Admin Area";
        auth_basic_user_file /etc/nginx/htpasswd;
    }
}

  1. 用户文件权限
  • 确保用户文件的权限设置正确,以防止未授权访问。通常,用户文件的权限应设置为 640600

chmod 640 /etc/nginx/htpasswd


举报

相关推荐

0 条评论