nginx认证与访问模块

nginx认证模块

Module ngx_http_auth_basic_module

Syntax: auth_basic string | off;
Default:    
auth_basic off;
Context:    http, server, location, limit_except
说明: string说明影响的范围,off只在当前的指定context中有效,不会被继承

Syntax: auth_basic_user_file file;
Default:    —
Context:    http, server, location, limit_except
Specifies a file that keeps user names and passwords, in the following format:

# comment
name1:password1
name2:password2:comment
name3:password3

使用htpasswd生成加密的密码文件
#htpasswd –help

Usage:
    htpasswd [-cmdpsD] passwordfile username
    htpasswd -b[cmdpsD] passwordfile username password 

    htpasswd -n[mdps] username
    htpasswd -nb[mdps] username password
    //-b命令行输入用户密码
 -c  Create a new file.
 -n  Don't update file; display results on stdout.
 -m  Force MD5 encryption of the password.
 -d  Force CRYPT encryption of the password (default).
 -p  Do not encrypt the password (plaintext).
 -s  Force SHA encryption of the password.
 -b  Use the password from the command line rather than prompting for it.
 -D  Delete the specified user.

example:

    location / {
        auth_basic           "closed site";
        auth_basic_user_file htpasswd;  //默认在conf下
    }

# htpasswd  -cb htpasswd  test 123
Adding password for user test
# cat htpasswd
test:89XzEEI/P0e56

输入错误的用户或密码,出现401认证失败
当auth_basic_user_file不正确,出现403错误,查看错误日志

403 Forbidden
403错误,表示资源不可用。服务器理解客户的请求,但拒绝处理它,通常由于服务器上文件或目录的权限设置导致的WEB访问错误。

nginx访问模块

Module ngx_http_access_module

Syntax: [allow|deny]  address | CIDR | unix: | all;
Default:     —
Context:    http, server, location, limit_except
Allows access for the specified network or address. If the special value unix: is specified (1.5.1), allows access for all UNIX-domain sockets.

Example Configuration

location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}

deny的ip访问时,报403错误
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值