First time setting up Nginx and my goal is to have example.com with a static 'index.html' page served with a minimalist config, nothing more. I also want to drop the www subdomain. Here are my sites-available server blocks:
server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
server_name example.com;
root /var/www/example.com/;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
If I use www.example.com or example.com they work fine, with www automatically dropped.
My issue is I can type anything after example.com and the index.html page still loads, like example.com/ABC or example.com/12345. These pages don't exist, why are the URLs accepted? I would expect any URL other than the domain root to return a 404 page instead.
This is probably a very simple issue but I've tried searching here & in the docs and I'm coming up with nothing so far.
It seems the correct behaviour of the try_files clause. From the nginx wiki:
So if you look for ABC or 12345, which can't be found, an internal redirect to index.html is invoked.
Try with:
Look here for a full reference:
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
Based on Martin Fjordvald's comment here is the minimal configuration for the two server blocks, tested and working:
2 things i could think of:
either you have a rewriterule back to the index.html
or it might be possible that you have a custom 404 page which is linked back to the index.html
at least that would be the 2 ideas i would first check.
other possible solution would be checking the logs, setting values to debug and check what "redirects" you back to the index.html sadly i do not know nginx good enough to assist you further.