【问题标题】:Nginx basic HTML authentication on a subfolder子文件夹上的 Nginx 基本 HTML 身份验证
【发布时间】:2015-06-25 19:16:02
【问题描述】:

我需要帮助应用基本 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 文件中添加了一个 location 块(见下文),并且 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 路径问题就会消失。

谢谢。

【问题讨论】:

    标签: authentication nginx .htpasswd


    【解决方案1】:

    经过更多研究,我找到了解决方案并解决了这个问题。这个指南是我找到它的地方 - 见第 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路径问题也消失了。

    干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 2018-01-11
      • 2016-01-11
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多