Ruby on rails:force_ssl 在使用 Thin start --ssl 时不会从 http 重定向到 https

问题描述:

我一直在尝试将我的 Rails 项目配置为使用 SSL(作为应用程序范围)和 Thin(通过添加 Thin gem 并将 config.force_ssl = true 放置到 application.rb) 但结果,我遇到了一个烦人的问题.

I have been trying to configure my rails project to use SSL (as application wide) with thin (by adding thin gem and placing config.force_ssl = true to application.rb) but as a result, I have an annoying problem.

当我使用 rails s 启动 Rails 服务器并尝试访问 http://localhost:3000 时,它重定向到 https://localhost:3000 这是正确的,但是它会抱怨缺少 SSL 认证..

When I start my rails server using rails s and try to access http://localhost:3000 it redirects to https://localhost:3000 which is correct, however it will complain about missing SSL certification..

SSL 连接错误

无法与服务器建立安全连接.这可能是一个服务器有问题,或者可能需要客户端您没有的身份验证证书.

Unable to make a secure connection to the server. This may be a problem with the server, or it may be requiring a client authentication certificate that you don't have.

另一方面,如果我使用 thin start --ssl 启动我的应用程序并尝试访问 http://localhost:3000 它不会重定向到https://localhost:3000 并抱怨这个:

On the other hand, If I start my application using thin start --ssl and try to access http://localhost:3000 it doesn't redirect to https://localhost:3000 and complains about this:

未收到数据

无法加载网页,因为服务器没有发送数据.

Unable to load the webpage because the server sent no data.

但是通过访问 https://localhost:3000 它只显示证书警告,但在同意继续后一切正常.

but by accessing https://localhost:3000 it just shows certificate warning but it all works after agreeing to continue.

所以我的问题是,在(开发模式)中使用 SSL 和精简版时,如何使 http 到 https 重定向工作?

So my question is, How can I make http to https redirection work when using SSL with thin in (development mode)?

完美的答案将包含使用 rails s 正常启动服务器的可能性,而 SSL 将在应用程序范围内启用并访问 http 重定向到 https 并且一切正常!:)

Perfect answer would contain possibility to start server normally by using rails s while SSL would be enabled application wide and accessing http redirects to https and all works! :)

提前致谢,已经搜索了几个小时但找不到答案.

Thanks in advance, have been searching for hours but cant find an answer.

使用 ngrok (https://ngrok.com/).免费.安装很简单.

Use ngrok (https://ngrok.com/). It's free. Installation is easy.

只需启动您的服务器,然后在另一个终端窗口中运行

Just start up your server, then in another terminal window run

ngrok http 3000

ngrok 会给你一个循环回到你的开发机器的地址——比如 http://randomstringhere.ngrok.io

ngrok will give you an address that loops back to your dev machine - something like http://randomstringhere.ngrok.io

把它放在你的浏览器中,你会看到它重定向到 https://randomstringhere.ngrok.io,仍然通过端口 3000 路由到您的服务器

Put that in your browser and you'll see it redirect to https://randomstringhere.ngrok.io, still routing to your server on port 3000