我有一个非常奇怪的问题,每次我尝试在非SSL模式下浏览我的Rails应用程序时,Chrome(v16)和Firefox(v7)会不断强制我的网站以HTTPS的forms提供服务。
我的Rails应用程序部署在使用Capistrano,nginx,Passenger和通配符SSL证书的Ubuntu VPS上。
我在nginx.conf中为端口80设置了这些参数:
passenger_set_cgi_param HTTP_X_FORWARDED_PROTO http; passenger_set_cgi_param HTTPS off;
我的nginx.conf的长版本可以在这里find: https : //gist.github.com/2eab42666c609b015bff
ssl-redirect.include文件包含:
rewrite ^/sign_up https://$host$request_uri? permanent ; rewrite ^/login https://$host$request_uri? permanent ; rewrite ^/settings/password https://$host$request_uri? permanent ;
当来自非SSL请求时,确保这三个页面使用HTTPS。
我的production.rb文件包含这一行:
# Enable HTTP and HTTPS in parallel config.middleware.insert_before Rack::Lock, Rack::SSL, :exclude => proc { |env| env['HTTPS'] != 'on' }
我已经尝试通过nginx重写redirect到HTTP,Ruby on Railsredirect,也使用Rails视图使用HTTP协议的URL。
我的application.rb文件包含在before_filter钩子中使用的这个方法:
def force_http if Rails.env.production? if request.ssl? redirect_to :protocol => 'http', :status => :moved_permanently end end end
每次尝试redirect到HTTP非SSL时,浏览器都会尝试将其redirect回HTTPS,从而导致无限redirect循环。 然而,Safari浏览器工作得很好。 即使当我禁止在nginx中提供SSL时,浏览器仍然尝试使用HTTPS连接到站点。 我还应该提到,当我将应用程序推送到Heroku时,Railsredirect工作对于所有浏览器都很好。
我想使用非SSL的原因是我的主页包含非安全的dynamicembedded对象和非安全的CDN,我想要防止安全警告。
我不知道是什么导致浏览器保持强制HTTPS请求。
如果你在你的环境configuration中使用了config.force_ssl = true ,然后关掉它,你的浏览器仍然只能通过ssl进行连接。
当force_ssl为true时,Rails会发送一个HSTS头 ,这将导致一些浏览器只允许通过HTTPS连接到有问题的域,不pipe在地址栏中input什么内容。 默认情况下,该设置将被浏览器caching1年。
在这个博客文章中看到一些如何避免这个问题的提示: http : //www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/#comment-40447
我发现了一种方法来closures所有地方的Wordpress支持论坛上的答案HSTS: https : //wordpress.org/support/topic/want-to-turn-off-http-strict-transport-security-hsts -header#后6068192
您可以发回一个头,将closuresHSTScaching。 使用Rails 4应用程序中的before_filter示例在Chrome中进行testing:
response.headers['Strict-Transport-Security'] = 'max-age=0; includeSubDomains'