【问题标题】:how to Handle Subdomain in rails with unicorn, and nginx如何使用独角兽和 Nginx 处理 Rails 中的子域
【发布时间】:2013-05-30 23:06:52
【问题描述】:

我在multitenant 上关注了一个简单的 railscasts 插曲 现在我的应用程序中的所有租户都进入了他们在我的开发系统上本地工作的子域,但是当我在我的 vps 系统上尝试相同的过程时,浏览器说找不到服务器。

我已将域连接到我的 ip 并修改了我的 nginx 文件,但仍然没有希望

一旦用户进入他的子域,请求就不会到达我的 rails 应用程序。

对此有任何想法,否则我可能做错了。谢谢

我的 nginx_unicorn_file

upstream unicorn {
  server unix:/tmp/unicorn.<%= application %>.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name shopnany.com *.shopnany.com;
  root <%= current_path %>/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 unicode nginx ruby-on-rails-3.2


    【解决方案1】:

    我这样做没有问题。首先,您需要在您使用的任何 DNS 提供商中创建一个 A 记录。例如创建一个 DNS 记录:

    it.shopnany.com

    然后你必须为这个特定的部署更新你的 nginx 配置模板。可以如下:

    upstream unicorn_<%= application %> {
      server unix:/tmp/unicorn.<%= application %>.sock fail_timeout=0;
    }
    
    server {
      listen 80;
      server_name it.shopnany.com;
      root <%= current_path %>/public;
    
      location ~ ^/assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
    
      try_files $uri/index.html $uri @unicorn;
      location @unicorn {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://unicorn_<%= application %>;
      }
    
      error_page 500 502 503 504 /500.html;
      client_max_body_size 4G;
      keepalive_timeout 10;
    }
    

    这对我有用。现在我有两个不同的应用程序在我的 www 和 it 子域上运行。

    【讨论】:

    • 经过深入研究,我发现我必须为 c 记录创建通配符 * 这样,客户可以在我不知道的情况下创建多个子域,因此我不必为单个客户创建 c 记录.如果主应用程序找不到子域,它会处理对主应用程序的请求。很简单
    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2012-09-11
    • 2020-10-17
    • 2012-09-17
    • 2015-12-01
    • 1970-01-01
    相关资源
    最近更新 更多