Setting up nginx to support custom domain name
Asked Answered
M

1

6

I have a Django web application hosted on a VM with the Debian-based Ubuntu as the OS, and nginx reverse proxy + gunicorn as the webserver.

The DNS of this web application is myapp.cloudapp.net. I also have a ccTLD mydomain.pk I need to be configured as a custom domain name for this web application.

My original registrar only supported nameservers. Thus I made an account on dns.he.net (a free DNS hosting provider) to host my nameservers, and set up the CName for my machine.

My problem is that once I set up the CName to point to my web app's DNS, entering mydomain.pk in the browser merely shows me a generic Welcome to ngnix! page. Whereas, entering myapp.cloudapp.net (or myapp.cloudapp.net:80) in the browser correctly opens up the web application. Why isn't setting up the CName working?

I've talked to the support staff at dns.he.net - I've been told my CName is set up correctly, and that there might be some problem with my nginx configuration.

For instance, here's my etc/nginx/sites-available/myproject file:

server {
    listen 80;
    server_name myapp.cloudapp.net;
    charset utf-8;
    underscores_in_headers on;
    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {

        root /home/myuser/projectpk/project;
    }

    location /static/admin {

        root /home/myuser/.virtualenvs/projectpk/local/lib/python2.7/site-packages/django/contrib/admin/static/;
    }

    location / {
        proxy_pass_request_headers on;
        proxy_buffering on;
        include proxy_params;
        proxy_pass          http://unix:/home/myuser/projectpk/project/project.sock;
    }


    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/myuser/projectpk/project/templates/;
    }
}
Mosquito answered 17/1, 2016 at 1:58 Comment(2)
you can remove the server_name line, it's not needed in nginx unless you want to serve different content depending on the host name you receive. If you remove that line, nginx will answer any request that arrives to your server at the proper port (80 in this case)Weisshorn
@tato: hmm okay will do, though since myapp.cloudapp.net works correctly, how does the server_name line mess up the forwarding when I set up a CName? Or is that not what you're implying?Mosquito
W
4

Remove the server_name line, it's not needed in nginx unless you want to serve different content depending on the host name you receive.

If you remove that line, nginx will answer any request that arrives to your server at the proper port (80 in this case), coming with myapp.cloudapp.net or mydomain.pk in the Host header.

This assumes that there is no other configuration in /etc/nginx/sites-enabled that would catch the requests.

Weisshorn answered 17/1, 2016 at 2:4 Comment(7)
Ah no, this change gives me a generic Welcome to nginx! page even on myapp.cloudapp.net now.Mosquito
do you have any other config file in the /etc/nginx/sites-enabled dir? It might be getting the requests. Try removing the other oneWeisshorn
The other file I have over there is default, that's probably the culprit. Let me get back to you, hold on.Mosquito
remove it (it's probably a link to sites-available so it's safe to delete) or move it elsewhereWeisshorn
For completeness: the sites-available folder still has the default file in it. Does it matter if I let that remain?Mosquito
sites-available can contain many files. Only those that are linked from sites-enabled will be read and used by nginx service.Weisshorn
but then, how can I configure ssl for that ? Normally a server-name is needed ?Winnipegosis

© 2022 - 2024 — McMap. All rights reserved.