【问题标题】:How do I handle dynamic subdomains with Nginx + Rails server?如何使用 Nginx + Rails 服务器处理动态子域?
【发布时间】:2017-11-10 18:37:36
【问题描述】:

我在将子域名称从 nginx 代理到 rails 服务器时遇到问题。在我的 Rails 应用程序中,我有 tenant1.localhost:3000、tenant2.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

但正如我所说,puma 甚至没有收到来自 nginx 的请求。

有什么想法吗?

【问题讨论】:

  • 请同时添加 nginx.error.log 日志以便更好地跟踪您的错误。
  • nginx.error.log 为空

标签: ruby-on-rails nginx puma


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2015-03-20
    • 2018-11-29
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    相关资源
    最近更新 更多