Linux命令 Nginx basicauth


原文链接: Linux命令 Nginx basicauth

生成htpasswd密码

  1. openssl passwd
  2. printf "hr:$(openssl passwd -crypt bpbqc)\n" >>htpasswd
    printf "elastic:$(openssl passwd -crypt changeme)\n" >htpasswd_elastic
    cat /etc/nginx/htpasswd ttlsa:xyJkVhXGAZ8tM

  3. htpasswd 生成
    htpasswd -c -d pass_file username password #-c 表示生成文件,-d 是以 crypt 加密。
    htpasswd -b /etc/nginx/htpasswd hyc 123456 #去掉-c选项,添加第二个用户
    htpasswd -nb Jack 123456 #只显示加密信息不写入文件

    修改htpasswd权限

    chmod 400 htpasswd

使用basicauth认证

location /
{

      auth_basic "nginx basic http test for ttlsa.com";
      auth_basic_user_file conf/htpasswd;
      autoindex on;

}

proxy

location /proxy/ {
  if ($arg_token ~ "^$") { return 404; }
  if ($arg_url ~ "^$") { return 404; }

  set $url $arg_url;
  set $token $arg_token;
  set $args "";

  # IMPORTANT, this is required when using dynamic proxy pass
  # You can alternatively use any DNS resolver under your control
  resolver 8.8.8.8;

  proxy_pass $url;
  proxy_set_header Authorization "Bearer $token";
  proxy_redirect off;
}
`