如何将 HTTP 用户名从 Apache 传递到 Mongrel/Rails?
目标:在 Mongrels 上运行 Rails 应用程序,在进行基本 HTTP 身份验证后允许通过 Apache 进行访问
问题:从 Rails
Apache 中读取提供的用户名:
<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:4001
# ...
Order deny,allow
Deny from all
AuthType Basic
AuthName "<realm>"
AuthUserFile "<users-file>"
AuthGroupFile "<groups-file>"
Require group <group>
Satisfy Any
</Proxy>
RewriteEngine On
# ...
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
效果很好, Apache 强制用户进行身份验证,如果成功则转发到 Rails。我省略了几行来处理静态文件等,并触发它们的身份验证。
从 Rails 的角度来看,环境变量包含常用条目以及 HTTP_X_FORWARDED_HOST
、HTTP_X_FORWARDED_SERVER
和 HTTP_X_FORWARDED_FOR
。我无法通过将自定义环境变量添加到重写规则来传递它们:
RewriteRule ... [P,QSA,L,E=foo:bar]
有什么想法吗?
The goal: running a Rails application on Mongrels, allowing access through Apache after doing basic HTTP Authentication
The problem: reading the supplied username from within Rails
Apache:
<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:4001
# ...
Order deny,allow
Deny from all
AuthType Basic
AuthName "<realm>"
AuthUserFile "<users-file>"
AuthGroupFile "<groups-file>"
Require group <group>
Satisfy Any
</Proxy>
RewriteEngine On
# ...
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
That works just fine, Apache forces the user to authenticate and forwards to Rails if successful. I omitted a few lines for handling static files and such, and triggering authentication for them as well.
The environment variables from Rails' perspective contain the usual entries and additionally HTTP_X_FORWARDED_HOST
, HTTP_X_FORWARDED_SERVER
and HTTP_X_FORWARDED_FOR
. I was unable to pass custom environment variables by adding them to the rewrite rule:
RewriteRule ... [P,QSA,L,E=foo:bar]
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
RequestHeader
指令将REMOTE_USER
放入 HTTP 标头中。这似乎对此线程中的人们有效:Try using the
RequestHeader
directive to putREMOTE_USER
in an HTTP header. This seems to have worked for the folks in this thread: