Ruby In Rack, what is env ['SCRIPT_NAME'] ???

clc3123 · 2012年03月02日 · 最后由 clc3123 回复于 2012年03月03日 · 4153 次阅读

和 PATH 有区别吗?

Rack 中貌似没有PATH这个东东。楼主的意思是系统环境变量里的PATH么?

Rack 中 env['SCRIPT_NAME']解释是:

## <tt>SCRIPT_NAME</tt>:: The initial portion of the request
##                        URL's "path" that corresponds to the
##                        application object, so that the
##                        application knows its virtual
##                        "location". This may be an empty
##                        string, if the application corresponds
##                        to the "root" of the server.

PEP333 也是这么说的。还有一个解释

the “base” of the URL, representing the root of the application.`

觉得这就是 App 启动后,URL 里的前缀。也可以说是项目的挂载路径吧。


比如,如果 config.ru 中为

run MyApp::Application
  1. 默认启动,首页地址为 http://locahost:3000/env['SCRIPT_NAME'] 的值为空
  2. 启动时加上--prefix=/p1参数(比如 thin start --prefix=/p1),首页地址为 http://localhost:3000/p1/env['SCRIPT_NAME'] 的值为 '/p1'

如果在config.ru中,写

map '/p2' do
  run MyApp::Application
end
  1. 默认启动,首页地址为 http://localhost:3000/p2/,那么 env['SCRIPT_NAME'] 的值就是 '/p2'
  2. 如果以类似 thin start --prefix=/p1 命令启动,首页地址为 http://localhost:3000/p1/p2/ ,那么 env['SCRIPT_NAME'] 的值就是 '/p1/p2'

#1 楼 @zhangyuan 太详细了,一看就明白了,谢谢!向你学习,多看源码多实践!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号