2

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.

2 Answers 2

4

It seems the correct behaviour of the try_files clause. From the nginx wiki:

Checks for the existence of files in order, and returns the first file that is found. A trailing slash indicates a directory - $uri /. In the event that no file is found, an internal redirect to the last parameter is invoked. Do note that only the last parameter causes an internal redirect, former ones just sets the internal URI pointer.

So if you look for ABC or 12345, which can't be found, an internal redirect to index.html is invoked.

Try with:

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

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:

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; } 
6
  • I don't see a redirect to example.com if I type example.com/12345, the index.html page is returned for everything. example.com/12345 is accepted and index.html is displayed at that URL. I am expecting either a 404 or for the /12345 to be stripped off the end? Commented Dec 20, 2013 at 9:14
  • It's an internal redirect, I won't see a 3xx response to the client. Try deleting the /index.html at the end of the try_files clause Commented Dec 20, 2013 at 9:30
  • Just tried it and now example.com/123 returns a '500 Internal Server Error'. Error.log shows [error] 11072#0: *898 rewrite or internal redirection cycle while internally redirecting to "/123". Shouldn't this be a 404 instead? This is frustrating and I suspect it's something really basic. Thanks for the advice though. Commented Dec 20, 2013 at 9:48
  • 1
    I've edited my answer whit an example of configuration Commented Dec 20, 2013 at 9:56
  • 2
    Just for the record, try_files $uri $uri/ =404; is the same as just removing try_files. Right now you're explicitly instructing nginx to do what it's default behaviour is. No one says you have to use try_files. Commented Dec 20, 2013 at 12:55
1

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.

1
  • Thanks for the quick reply, but that's my complete config up there. There aren't any rewrite rules or custom 404 pages. I took the existing default Nginx sites-enabled and deleted all comment lines and defaults and now I've made a mess of it I guess. Commented Dec 20, 2013 at 8:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.