【问题标题】:How to set index.html as root file in Nginx?如何在 Nginx 中将 index.html 设置为根文件?
【发布时间】:2012-08-10 20:38:28
【问题描述】:

如何为域名例如设置 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;
    }

}

没有任何帮助。

还有其他一些带有 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;
}

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

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

【问题讨论】:

    标签: configuration nginx


    【解决方案1】:

    在您的位置块中,您可以执行以下操作:

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

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

    【讨论】:

    • 如果文件夹不包含 index.html,看起来会导致无限循环
    • 我不确定我是否会认为这是最佳实践或任何东西。如果找不到您要查找的文件或目录,它确实应该返回 404,而不是索引。
    • 它只会返回索引,如果它是一个目录名,否则你会寻找 \index.html 它不会在那里,因此会返回 404
    • 请注意,如果您的位置不是 /(例如 /blog/),则 try_files 将为 $uri $uri/blog/index.html
    • 您可能希望将其设置为 try_files $uri $uri/index.html =404; 以回退到 404 而不是 500。
    【解决方案2】:

    location / { 是最通用的位置(与location {)。它会匹配任何东西,AFAIU。我怀疑拥有location / { index index.html; } 是否有用,因为您网站的每个子目录都有很多重复的内容。

    的方法

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

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

    你的方法

    location = / {
       index index.html;
    

    也很糟糕,因为index 也会进行内部重定向。如果您需要,您应该能够在特定的location 中处理它。创建例如

    location = /index.html {
    

    按照here 的建议。但随后您将拥有一个工作链接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;
    }
    

    附: debug nginx 非常容易(如果你的二进制文件允许的话)。只需添加到server { 块中:

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

    并查看所有内部重定向等。

    【讨论】:

    • 如果您托管 html 以外的其他文件(例如 javaScript 或图像),我认为您希望倒数第二行是:try_files $uri $uri.html =404; 要使 index.html 返回 404,您可以这样做:location = /index { return 404; }location = /index.html { return 404; }
    【解决方案3】:

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

    root   /srv/www/ducklington.org/public_html;
    

    【讨论】:

    • 对我来说 index.html 完美。没关系,只是文件系统上的一个文件名。
    【解决方案4】:

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

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

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

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

    【讨论】:

    • 它在 try_files 之后检查文件 - 如果没有找到任何文件,则内部重定向到最后一个参数中指定的 uri。如果您想查看 404 代码页面以防所有文件未找到,则需要将其指定为最后一个参数。位置 / { try_files $uri $uri/index.html $uri.html =404; }
    【解决方案5】:

    对我来说,(目前投票最多的)答案https://stackoverflow.com/a/11957896/608359 中的try_files 指令导致了重写周期,

    *173 rewrite or internal redirection cycle while internally redirecting
    

    我对 index 指令有更好的运气。请注意,我在名称前使用了正斜杠,这可能是也可能不是您想要的。

    server {
      listen 443 ssl;
      server_name example.com;
    
      root /home/dclo/example;
      index /index.html;
      error_page 404 /index.html;
    
      # ... ssl configuration
    }
    

    在这种情况下,我希望所有路径都指向 /index.html,包括返回 404 时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 2021-11-26
      • 1970-01-01
      相关资源
      最近更新 更多