mrvautin
mrvautin

Reputation: 300

Nginx - Proxy request to Heroku

I'm trying to setup an Nginx proxy which will proxy all incoming requests to an Heroku app. The idea is to allow customers of my SaaS app to be able to use custom domains. A customer will setup a CNAME to my proxy and it will set the Host header and proxy the request to Heroku.

So far I have this Nginx config:

location / {
        proxy_set_header Host $host;
        proxy_pass http://myapp.herokuapp.com;
}

Heroku requires that the Host header is set to determine which application to use.

The requests are getting to Heroku but it seems the Host header is not being set.

Any ideas?

Upvotes: 3

Views: 1952

Answers (1)

mrvautin
mrvautin

Reputation: 300

Answering my own question here...

Managed to go for this approach:

location / {
    proxy_set_header    Host $host;
    proxy_set_header    X-Real-IP $remote_addr;
    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header    Host $host-customdomain.mydomain.com;
    proxy_redirect      off;
    proxy_pass          http://my_heroku_app_name.herokuapp.com;
}

I then trimmed -customdomain.mydomain.com from the Host header when it gets to my Heroku app and then my app knows which customer the request is for. Hopefully this helps someone. I've written a blog post with more detail: https://mrvautin.com/enabling-custom-domain-for-saas-application-on-heroku/

Upvotes: 6

Related Questions