【问题标题】:Unexpected behavior with resource_url on Nginx and UnicornNginx 和 Unicorn 上的 resource_url 出现意外行为
【发布时间】:2012-08-31 13:15:44
【问题描述】:

我在 cpanel 上有一个简单的 unicorn+nginx 设置,使用 capistrano 部署。 nginx.conf 服务器块如下所示:

server {
  error_log /var/log/nginx/vhost-error_log warn;
  listen 123.456.789.0;
  server_name my.dev.site.com www.my.dev.site.com;
  access_log /usr/local/apache/domlogs/my.dev.site.com-bytes_log bytes_log;
  access_log /usr/local/apache/domlogs/my.dev.site.com combined;
  root /home/me/sites/dev/current/public;
  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_pass http://unicorn_dev_site;
  }
  error_page 500 502 503 504 /500.html;
}

在此设置下,对 User 之类的资源的任何引用都会在 url 帮助程序中产生奇怪的行为。

<%= users_path %> 按预期变为my.dev.site.com/users

<%= users_url %> 变为 unicorn_dev_site/users

是什么原因造成的,这两个帮助方法之间有什么区别提示这种行为?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 nginx unicorn


    【解决方案1】:

    这是您的代理。 *_path 助手生成表单的字符串

    /path
    

    *_url 帮助器生成类似的字符串

    http(s)://h.o.s.t/path
    

    因此,多亏了您的代理设置,当请求到达 Rails 时,它已经过调整,主机被列为 http://unicorn_dev_site,Rails 忠实地复制了它生成的 URL。

    也就是说,除非您有多个主机,否则通常不需要使用 *_url 助手而不是 *_path 助手。但是,如果您想使用它们,您有几个选择:您可以在控制器中使用 set the default host,也可以使用 :host 选项以 url_for 为每个 URL 设置它(我希望您可以很容易地将其扩展到 *_url 助手,但我不确定)。

    无论如何 - 希望有帮助!

    【讨论】:

    • 我也遇到了这个问题。非常感谢!
    • 感谢您的提示。我解决了这个问题,更改了proxy_pass 名称,这样rails 就会得到正确的名称。
    猜你喜欢
    • 2021-08-06
    • 2021-10-02
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多