【问题标题】:How to handle nginx redirect to a mounted Rails engine如何处理 nginx 重定向到已安装的 Rails 引擎
【发布时间】:2015-04-08 21:03:58
【问题描述】:

我有一个 rails 4 应用程序,其引擎安装在单独的域上,如下所示:

mount Test::Engine => '/', as: 'test', constraints: { domain: 'test_engine.com' }

应用程序本身托管在 DigitalOcean 液滴上。它使用不同的域,比如application.com

我需要几个域(test_domain1.com、test_domain2.com)来打开 rails 引擎。 我想我可以通过将所有域 DNS A 记录(机器人裸域和 www.*)配置为指向 droplet IP 并让 nginx 将它们重定向到 test_engine.com 来实现这一点。

nginx.conf的要点是:

server {
  listen 80;
  server_name test_domain1.com www.test_domain1.com test_domain2.com www.test_domain2.com;
  return 307  https://test_engine.com$request_uri;
}

server {
  listen 80 default_server;
  server_name application.com www.application.com;
  return 307  https://application.com$request_uri;
}

不幸的是,重定向似乎无法正常工作,例如,访问www.test_domain1.com 我最终到达https://application.com 而不是https://test_engine.com

我意识到这是非常具体的,可能会有更好的解决方案。 提前感谢您的帮助。

【问题讨论】:

  • 你想要 301 而不是 307。这只是在这里发帖的错字吗?
  • 301 会设置永久重定向,破坏简单的测试。 307301与功能无关,
  • 你用 curl/wget 测试过吗?

标签: ruby-on-rails ubuntu nginx


【解决方案1】:

我最终使用正则表达式匹配服务器名称 a-la

server {
  listen 80;
  server_name ~test_domain;
  return 307  https://test_engine.com$request_uri;
}

硬重启 nginx 使用 sudo service nginx restart 而不是 sudo service nginx reload

【讨论】:

    猜你喜欢
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多