ruby-on-rails – 为什么我无法通过nginx代理传递访问子域?

我想通过Nginx和RoR Web服务器(如Unicorn,Thin或WEBrick)在我的本地计算机上部署我的Ruby on Rails应用程序.

如下所示,我想通过post subdomain访问我的网络应用程序:

upstream sub {
  server unix:/tmp/unicorn.subdomain.sock fail_timeout=0;
#  server 127.0.0.1:3000;
}

server {
  listen   80;
  server_name post.subdomain.me;

  access_log /var/www/subdomain/log/access.log;
  error_log  /var/www/subdomain/log/error.log;
  root     /var/www/subdomain;
  index    index.html;

  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $http_host;
    proxy_redirect  off;
    try_files /system/maintenance.html $uri $uri/index.html $uri.html @ruby;
  }

  location @ruby {
    proxy_pass http://sub;
  }
}

一切正常,当我输入post.subdomain.me时,我可以看到我的RoR应用程序.

问题:当我使用post.subdomain.me url时,我无法访问我的子域(request.subdomain返回空,request.host返回subdomain instaed of subdomain.me).但是当我使用post.subdomain.me:3000时,每件事情都很完美(我失去了一半的头发才能意识到这一点).为什么以及如何解决?

解决方法:

当您使用端口访问应用程序时 – 您正在直接访问rails服务器,而不是由nginx代理,这适用于调试,但通常不适合生产.

客户端可能不会传递主机头,$host默认为nginx主机

尝试

location @ruby {
    proxy_set_header Host $host;
    proxy_pass http://sub;
}

和’硬编码’方式:proxy_set_header主机post.subdomain.me;

上一篇:nginx – Rails生产 – 新部署后所有图片都被破坏了


下一篇:nth-of-type