66

How to set index.html for the domain name e.g. https://www.example.com/ - leads user to index.html in root directory.

I've tried different things like:

server {
    # some configs

    location = / {
            index index.html;
            fastcgi_index index.html;
    }
or 
    location / {
            index index.html;
            fastcgi_index index.html;
    }

}

Nothing helped me.

There are some other configs with location keyword, though I'd commented them either.

Other "location" configs in the server { clause:

location ~ .*(css|htc|js|bmp|jp?g|gif|ico|cur|png|swf|htm?|html)$ {
        access_log off;
        root $www_root;
}

location ~ \.php$
{
        include                         /etc/nginx/fastcgi_params;
        index                           index.html;
        fastcgi_index                   index.html;
        fastcgi_param SCRIPT_FILENAME   $www_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING      $query_string;
        fastcgi_param PATH_INFO         $fastcgi_path_info;
        fastcgi_pass                    127.0.0.1:9000;
        # Директива определяет что ответы FastCGI-сервера с кодом больше или равные 400
        # перенаправлять на обработку nginx'у с помощью директивы error_page
        fastcgi_intercept_errors        on;
        break;
}

location ~ /\.ht {
    deny all;
}

All them were commented and uncommented, but nothing helped.

PS Editions were made in /etc/nginx/sites-enabled/domainname.com file.

6 Answers 6

66

in your location block you can do:

location / {
  try_files $uri $uri/index.html;
}

which will tell ngingx to look for a file with the exact name given first, and if none such file is found it will try uri/index.html. So if a request for https://www.example.com/ comes it it would look for an exact file match first, and not finding that would then check for index.html

9
  • 3
    Look like it results in infinite loop if the folder doesn't contains index.html Apr 17, 2014 at 7:36
  • 11
    I'm not sure I'd consider this to be a best practice or anything. If you can't find the file or directory you are looking for, it really should return a 404, not the index.
    – Joel B
    Sep 16, 2014 at 16:17
  • it will only return the index if it's a directory-name otherwise you'd be looking for <filename>\index.html which won't be there and will thus return 404
    – cobaco
    Sep 18, 2014 at 9:38
  • Note that, if your location is something else than / (e.g. /blog/), the try_files would be $uri $uri/blog/index.html. Jan 12, 2015 at 17:19
  • 8
    You probably want to set it to try_files $uri $uri/index.html =404; to fallback to 404 instead of 500. Jan 22, 2017 at 11:01
48

location / { is the most general location (with location {). It will match anything, AFAIU. I doubt that it would be useful to have location / { index index.html; } because of a lot of duplicate content for every subdirectory of your site.

The approach with

try_files $uri $uri/index.html index.html;

is bad, as mentioned in a comment above, because it returns index.html for pages which should not exist on your site (any possible $uri will end up in that). Also, as mentioned in an answer above, there is an internal redirect in the last argument of try_files.

Your approach

location = / {
   index index.html;

is also bad, since index makes an internal redirect too. In case you want that, you should be able to handle that in a specific location. Create e.g.

location = /index.html {

as was proposed here. But then you will have a working link http://example.org/index.html, which may be not desired. Another variant, which I use, is:

root /www/my-root;

# http://example.org
# = means exact location
location = / {
    try_files /index.html =404;
}

# disable http://example.org/index as a duplicate content
location = /index      { return 404; }

# This is a general location. 
# (e.g. http://example.org/contacts <- contacts.html)
location / {
    # use fastcgi or whatever you need here
    # return 404 if doesn't exist
    try_files $uri.html =404;
}

P.S. It's extremely easy to debug nginx (if your binary allows that). Just add into the server { block:

error_log /var/log/nginx/debug.log debug;

and see there all internal redirects etc.

1
  • 1
    If you are hosting other files than html (such as javaScript or images) I think you want the 2nd to last line to be: try_files $uri $uri.html =404; To make index.html return 404, you can then do: location = /index { return 404; } location = /index.html { return 404; } Feb 26, 2019 at 8:46
24

The answer is to place the root dir to the location directives:

root   /srv/www/ducklington.org/public_html;
1
  • for me index.html works perfectly. It doesn't matter at all, it's just a name of a file on the filesystem. Oct 30, 2015 at 15:17
7

According to the documentation Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made. Important

an internal redirect to the uri specified in the last parameter is made.

So in last parameter you should add your page or code if first two parameters returns false.

location / {
  try_files $uri $uri/index.html index.html;
}
1
  • It's checks files after try_files - If none of the files were found, an internal redirect to the uri specified in the last parameter is made. If you want to see 404 Code page in case if all files not found you need specified it as last argument. location / { try_files $uri $uri/index.html $uri.html =404; }
    – Nokors
    Jan 8, 2016 at 9:31
4

For me, the try_files directive in the (currently most voted) answer https://stackoverflow.com/a/11957896/608359 led to rewrite cycles,

*173 rewrite or internal redirection cycle while internally redirecting

I had better luck with the index directive. Note that I used a forward slash before the name, which might or might not be what you want.

server {
  listen 443 ssl;
  server_name example.com;

  root /home/dclo/example;
  index /index.html;
  error_page 404 /index.html;

  # ... ssl configuration
}

In this case, I wanted all paths to lead to /index.html, including when returning a 404.

4

Add this to the location block in nginx works for me

try_files $uri $uri/ /index.html =404;

thats the entire block

location / {
  expires -1;
  add_header Pragma "no-cache";
  add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
  root /var/www
  try_files $uri $uri/ /index.html =404;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.