Display domain name instead of IP when using Nginx + Gunicorn + Django

Florian Dietz asked:

I am building a server on Ubuntu using Nginx + Gunicorn + Django.

I have a domain name registered with godaddy that redirects to the IP of the server. The Nginx configuration looks like this:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name mydomainname.com www.mydomainname.com localhost;

        location / {
            proxy_redirect off;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://unix:/path/to/my/socket.sock;
        }
    }

When I type the domain name in the browser, I get correctly redirected to the server. However, the url in the browser now displays the IP instead of the domain name.

How can I make the server display the domain name instead of the IP?

(Godaddy provides an option to do this by using forward masking, but I have been advised not to use this, and the browser complains about cross-origin problems anyway when I try this approach.)

I’m not even sure if I need to change something in Nginx or Gunicorn or Django.

My answer:


The sample configuration suggests that you also need to pass the Host: header upstream. For example, ripped out of that same configuration:

      proxy_set_header Host $http_host;
      # we don't want nginx trying to do something clever with
      # redirects, we set the Host: header above already.
      proxy_redirect off;

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.