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

Vin*_*ent 10 deployment ruby-on-rails passenger nginx password-protection

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

location = / {
  auth_basic "Restricted";
  auth_basic_user_file htpasswd;
}  
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

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

Hon*_*gli 17

您需要在位置块中重新指定passenger_enabled.

  • 添加"passenger_enabled on"; 在位置块内. (4认同)

edg*_*ner 6

您可以让Rails处理身份验证

# application_controller.rb
before_filter :authenticate

protected

def authenticate
  authenticate_or_request_with_http_basic do |username, password|
    username == "foo" && password == "bar"
  end
end
Run Code Online (Sandbox Code Playgroud)

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