404 Nginx Ubuntu Error When Not Using WWW
Hi,
I'm getting this strange issue when navigating to my site. When I navigate to my site by typing in sitename.com (without www), I get this 404 Nginx Ubuntu Error. If I navigate to the site using www.sitename.com (with www), I'm able to see the site and everything works fine. Lastly, if I then navigate to the site using sitename.com (without www), the site works fine.
This 404 Nginx Ubuntu Error only shows up if I'm going to the site for the first time on a new device using a new browser.
I've configured the A/AAAA record for www and without www so both should work fine.
I'm not sure where the issue is, please help.
Thanks!
2 Replies
A 404 is NGINX's default way of saying that a file or directory doesn't exist. If you're seeing a 404, it means that NGINX can't find the info it needs to serve when queried. The good news is that since you're seeing a 404, your A/AAAA records are pointing to the right IP; it's just that when it gets there, it can't find what it's looking for.
You'll want to double check your sites-available configuration (the file should be located in /etc/nginx/sites-available/example.com
) and make sure your symlinks between sites-available and sites-enabled (/etc/nginx/sites-enabled/
) are set up properly. Just in case, you'll also want to double check your /etc/hosts
file configuration to make sure your IP is pointing to the correct FQDN.
We have instructions for setting up your /etc/hosts/
file in our Getting Started guide here, but here's a quick default config:
127.0.0.1 localhost.localdomain localhost
203.0.113.10 example-hostname.example.com example-hostname
2600:3c01::a123:b456:c789:d012 example-hostname.example.com example-hostname
Likewise, here's a good example of a healthy sites-available config from our NGINX guide. Keep in mind, this is a basic setup with NGINX only being configured for HTTP connections on port 80:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
For what it's worth, I set up a basic site on a Linode solely running NGINX with A/AAAA records for my site with and without "www", and it worked like a charm using the Getting Started and NGINX guides I linked above.
I also tracked down a nice post from NGINX's official site with tons of configuration troubleshooting tips: NGINX: Pitfalls and Common Mistakes