【问题标题】:Rails redirect_to is redirecting to https://subdomainRails redirect_to 正在重定向到 https://subdomain
【发布时间】:2018-09-19 11:10:02
【问题描述】:

我刚刚在我的网站上设置了 cloudflare ssl,现在每当在 rails 中调用 redirect_to @thing 时,客户端都会重定向到 https://subdomain/thing/1 而不是 https://subdomain.domain.com/thing/1。不确定我是否需要更改 cloudflare A 记录,因为我在设置了 ssl 之后,因为我在 CNAME | subdomain | host 之前拥有的 A 记录不再工作了。如果需要更多信息,请告诉我!

Rails 服务器是这样启动的 rails s -p 3000 -b0.0.0.0 -e production

DNS 记录是 CNAME | subdomain | host A | domain | serverip A | www | serverip

rails 服务器在serverip:3000 运行,而 nginx 正在将具有特定子域的所有流量路由到此端口。如果一切都使用serverip:3000 完成,则没有重定向问题,但是当我使用subdomain.domain.com 执行操作时,重定向问题再次出现

nginx 配置 ```

server {
    listen 80;
    server_name subdomain.domain.com;
    location / {
        proxy_pass http://subdomain;
    }
}

upstream subdomain {
    server 127.0.0.1:3000;
}

```

【问题讨论】:

  • 正确设置您的 routes.rb - 请参阅此处作为示例:tomdallimore.com/blog/…
  • 我不认为这是一个编码问题,而更多的是 cloudflare 的配置问题。 rails 服务器在serverip:3000 运行,nginx 正在将具有特定子域的所有流量路由到此端口。如果使用serverip:3000 完成所有操作,则不会出现重定向问题,但是当我使用subdomain.domain.com 执行操作时,重定向问题会再次出现。

标签: ruby-on-rails ssl nginx subdomain cloudflare


【解决方案1】:

您可以添加以下更改routes.rb

constraints subdomain: false do
  get ':any', to: redirect(subdomain: 'www', path: '/%{any}'), any: /.*/
end

您可以将顶级域的数量设置为子域的参数,例如:

>> app.get('http://www.localhost')
>> app.request.subdomain
=> "" 
>> app.request.subdomain(0)
=> "www" 

这也可以在环境文件中设置:

# config/environments/development.rb
config.action_dispatch.tld_length = 0

【讨论】:

  • 我不认为这是一个编码问题,而更多的是 cloudflare 的配置问题。 rails 服务器在 serverip:3000 运行,nginx 正在将具有特定子域的所有流量路由到此端口。如果一切都使用 serverip:3000 完成,则没有重定向问题,但是当我使用 subdomain.domain.com 执行操作时,重定向问题再次出现。
猜你喜欢
  • 2014-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多