子文件夹上的 Nginx 基本 HTML 身份验证

Nginx basic HTML authentication on a subfolder

我需要帮助应用基本 HTML 身份验证来密码保护我网站上的子文件夹 - 由 Digitalocean 托管,使用 Nginx 提供服务。我遵循了教程 - https://www.digitalocean.com/community/tutorials/how-to-set-up-http-authentication-with-nginx-on-ubuntu-12-10

但我的结果是: 1.整个站点提示输入凭据而不是指定的子文件夹,并且 2.本站所有页面不再能找到css和js文件

这是我尝试过的: 1)在子文件夹中生成了一个.htpasswd文件, 2) 在 nginx.conf 文件中添加了一个位置块(见下文),以及 3) 重新加载 nginx。

location / {
auth_basic "Restricted";
auth_basic_user_file /var/www/pepperslice/current/public/ps/jeffaltman/.htpasswd;
}

完整的nginx.config文件如下:

upstream unicorn {
  server unix:/tmp/unicorn.pepperslice.sock fail_timeout=0;
}

server {
  listen 80 default;
  root /var/www/pepperslice/current/public;
  try_files $uri/index.html $uri @unicorn;

  location = /images {
    root /var/www/pepperslice/current/public/images;
  }

  location @unicorn {
    proxy_pass http://unicorn;
  }

location / {
auth_basic "Restricted";
auth_basic_user_file /var/www/pepperslice/current/public/ps/jeffaltman/.htpasswd;
}

  error_page 500 502 503 504 /500.html;
}

目前,我添加的位置块被注释掉了。

我正在寻求帮助,我可以: 1. 密码保护子文件夹 pepperslice.com/ps/jeffaltman,以及 2.密码保护其他子文件夹使用不同的用户名和密码组合。

此外,关于 css 和 js 路径为何失败的任何想法?我猜一旦身份验证问题得到解决,css/js 路径问题就会消失。

谢谢。

经过更多研究,我找到了解决方案并解决了问题。本指南是我找到它的地方 - 请参阅第 3 节 - https://www.howtoforge.com/basic-http-authentication-with-nginx

代替以“location / {...}”(站点的根目录)开头的位置块,对于子文件夹,它应该以子文件夹路径开头。例如:

location /ps/jeffaltman {
auth_basic "Restricted";
auth_basic_user_file /var/www/pepperslice/current/public/ps/jeffaltman/.htpasswd;
}

此外 css 和 js 路径问题也消失了。

干杯。