How do I get nginx to serve the "www." version of domain.com?

October 27, 2014 1.8k views

Hello everyone,

I downloaded a LEMP stack onto my server and I have gotten nginx to serve domain.com, but for the life of me I cannot figure out how to get nginx to serve www.domain.com.

Here is what the code looks like currently:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        server_name domain.com www.domain.com;

        location / {
                try_files $uri $uri/ =404;
        }

What change can I make to get www.domain.com to work?

7 comments
  • Seems legit. Did you configure DNS correctly? You're not giving enough information to go on.

  • I am not sure. Here is all that I know:

    The domain name is signed up with Hover and the Name servers are ns1 to ns3.digitalocean.com

    The DNS settings for the domain name are on their default settings.

    On the DigitalOcean side, I have domain.com signed up with ns1 to ns3.digitalocean.com

    There is an "A" record for domain.com. The line is A @ "ip of server".

  • Do you get the correct IP when you try to resolve www.domain.com? If so, did you change your nginx config and forgot to reload it? Do you have something in /etc/hosts pointing to www.domain.com? Furthermore, why do you try to hide the domain name? It doesn't make it easier to troubleshoot for us if you do not even tell us what your domain name really is.

  • Domain name is capayan.com.

  • You did not setup DNS correctly. The A record is missing for www.capayan.com.

  • Edit: extra information that was not needed

  • Thank you for the help cherwin, appreciate it. I added an A record for www.capayan.com.

2 Answers

You did not setup DNS correctly. Fix the A record for www.capayan.com

Have another answer? Share your knowledge.