密码保护在Nginx和Phusion Passenger上运行的Rails站点

时间:2010-11-13 07:27:41

标签: ruby-on-rails deployment nginx passenger password-protection

我希望使用基本的http身份验证来保护我新部署的Rails 3应用。它运行在最新的Nginx / Passenger上,我使用以下Nginx指令来保护Web根目录:

location = / {
  auth_basic "Restricted";
  auth_basic_user_file htpasswd;
}  

htpasswd文件是使用Apache htpasswd utililty生成的。但是,输入正确的用户名和密码后,我将转移到403 Forbidden错误页面。分析Nginx错误日志显示:

directory index of "/var/www/mysite/public/" is forbidden, client: 108.14.212.10, server: mysite.com, request: "GET / HTTP/1.1", host: "mysite.com"

显然,我不想列出mysite / public目录的内容。如何正确配置以便在输入登录信息后启动Rails应用程序?

3 个答案:

答案 0 :(得分:17)

您需要在位置区重新指定passenger_enabled。

答案 1 :(得分:6)

你可以let Rails handle the authentication

# application_controller.rb
before_filter :authenticate

protected

def authenticate
  authenticate_or_request_with_http_basic do |username, password|
    username == "foo" && password == "bar"
  end
end

您还应该在config.serve_static_assets = true(或Rails 3中的environment.rb)中设置applicaion.rb,以便public中的静态资源通过相同的过滤器。

答案 2 :(得分:2)

检查您的Nginx错误日志。 403表示您的密码文件路径错误。