【问题标题】:Rails redirect fails on nginx & unicorn setupRails 重定向在 nginx 和 gunicorn 设置上失败
【发布时间】:2011-12-31 03:53:53
【问题描述】:

如 Railscasts 第 293 集所述,我已设置为在 nginx 和 unicorn 上运行。

当我尝试重定向时,比如

class PostsController < ApplicationController
  def show
    redirect_to posts_path, :notice => "Test redirect"
  end
end

我被重定向到 http://unicorn/posts 而不是 http://mydomain.com/posts

这是我的应用程序的 nginx.conf

upstream unicorn {
  server unix:/tmp/unicorn.scvrush.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  # server_name example.com;
  root /var/apps/current/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;
  }

  keepalive_timeout 5;
}

【问题讨论】:

  • 我有类似的设置,但不是location @unicorn,而是location /。你试过吗?
  • @MartinFrost 是的,没有帮助。在我看来,它使用 proxy_pass URL 作为基本 URL,而不是域名。
  • 您也可以发布您的./config/unicorn.rb 内容吗?例如带有listen 指令的行。

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


【解决方案1】:

这对我有用:

upstream unicorn {
  server unix:/tmp/unicorn.example.sock fail_timeout=0;
}

server {
  listen       80;
  listen       localhost;
  server_name  www.example.com;
  keepalive_timeout 5;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # this is required for HTTPS:                                                                                                                                                                                                                                             
    # proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

}

在我的 ./config/unicorn.rb 文件中:

# Listen on a Unix data socket                                                                                                                                                                                                                                                                                  
listen "/tmp/unicorn.example.sock", :backlog => 64

【讨论】:

    猜你喜欢
    • 2011-10-13
    • 2018-08-24
    • 2017-04-24
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2016-05-10
    相关资源
    最近更新 更多