【发布时间】:2009-10-05 19:29:09
【问题描述】:
有没有根据部署类型修改路由的好方法?
基本上,我有一个具有 :requirements => {:protocol => "https"} 的路由,我希望它只发生在生产中,而不是在开发中。
【问题讨论】:
标签: ruby-on-rails deployment routing
有没有根据部署类型修改路由的好方法?
基本上,我有一个具有 :requirements => {:protocol => "https"} 的路由,我希望它只发生在生产中,而不是在开发中。
【问题讨论】:
标签: ruby-on-rails deployment routing
您可以单独显式定义它们并测试环境
if Rails.env.production?
map.resources :purchases, :requirements => {:protocol => "https"}
else
map.resources :purchases
end
注意,如果您使用的是旧版本的 Rails,请改用 ENV['RAILS_ENV'] == 生产
【讨论】:
Rails.env.development? 不会“更安全”吗?我希望我的产品是默认的,开发是例外。
在路由文件的顶部添加一个常量,例如:
ROUTES_PROTOCOL = (Rails.env.production? ? "https" : "http")
然后就这样做:
:protocol => ROUTES_PROTOCOL
对于需要 https 的路由
【讨论】:
最好坚持当前的协议。
如果您的生产环境涉及静态资产和 ssl 的 apache 或 nginx,请确保当客户端查询在 https 端口上时将 X-FORWARDED_PROTO https 标头发送给工作人员。
这样工作人员就会意识到 ssl 是在外部处理的,他们可以使用正确的协议生成链接。
我知道在 serverfault 上会比这里更好,但这里有一个示例 nginx 配置文件,它强制使用 https 并为 unicorn workers 中的 ssl 管理设置正确的标头:
upstream WEBAPP_NAME {
server unix:/path/to/webapp/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 4343;
server_name example.com;
root /path/to/webapp/public;
access_log /path/to/logs/nginx-access.log;
error_log /path/to/logs/nginx-error.log;
rewrite_log on;
ssl on;
# redirect when http request is done on https port
error_page 497 https://example.com:4343$request_uri;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location ~ ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header ETag "";
break;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-FORWARDED_PROTO https;
proxy_pass http://WEBAPP_NAME;
proxy_redirect default;
}
}
【讨论】: