Why does ActionDispatch :: Request.original_url return the wrong schema?

I have a Ruby on Rails application that has the following in its template:

<form action="<%= request.original_url %>" method="post">

When I request a page over https, I get a form action that has the same url as the original request, but instead https://

it is generated as http://

, resulting in mixed content errors.

What am I missing? Under what circumstances request.original_url

will it return the wrong schema?

I am running Ruby on Rails with Unicorn and nginx.

+3


source to share


2 answers


It looks like this might be the solution to the problem:

http://blog.seancarpenter.net/2013/09/02/rails-ssl-route-generation-with-nginx-and-unicorn/

Since the site is running in nginx, nginx terminates SSL, and Rails doesn't know that it is.



To pass this information to Rails, the nginx configuration needs to be configured to add a header X-Forwarded-Proto https

as it forwards the request to the application server.

The configuration example from the above article shows ...

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; }

+4


source


You can remove the http part like this request.original_url[4...-1]

It will remove the last character as well, but you don't need the last backslash anyway.



Then you get your https: // by writing "https#{request.original_url[4...-1]}"

and if you want a trailing backslash just add it.

0


source







All Articles