Search code examples
nginxsails.jspassport.jspassport-twitter

Sails.js + passport behind Nginx proxy


I've started a new Sails.js project and want to implement passport-twitter for authentication.

Everything is implemented correctly using the sails-generator-auth module, but after Twitter authorization I get redirected to

http://localhost:9090/auth/twitter/callback

Sails is running behind nginx on localhost:9090 - how can I override the hostname?

So far I tried setting app.set('trust proxy'); in config/env/development.js

   http: {
    customMiddleware: function(app) {
      console.log('trust proxy');
      app.enable('trust proxy');
    }
  },

I see the console.log output when I lift sails, but it doesn't affect the redirection.


Solution

  • The answer is simple, but not very well documented. There's another post which actually talks about a slighty different problem, but leads to the correct answer: How to get current domain address in sails.js

    If you're on Sails v0.10.x, you can use sails.getBaseurl() to get the full protocol, domain and port that your app is being served from. Starting with Sails v0.10.0-rc6, this also checks the sails.config.proxyHost and sails.config.proxyPort, which you can set manually in your one of your config files (like config/local.js) if your app is being served via a proxy (e.g. if it's deployed on Modulus.io, or proxied through an Nginx server).

    It is correct to configure a sails instance to localhost:9090, but if you want to change the hostname/port behind a proxy, you have to configure proxyHostand proxyPort - this can be done anywhere, but is most suitable in local.js