【问题标题】:htaccess on heroku for django app用于 django 应用程序的 heroku 上的 htaccess
【发布时间】:2012-03-13 01:52:46
【问题描述】:

标题几乎概括了我的问题。我想用密码保护我的 django 应用程序中存在于 heroku 上的一些文件。
如果我不能使用 htaccess,是否有人对我还可以使用什么提出建议? 谢谢。

【问题讨论】:

  • 我宁愿不使用任何特定于 django 的东西,因为我有一个测试、生产和演示服务器要处理
  • 为什么不使用SITE_ID 和站点框架来区分?

标签: django .htaccess heroku


【解决方案1】:

正如@mipadi 所说,您不能在 Heroku 上使用 htaccess,但您可以为此创建一个中间件:

from django.conf import settings
from django.http import HttpResponse
from django.utils.translation import ugettext as _


def basic_challenge(realm=None):
    if realm is None:
        realm = getattr(settings, 'WWW_AUTHENTICATION_REALM', _('Restricted Access'))
    # TODO: Make a nice template for a 401 message?
    response =  HttpResponse(_('Authorization Required'), mimetype="text/plain")
    response['WWW-Authenticate'] = 'Basic realm="%s"' % (realm)
    response.status_code = 401
    return response

def basic_authenticate(authentication):
    # Taken from paste.auth
    (authmeth, auth) = authentication.split(' ',1)
    if 'basic' != authmeth.lower():
        return None
    auth = auth.strip().decode('base64')
    username, password = auth.split(':',1)
    AUTHENTICATION_USERNAME = getattr(settings, 'BASIC_WWW_AUTHENTICATION_USERNAME')
    AUTHENTICATION_PASSWORD = getattr(settings, 'BASIC_WWW_AUTHENTICATION_PASSWORD')
    return username == AUTHENTICATION_USERNAME and password == AUTHENTICATION_PASSWORD

class BasicAuthenticationMiddleware(object):
    def process_request(self, request):
        if not getattr(settings, 'BASIC_WWW_AUTHENTICATION', False):
            return
        if 'HTTP_AUTHORIZATION' not in request.META:
            return basic_challenge()
        authenticated = basic_authenticate(request.META['HTTP_AUTHORIZATION'])
        if authenticated:
            return
        return basic_challenge()

那你需要在settings.py中定义:

BASIC_WWW_AUTHENTICATION_USERNAME = "your user"
BASIC_WWW_AUTHENTICATION_PASSWORD = "your pass"
BASIC_WWW_AUTHENTICATION = True

【讨论】:

  • 这很好用。如何编辑它使其仅适用于特定网址?
  • 我实际上是通过使用 request.META['PATH_INFO'] 检查 url 并在我不希望该 url 密码保护时返回
  • 如果您在使用本示例时收到错误TypeError: object() takes no parameters,您可能使用的是Django 1.10 或更高版本,需要添加django.utils.deprecation.MiddlewareMixin 或升级旧式中间件。请参阅:docs.djangoproject.com/en/1.10/topics/http/middleware/…
  • 很好的答案!注意:Python 3 用户将获得一个'str' object has no attribute 'decode'̀. They'll need to import the base64` 模块并将auth.strip().decode('base64') 更改为base64.b64decode(b).decode('utf-8')
【解决方案2】:

我可以在 heroku 上使用 .htaccess 文件和雪松堆栈。

  1. Procfile需要为web节点指定一个脚本:

    web:    sh www/conf/web-boot.sh
    
  2. conf/web-boot.sh 滑流包含 apache 配置文件,例如:

  3. conf/httpd/default.conf 然后可以允许覆盖,正如您从 apache 中知道的那样。

然后您可以只使用.htaccess 文件。整个过程在我的博文PHP on Heroku again中有详细记录,其中一部分是关于apache配置的。 2.中的步骤包括自己的httpd配置基本上是:

sed -i 's/Listen 80/Listen '$PORT'/' /app/apache/conf/httpd.conf
sed -i 's/^DocumentRoot/# DocumentRoot/' /app/apache/conf/httpd.conf
sed -i 's/^ServerLimit 1/ServerLimit 8/' /app/apache/conf/httpd.conf
sed -i 's/^MaxClients 1/MaxClients 8/' /app/apache/conf/httpd.conf

for var in `env|cut -f1 -d=`; do
  echo "PassEnv $var" >> /app/apache/conf/httpd.conf;
done
echo "Include /app/www/conf/httpd/*.conf" >> /app/apache/conf/httpd.conf
touch /app/apache/logs/error_log
touch /app/apache/logs/access_log
tail -F /app/apache/logs/error_log &
tail -F /app/apache/logs/access_log &
export LD_LIBRARY_PATH=/app/php/ext
export PHP_INI_SCAN_DIR=/app/www
echo "Launching apache"
exec /app/apache/bin/httpd -DNO_DETACH

我希望这会有所帮助。我将它用于 .htaccess 和更改 webroot。

【讨论】:

    【解决方案3】:

    您不能使用 .htaccess,因为 Heroku 应用程序不使用 Apache。不过,您可以使用Django authentication

    或者您可以使用 Apache 从另一台正在的服务器上提供文件。

    【讨论】:

    • 谁说 Heroku 应用程序不使用 Apache?我有一个运行良好的 apache 配置。您的答案属于哪个堆栈?我在 cedar 上使用 .htaccess 文件没有问题,基本设置是 documented on my blog,以防万一。
    • @hakre:大多数堆栈都使用 nginx。也许 PHP buildpack 做了一些不同的事情。
    • 好的,可能是这样。我知道您可以创建自己的 buildback,因此甚至指定网络服务器软件。
    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2017-07-08
    • 2012-06-24
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多