如何在Nginx中将index.html设置为根文件?

Art*_*man 48 configuration nginx

如何为域名设置index.html,例如https://www.example.com/ - 将用户引导到根目录中的index.html.

我尝试过不同的东西,比如:

server {
    # some configs

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

}
Run Code Online (Sandbox Code Playgroud)

什么都没有帮助我.

有一些其他配置与location关键字,但我也评论过它们.

server {条款中的其他"位置"配置:

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;
}
Run Code Online (Sandbox Code Playgroud)

所有这些都被评论和取消评论,但没有任何帮助.

PS版本是在/etc/nginx/sites-enabled/domainname.com文件中制作的.

cob*_*aco 46

在您的位置栏中,您可以:

location / {
  try_files $uri $uri/index.html;
}
Run Code Online (Sandbox Code Playgroud)

这将告诉ngingx寻找具有首先给出的确切名称的文件,如果没有找到这样的文件,它将尝试uri/index.html.因此,如果有来自https://www.example.com/的请求,它会首先查找完全匹配的文件,然后找不到它会检查index.html

  • 我不确定我是否认为这是最佳做法或任何事情.如果找不到要查找的文件或目录,它确实应该返回404而不是索引. (6认同)
  • 您可能希望将其设置为`try_files $ uri $ uri / index.html = 404;`以回退到404而不是500。 (3认同)
  • 如果文件夹中不包含index.html,它看起来将导致无限循环 (2认同)

Yar*_*nko 34

location / {是最普遍的位置(有location {).它将匹配任何东西,AFAIU.我怀疑location / { index index.html; }因为你网站的每个子目录有很多重复内容而有所帮助.

采用的方法

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

如上面的评论中所提到的那样糟糕,因为它会返回index.html您网站上不应存在的页面(任何可能的内容$uri都会以此结束).另外,如上面的答案所述,在最后一个参数中有一个内部重定向try_files.

你的方法

location = / {
   index index.html;
Run Code Online (Sandbox Code Playgroud)

也很糟糕,因为也index进行了内部重定向.如果你想要,你应该能够在特定的情况下处理它location.创建例如

location = /index.html {
Run Code Online (Sandbox Code Playgroud)

正如这里提出的那样.但是你会有一个工作环节http://example.org/index.html,这可能是不可取的.我使用的另一个变体是:

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;
}
Run Code Online (Sandbox Code Playgroud)

PS 调试 nginx 非常容易(如果您的二进制文件允许).只需添加到server {块中:

error_log /var/log/nginx/debug.log debug;
Run Code Online (Sandbox Code Playgroud)

并看到所有内部重定向等


Art*_*man 17

答案是将根目录放置到位置指令:

root   /srv/www/ducklington.org/public_html;
Run Code Online (Sandbox Code Playgroud)


Nok*_*ors 5

根据文档按指定顺序检查文件的存在,并使用第一个找到的文件进行请求处理;处理是在当前上下文中执行的。文件的路径是根据 root 和 alias 指令从 file 参数构造的。可以通过在名称末尾指定斜杠来检查目录是否存在,例如“$uri/”。如果没有找到任何文件,则内部重定向到最后一个参数中指定的 uri。重要的

内部重定向到最后一个参数中指定的 uri。

因此,如果前两个参数返回 false,则应在最后一个参数中添加您的页面或代码。

location / {
  try_files $uri $uri/index.html index.html;
}
Run Code Online (Sandbox Code Playgroud)