首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >代理后面的rails应用程序的https重定向?

代理后面的rails应用程序的https重定向?
EN

Stack Overflow用户
提问于 2012-05-03 21:19:48
回答 1查看 14.2K关注 0票数 20

我的nginx.conf中的服务器声明:

    listen       1.2.3.4:443 ssl;
    root /var/www/myapp/current/public;
    ssl on;
    ssl_certificate /etc/nginx-cert/server.crt;
    ssl_certificate_key /etc/nginx-cert/server.key;
    location / {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect off;

          if (!-f $request_filename) {
            proxy_pass http://upstreamy;
            break;
          }
     }

Nginx.conf中的上游声明:

upstream upstreamy {
    server unix:/var/www//myapp/shared/sockets/unicorn.sock fail_timeout=0;
}

这很好用,我的应用程序可以作为https://somehost访问

但应用程序会生成http url用于重定向,例如,当使用devise进行身份验证时,/会被重定向到http://somehost/user/sign_in,而不是https (从rails应用程序的角度来看,无论如何都是http )。

我试过了

proxy_pass https://upstreamy;

但这只是试图加密nginx和运行rails应用程序的独角兽之间的流量。

我也尝试过了,在application_helper.rb中:

# http://stackoverflow.com/questions/1662262/rails-redirect-with-https
def url_options
  super
  @_url_options.dup.tap do |options|
  options[:protocol] = Rails.env.production? ? "https://" : "http://"
  options.freeze
end

但它似乎不起作用。

怎么解决这个问题呢?

编辑:所以,我们的目标不是让rails应用程序需要ssl,或者强制使用ssl;目标是让rails应用程序在重定向时生成https:// urls……(我认为所有其他urls都是相对的)。

EN

回答 1

Stack Overflow用户

发布于 2012-05-03 21:46:16

您可以将:protocol => "https"传递给redirect_to。

您可以通过将以下内容添加到application.rb中将其设置为缺省值

Rails.application.routes.default_url_options[:protocol]= 'https'

参考:https://stackoverflow.com/a/6101147/446203

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10432266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档