Den*_*nis 9 ssl ruby-on-rails amazon-ec2
我有一个我在Heroku上构建的rails应用程序,我已将其配置为在那里使用SSL.现在我转向AWS EC2,我想让我的应用程序版本在没有SSL的情况下运行.完成后,我将在稍后添加SSL功能.
我的堆栈是Puma + Nginx + PostgreSQL,我正在使用Rails 4.2.4,Ruby 2.2.3和Capistrano 3.4.0.
我记得在我的应用程序中,我曾经插入过该行
config.force_ssl = true
Run Code Online (Sandbox Code Playgroud)
在config/environments/production.rb中.我评论了这一点,希望我的应用程序能够重新使用http.但事实并非如此:即使在评论该行后,每当我访问我的EC2公共IP(52.35.82.113)时,请求都会在端口80(http)上发送,并被重定向到端口443(https).
当我curl -v http://localhost在我的EC2实例上运行它返回时,可以更清楚地看到这一点:
* Rebuilt URL to: http://localhost/
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Sat, 12 Dec 2015 12:22:56 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Location: https://localhost/
<
* Connection #0 to host localhost left intact
Run Code Online (Sandbox Code Playgroud)
对于这些东西,我不是很有经验.我认为最初的问题是我的Nginx的配置,在我刚才的问题在这里有人建议我,有什么错我的Nginx的配置和重定向从Rails的到来.我怀疑是这种情况,因为我在Nginx中看不到可以强制执行重定向的任何内容,但如果您认为问题可能存在,那么您可以在上面的链接中看到很多相关代码.
除了上面打印的force_ssl之外,Rails中的其他内容可能导致重定向?
谢谢大家的帮助.如果您有任何疑问或需要更多信息,请与我们联系!
在Rails中,您可以使用配置文件强制站点范围的SSL(就像您所做的那样),或者您可以选择哪些端点将使用SSL并force_ssl在控制器级别使用类方法。
也许您在您的application_controller.rb或任何为根路径提供服务的控制器上使用了该方法,但忘记了它。您可以在 Rails 文档中找到此类机制的示例:http://api.rubyonrails.org/classes/ActionController/ForceSSL/ClassMethods.html
force_ssl(options = {}) Link 强制向此特定控制器发出请求或指定操作采用 HTTPS 协议。
如果您出于任何原因(例如开发)需要禁用此功能,则可以使用 :if 或 :unless 条件。
class AccountsController < ApplicationController
force_ssl if: :ssl_configured?
def ssl_configured?
!Rails.env.development?
end
end
Run Code Online (Sandbox Code Playgroud)