Why am I getting infinite redirect loop with force_ssl in my Rails app?

前端 未结 2 1221
鱼传尺愫
鱼传尺愫 2020-12-07 08:32

I want to have my API controller use SSL, so I added another listen directive to my nginx.conf

upstream unicorn {
  server unix:/tmp/unicorn.foo.sock fail_ti         


        
相关标签:
2条回答
  • 2020-12-07 09:01

    You're not forwarding any information about whether this request was an HTTPS-terminated request or not. Normally, in a server, the "ssl on;" directive will set these headers, but you're using a combined block.

    Rack (and force_ssl) determines SSL by:

    • If the request came in on port 443 (this is likely not being passed back to Unicorn from nginx)
    • If ENV['HTTPS'] == "on"
    • If the X-Forwarded-Proto header == "HTTPS"

    See the force_ssl source for the full story.

    Since you're using a combined block, you want to use the third form. Try:

    proxy_set_header X-Forwarded-Proto $scheme;
    

    in your server or location block per the nginx documentation.

    This will set the header to "http" when you come in on a port 80 request, and set it to "https" when you come in on a 443 request.

    0 讨论(0)
  • 2020-12-07 09:07

    Try setting this directive in your nginx location @unicorn block:

    proxy_set_header X-Forwarded-Proto https;

    I had this same issue and investigating the Rack middleware handler (not force_ssl but similar) I could see that it was expecting that header to be set to determine if the request was already processed as being SSL by nginx.

    0 讨论(0)
提交回复
热议问题