在HTTPS中设计confirmation_url

Don*_*lio 3 ssl ruby-on-rails nginx devise

我的网站完全是 SSL,所以我还希望由 devise (3.2.2) 生成用于电子邮件验证的网址https://....

目前,url 是通过以下方式生成的:

confirmation_url(@resource, :confirmation_token => @token)
Run Code Online (Sandbox Code Playgroud)

它会产生漂亮的网址,例如:

http://example.com/users/confirmation?confirmation_token=zqfHS35ckLQZscSbsgMm
Run Code Online (Sandbox Code Playgroud)

我希望网址是

https://example.com/users/confirmation?confirmation_token=zqfHS35ckLQZscSbsgMm
Run Code Online (Sandbox Code Playgroud)

另外,目前电子邮件验证不起作用,因为 nginx 会重定向到https每个页面的等效内容,并且由于某些原因,事情会变得混乱,并且https版本是损坏的网址,例如:

https://example.com/users/confirmation?confirmation_token=zqfHS35ckLQZscSbsgMm?confirmation_token=zqfHS35ckLQZscSbsgMm
Run Code Online (Sandbox Code Playgroud)

由于某些原因,nginx 会重定向到这个损坏的 url,因此 Unicorn 只能拒绝该请求。

有什么线索吗?

Tim*_*Tim 5

您可以在电子邮件模板中指定协议,就像您在自己的答案中所做的那样,也可以在邮件程序中将其指定为默认协议。如果您愿意所有电子邮件都使用 https 链接,最简单的方法是将其添加到您的应用程序配置中。例如,在您的 Production.rb 中:

\n\n
  config.action_mailer.default_url_options = {:protocol => \'https\', :host => \'example.com\'}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我知道如果你直接访问 https 就不再重要了,但是 nginx 从 http 重定向到 https 后的 url 看起来像是将查询字符串附加到整个 url 中,所以值得修复这个问题因此,即使您不再需要它来处理电子邮件,它也适用于所有情况。如果您在 nginx 配置中使用return 301 \xe2\x80\xa6语句,则可能有尾随$query_string$args您不需要 - 例如,如果您使用的语句$request_uri中已经包含 GET 参数。

\n\n

另外,我认为您不会在任何地方找到直接定义的confirmation_url。如果您尝试,rake routes您可能会看到其中之一是:

\n\n
user_confirmation GET    /users/confirmation(.:format)     {:controller=>"devise/confirmations", :action=>"show"}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这意味着将自动有一个 user_confirmation_url 助手可用,就像一般的路由一样。我认为 devise 允许您使用confirmation_url,因为它巧妙地跟踪您正在使用的范围(在本例中为“用户”),尽管我必须承认我在 devise 中还没有足够地查看代码的路由以确切地了解它如何为路由执行此操作。

\n