配置nginx使用基本身份验证?

时间:2015-05-26 18:33:43

标签: nginx

我有nginx工作正常,我想添加基本的auth:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
  open_file_cache max=100;
  log_format octohost '$http_x_forwarded_for - $remote_user [$time_local]  '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent" "$http_x_request_id"';
  access_log /dev/stdout;
  error_log /dev/stdout info;

  server {
      auth_basic                  "Restricted";
      auth_basic_user_file        /etc/nginx/sites-available/.htpasswd;
  }
}

daemon off;

但是,它没有请求用户名/密码。 .htpasswd文件只是“测试:测试”...我对此缺少什么?

1 个答案:

答案 0 :(得分:0)

我认为auth_basic指令需要位于location部分内。例如:

server {
  location / {
    auth_basic           "closed site";
    auth_basic_user_file conf/htpasswd;
 }
}