Get server URL with Nginx proxy


(Christian Kaps) #1

Hi,

I use Play behind a Nginx proxy. It’s configured as suggested in the documentation. In my application I need to access the real server URL but I only get the upstream URL http://my-backend.

The Nginx setting proxy_set_header Host $http_host; is used to pass the server address in the HOST header. But it seems that Play ignores this header because it gets an absolute URL from the proxy.

Does anyone know how I can configure my proxy to pass the server URL to Play?

Best regards,
Christian


(Gustavo Santos) #2

Try with these:

                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;

(Christian Kaps) #3

Thanks for your answer. But it doesn’t work.


(Gustavo Santos) #4

I have this working on multiple apps, so it has to be one of the options below. If you say those 2 don’t work, it should be X-Forwarded-For.

                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header SCHEME $scheme;      # http/https
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

(Christian Kaps) #5

It only works if the proxy settings are located in the location directive.


(Christian Kaps) #6

I think I’ve found the issue. The documentation says:

These directives are inherited from the previous level if and only if there are no proxy_set_header directives defined on the current level. By default, only two fields are redefined:

And I have an additional proxy_set_header in my location directive. So the settings defined on the higher level will not be included.


(Gustavo Santos) #7

Yes I forgot about that. All these settings are defined inside the location / directive.