为什么ActionDispatch::Request.original_url return scheme不对?
Why does ActionDispatch::Request.original_url return the wrong scheme?
我在 Rails 应用程序上有一个 Ruby,它在模板中包含以下内容...
<form action="<%= request.original_url %>" method="post">
当我通过 https 请求页面时,我最终得到一个与原始请求具有完全相同的 url 的表单操作,但生成的不是 https://
,而是 http://
, 导致混合内容错误。
我错过了什么?什么情况下会request.original_url
return错误方案?
我 运行 Ruby Rails 使用 Unicorn 和 nginx。
看起来这可能是问题的解决方案:
http://blog.seancarpenter.net/2013/09/02/rails-ssl-route-generation-with-nginx-and-unicorn/
因为站点运行在nginx中,nginx正在终止SSL,而Rails不知道是这种情况。
要将此信息传递给 Rails,需要设置 nginx 配置,以便在将请求转发到应用服务器时添加 X-Forwarded-Proto https
header。
上述文章中的示例配置显示...
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https; # New header for SSL
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn_something_server;
}
您可以像这样删除 http 部分
request.original_url[4...-1]
它还会删除最后一个字符,但无论如何您都不需要最后一个反斜杠。
然后你通过写得到你的https://
"https#{request.original_url[4...-1]}"
如果您需要结束反斜杠,只需添加即可。
我在 Rails 应用程序上有一个 Ruby,它在模板中包含以下内容...
<form action="<%= request.original_url %>" method="post">
当我通过 https 请求页面时,我最终得到一个与原始请求具有完全相同的 url 的表单操作,但生成的不是 https://
,而是 http://
, 导致混合内容错误。
我错过了什么?什么情况下会request.original_url
return错误方案?
我 运行 Ruby Rails 使用 Unicorn 和 nginx。
看起来这可能是问题的解决方案:
http://blog.seancarpenter.net/2013/09/02/rails-ssl-route-generation-with-nginx-and-unicorn/
因为站点运行在nginx中,nginx正在终止SSL,而Rails不知道是这种情况。
要将此信息传递给 Rails,需要设置 nginx 配置,以便在将请求转发到应用服务器时添加 X-Forwarded-Proto https
header。
上述文章中的示例配置显示...
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https; # New header for SSL
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn_something_server;
}
您可以像这样删除 http 部分
request.original_url[4...-1]
它还会删除最后一个字符,但无论如何您都不需要最后一个反斜杠。
然后你通过写得到你的https://
"https#{request.original_url[4...-1]}"
如果您需要结束反斜杠,只需添加即可。