Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

configuration - How to set index.html as root file in Nginx?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...