Omniauth Nginx Unicorn callback to wrong host URL

2019-08-07 14:39发布

问题:

I've deployed a Rails app to a VPS server, and I'm using the Nginx/Unicorn combo, everything works fine, except that for some reason beyond my understanding, the Omniauth callbacks redirect wrong,

ie.

http://unicorn/users/auth/linkedin/callback?oauth_token=95218ed3-b426-45ab-b022-693d2a2447cb&oauth_verifier=25955

it should instead be:

http://my-real-domain.com/users/auth/linkedin/callback?oauth_token=95218ed3-b426-45ab-b022-693d2a2447cb&oauth_verifier=25955

What's wrong? why is the callback using the name of the upstream defined in nginx?

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

server {
  listen 80;
  listen [::]:80 ipv6only=on default_server;

  root /home/deploy/work/project/current/public;
  index index.html index.htm;

  server_name my-real-domain.com;

  try_files $uri/index.html $uri @unicorn;

  location @unicorn {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Client-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;

  location ~ ^/assets/ {
    expires 1y;
    add_header Cache-Control public;

    add_header ETag "";
    break;
  }
}

Could you please help me? I need to know how to overcome this wrong redirection.

Thanks in advance!

回答1:

Nginx doesn't pass the host header by default, you have to tell it to:

 location @unicorn {
    proxy_set_header Host $http_host; 
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Client-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass http://unicorn;
  }

Otherwise which host the request was sent to gets lost.