Rails Not able to access headers after moving to D

2019-08-12 06:42发布

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.

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

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

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

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"]}"

In development this produced auth_token xxxxxxxxxxxxxxxx as it should.

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

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

ADDITIONAL INFO Procfile

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

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

1条回答
放我归山
2楼-- · 2019-08-12 07:17

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

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;

It works.

查看更多
登录 后发表回答