Ruby on Rails中的奇数重定向

时间:2014-01-05 00:16:09

标签: ruby-on-rails ruby-on-rails-3.2 memcached rails-activerecord rails-routing

我一般都会遇到奇怪的重定向。它适用于开发但不适用于生产。以下是更新的回复。创建时也会发生同样的事情。

Redirected to http://localhost:3000/trackers
Completed 302 Found in 43ms (ActiveRecord: 18.7ms)
Started GET "/trackers" for 127.0.0.1 at 2014-01-06 06:41:02 -0600

PRODUCTION

Redirected to http://example.com/trackers
Completed 302 Found in 218ms (ActiveRecord: 63.9ms)
Started GET "/" for [IP} at 2014-01-05 20:15:33 +0000

routes.rb(pertinant)

              trackers GET    /trackers(.:format)                       trackers#index
                       POST   /trackers(.:format)                       trackers#create
           new_tracker GET    /trackers/new(.:format)                   trackers#new
          edit_tracker GET    /trackers/:id/edit(.:format)              trackers#edit
               tracker GET    /trackers/:id(.:format)                   trackers#show
                       PUT    /trackers/:id(.:format)                   trackers#update
                       DELETE /trackers/:id(.:format)                  trackers#destroy

这是我的智能设备控制器

class TrackersController < ApplicationController
 before_filter :authenticate_user!
load_and_authorize_resource

def create
  @tracker = params[:tracker]
  @user = current_user
  if @user.trackers.create(@tracker)
   redirect_to trackers_path, :notice => "New Tracker Created!"
  else
   redirect_to trackers_path, :notice => "Tracker Could not be created."
  end
end

def update
  @tracker = Tracker.find(params[:id])
  if @tracker.update_attributes(params[:tracker])
   redirect_to trackers_path, :notice => "Tracker Updated!"
  else
   redirect_to trackers_path(params[:id]), :notice => "Unable to Update Tracker"
  end
end

end

NGINX

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


server {
listen      80;
server_name example.com;
return 301 https://$host;}


server {

listen 443 default;
server_name example.com;
root /home/a/apps/legalleads/public;
try_files $uri/index.html $uri @unicorn;

ssl on;
ssl_certificate /etc/nginx/ssl/com.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;


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

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;
}

error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}

2 个答案:

答案 0 :(得分:1)

您以一种与HTTP规范不兼容的方式将呈现和重定向链接在一起。从您提供的控制器的有限部分开始,我假设在登录时发出POST请求,该请求会反馈到重定向,然后重定向到另一个重定向或呈现< / em>的。最后的重定向/渲染调用是罪魁祸首。

  

如果收到302状态代码以响应除以外的请求   GET或HEAD,用户代理不得自动重定向   除非可以由用户确认,否则请求,因为这可能   改变发出请求的条件。

来自HTTP/1.1 Status Code Definitions

您应该跟踪所有回调中的逻辑,以确保在POST请求结束时只调用一次渲染/重定向。

答案 1 :(得分:1)

法拉兹,非常感谢你对此的暗示。这是帮助我找到答案的解决方案和博客

http://blog.seancarpenter.net/2013/09/02/rails-ssl-route-generation-with-nginx-and-unicorn/

我不得不告诉nginx保留https。这就是为什么有些时候它会工作,有时它不会。

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;
}

不得不改为

location @unicorn {
  proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header    X-Forwarded-Proto https; # New header for SSL
  proxy_set_header    Host $http_host;
  proxy_redirect      off;
  proxy_pass          http://unicorn_something_server;
}