欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Nginx Basic Auth认证

程序员文章站 2022-03-24 20:13:33
...

Basic Auth配置:

ubuntu 安装 apache2-utils centos安装httpd-tools

apt install apache2-utils  
#创建用户和密码
htpasswd -c -d /usr/local/openresty/nginx/conf/htpasswd yzq
#配置nginx server 添加以下内容
server {
    listen       8080;   
    server_name  localhost;

    auth_basic   "登录认证";  
    auth_basic_user_file /usr/local/openresty/nginx/conf/htpasswd;
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
	.......
}
#添加完成之后重启nginx
/usr/local/nginx/sbin/nginx -s reload

Basic Auth访问方法:

# 浏览器中使用
直接在浏览器中输入地址, 会弹出用户密码输入框, 输入即可访问

# 使用 curl
curl -u yzq:111.-O http://127.0.0.1/auth

# 使用 wget
wget --http-user=yzq--http-passwd=111. http://127.0.0.1/auth

#python访问
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth("yzq".encode('utf-8'), "111.")
resopnse = requests.get("http://127.0.0.1/auth", auth=auth)
相关标签: linux搭建服务