Question

Cannot make www. version of my domain work with Nginx

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


}



Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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