Question

Cannot make www. version of my domain work with Nginx

Posted November 30, 2020 277 views
NginxDNSConfiguration Management

I have purchased a custom domain with GoDaddy and have successfully gotten it to direct to my droplet. However, this only works when the domain is typed in as example.com. www.example.com just results in a 404 Not Found error. I have been trying to fix this for a while and implemented guidance from a number of tutorials and the Digital Ocean support team, but nothing works. I have created an A record for the www. version of my domain and put it in my Nginx configuration file as an alias. After being advised to follow this tutorial, I entered a new server block into my configuration file as indicated in the tutorial:

server {
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

When I was told by the support team that it should be

server {
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

I tried that - neither of those worked. It has also become quite confusing to follow any instructions because my Nginx configuration file is set up as a reverse proxy (so that I can have my domain lead directly to my shiny server) and I have an SSL certificate for the example.com version of my domain (I tried setting it up for both this and the alias but that was not possible because the alias does not link to my server).

I have further been advised to check my Nginx error log after trying to access the www.example.com version of my domain, but there is nothing about it in the error log.

I am out of ideas and would really appreciate any pointers from someone more experienced than myself. This is the code of my configuration file:

server {

server_name example.com www.example.com;

location / {

proxy_pass http://127.0.0.1:3838;

}


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}



server {
        server_name example.com;
        return 301 $scheme://www.example.com$request_uri;
}





server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



listen 80;

server_name example.com www.example.com;
    return 404; # managed by Certbot


}


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @pk94,

First things first, make sure your A record for the root domain and the www one are the same. I know it’s a basic suggestion but trust me, it happens a lot more than you think.

Next step, I can see you are using a reverse proxy

proxy_pass http://127.0.0.1:3838;

You mentioned that it’s redirecting to your shiny new server. I take it, you are talking about your DigitalOcean droplet as this is actually forwarding traffic to port 3838 on your droplet. Is that how it’s supposed to be? What is on this port, maybe the issue lies there?

One other thing, I would suggest tidying up a bit your Nginx configuration as at the moment it’s all over to place so to say.

Regards,
KFSys

  • Hi KFSys,

    Many thanks for your answer. I should clarify that this is my first time managing a server/hosting a website and I pretty much have no idea what I’m doing - so with that in mind…

    What does it mean for the A record for the root and www domain to be the same? Right now I have two A records and they are as follows:
    A www.example.com directs to IPaddress 3600
    A example.com directs to IPaddress 3600
    (IPaddress being my droplet’s IP address)

    In terms of the reverse proxy: I am using the droplet to host a single shiny app. I want the shiny app to be the first page that appears when users enter my domain. Initially, they had to go through the Nginx landing page where I put a link to the shiny-server subdomain, but I then set up the reverse proxy to ensure that the landing page is my shiny-app’s landing page. The www. version of my domain has never worked, not even when I still used the Nginx landing page (at that time I also did not have the redirect block in the configuration file and I had not made the second A record, but I had put example.com www.example.com as the server name).

    I agree the configuration file is all over the place and it makes it harder to follow any instructions, but since I have never used Nginx before and am completely dependent on tutorials/instructions I would struggle to tidy it up and not break it…

    • Hi @pk94,

      Don’t worry about that.

      You need to update your DNS records.

      Create an “A” record for the example.com that points to the droplet IP address.
      Create a “CNAME” record that points to the root domain example.com.

      And for the Nginx configuration, I suggest you should use this tool - https://www.digitalocean.com/community/tools/nginx

      • Dear @akashkanaujiya,

        Many thanks for your response. I have changed the DNS records as you suggested and the page which appears at www.example.com has now changed to a colourful page with my domain example.com at the top and some links labeled “MANAGE CUSTOMERS SOFTWARE” and the like, but it’s still not the page I want. Thank you for sharing the tool, but unfortunately I don’t know what I need to enter there to get the configuration I want :( Just looking at the configuration file I shared in my post, is there anything which you think could be the reason this is not working?

        Thanks,
        Pia