如何使用Nginx + Rails服务器处理动态子域?

时间:2017-10-13 11:25:50

标签: ruby-on-rails nginx puma

我在将子域名从nginx代理到rails服务器时遇到了问题。在我的rails应用程序中,我有tenant1.localhost:3000tenant2.localhost:3000等链接,它运行正常。在制作时我使用Nginx + Puma,如果我打开与子域名的链接,nginx不会代理任何请求。

nginx.conf

upstream puma_muninn {
  server app:3000;
}

server {

  listen 80;

  client_max_body_size 4G;
  keepalive_timeout 10;

  error_page 500 502 504 /500.html;
  error_page 503 @503;

  server_name localhost puma_muninn;
  server_name ~^(?<subdomain>.+)localhost$;
  root /var/www/muninn/public;
  try_files $uri/index.html $uri @puma_muninn;

  location @puma_muninn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://$subdomain.puma_muninn;
    # limit_req zone=one;
    access_log /var/www/muninn/log/nginx.access.log;
    error_log /var/www/muninn/log/nginx.error.log;
  }

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

  location = /50x.html {
    root html;
  }

  location = /404.html {
    root html;
  }

  location @503 {
    error_page 405 = /system/maintenance.html;
    if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
    rewrite ^(.*)$ /503.html break;
  }

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

  location ~ \.(php|html)$ {
    return 405;
  }
}

production.rb

config.action_dispatch.tld_length = 2

但正如我所说,彪马甚至没有得到nginx的请求。

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

nginx.conf中的域必须是特定的而不是localhost。