移动到Digital Ocean后,Rails无法访问标头 [英] Rails Not able to access headers after moving to Digital Ocean

查看:113
本文介绍了移动到Digital Ocean后,Rails无法访问标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为网络和Android客户端提供服务的Rails应用程序.我最近从1.9.3升级到了Ruby 2.0.我在Android客户端上进行了一些工作,并不断从API获得状态500.我通过在标头中发送他们的auth_token来返回每个用户的JSON数据.

I have a rails app that serves the web and an Android client. I recently upgraded to Ruby 2.0 from 1.9.3. I went to do some work on the Android client and kept getting status 500 from the API. I return JSON data for each user by sending their auth_token in the headers.

我检查了服务器日志以查看发生了什么.此方法返回nil:

I checked the server logs to see what was up. This method was returning nil:

def api_user
  my_auth = request.headers["auth_token"]
  api_user = User.where(authentication_token: my_auth).first
end

我没有碰过我的Android客户端,所以我知道问题不存在.

I hadn't touched my Android client so I knew the problem wasn't there.

我尝试了curl --header "auth_token: xxxxxxxxxxxxxxxxxxxx" https://myapp.com/api/etc.json 并获得500的地位.

I tried curl --header "auth_token: xxxxxxxxxxxxxxxxxxxx" https://myapp.com/api/etc.json And got status 500 as well.

我最近对Rails应用程序做了很多更改,并从Heroku移到了Digial Ocean,所以我认为也许我在此过程中搞砸了一些事情.我在本地尝试过:

I recently made a bunch of changes to the rails app and moved from Heroku to Digial Ocean, so I figured that maybe I'd messed something up in the process. I tried locally:

curl --header "auth_token: xxxxxxxxxxxxxxxxxxxx" http://localhost:3000/api/etc.json

它返回指定用户的数据!

And it returned data for the specified user!

我真的很困惑.我尝试登录生产以查看标头是否完全存在:

I'm really confused. I tried logging in production to see if the headers were there at all:

Rails.logger.info "auth_token #{request.headers["auth_token"]}"

在开发中,它应生成了auth_token xxxxxxxxxxxxxxxx.

In development this produced auth_token xxxxxxxxxxxxxxxx as it should.

在生产中,此返回auth_token.因此,我无法在生产中访问标题.为什么会这样?

In production this returned auth_token. So I'm not able to access the headers in production. Why is this?

设置信息:运行Dokku的DigitalOcean小滴上的独角兽.让我知道您是否需要更多信息-我真的很困惑....

Setup info: unicorn on a DigitalOcean droplet running Dokku. Let me know if you need more info - I'm really stumped....

其他信息 Procfile

ADDITIONAL INFO Procfile

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: bundle exec rake jobs:work

config unicorn.rb

config unicorn.rb

worker_processes 3
timeout 3000
preload_app true

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

推荐答案

好的,这最终与nginx有关.我在此处.

Okay, this ended up being something related to nginx. I found the answer here.

基本上,您必须在http {之后紧接在nginx.conf文件中添加以下行,才能在标头中使用下划线.

Basically, you have to add the line below to your nginx.conf file right after http {to use underscores in headers.

underscores_in_headers on;

有效.

这篇关于移动到Digital Ocean后,Rails无法访问标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆