Configuration 如何在Nginx中将index.html设置为根文件?
如何为域名设置index.html,例如-引导用户在根目录中找到index.html 我尝试过不同的方法,比如:Configuration 如何在Nginx中将index.html设置为根文件?,configuration,nginx,Configuration,Nginx,如何为域名设置index.html,例如-引导用户在根目录中找到index.html 我尝试过不同的方法,比如: server { # some configs location = / { index index.html; fastcgi_index index.html; } or location / { index index.html; fastcgi_
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文件中制作的。答案是将根目录放置到位置指令:
root /srv/www/ducklington.org/public_html;
在位置块中,您可以执行以下操作:
location / {
try_files $uri $uri/index.html;
}
这将告诉ngingx首先查找具有给定确切名称的文件,如果找不到这样的文件,它将尝试uri/index.html。因此,如果请求它,它将首先查找精确的文件匹配,如果没有找到,则根据文档检查index.html 按指定顺序检查文件是否存在,并使用找到的第一个文件进行请求处理;处理在当前上下文中执行。根据root和alias指令从file参数构造文件路径。可以通过在na结尾指定斜杠来检查目录是否存在me,例如“$uri/”。如果未找到任何文件,则会对上一个参数中指定的uri进行内部重定向。重要信息 最后一个参数中指定的uri的内部重定向是 制造 所以,在最后一个参数中,若前两个参数返回false,则应添加页面或代码
location / {
try_files $uri $uri/index.html index.html;
}
location/{
是最常用的位置(带有location{
)。它将匹配任何内容。我怀疑使用location/{index.html;}
是否有用,因为站点的每个子目录都有大量重复内容
与
尝试_文件$uri$uri/index.html index.html
正如上面的评论中所提到的,这是不好的,因为它返回index.html
,用于您的站点上不应该存在的页面(任何可能的$uri
都将在该页面中结束)。
此外,正如上面的回答所述,try\u files
的最后一个参数中有一个内部重定向
你的方法
也不好,因为索引也会进行内部重定向。如果您需要,您应该能够在特定的位置处理该重定向
location = /index.html {
正如建议的那样。但是这样你就有了一个工作链接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;
}
查看所有内部重定向等。对于我来说,(目前投票最多的)答案中的try\u 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 x.html,包括当返回一个404。./p>看起来它会导致无限循环,如果文件夹不包含索引。HTMLI不确定我会认为这是一个最佳实践或什么。如果你找不到你要找的文件或目录,它真的应该返回一个404,而不是索引。如果它是目录名,则只返回索引,否则您将查找\index.html,但它不在那里,因此将返回404注意,如果您的位置不是/(例如/blog/),try_文件将是$uri$uri/blog/index.html
。您可能希望将其设置为try_文件$uri$uri/index.html=404;
,以回退到404而不是500。对我来说index.html
工作得很好。这根本不重要,它只是文件系统上的一个文件名。它会在try_文件之后检查文件-如果没有找到文件后,将对最后一个参数中指定的uri进行内部重定向。如果要查看404代码页以防未找到所有文件,则需要将其指定为最后一个参数。位置/{try_files$uri$uri/index.html$uri.html=404;}如果您托管的是html以外的其他文件(如javaScript或图像)我想您希望倒数第二行是:try_files$uri$uri.html=404;
要使index.html返回404,您可以这样做:location=/index{return 404;}
location=/index.html{return 404;}
*173 rewrite or internal redirection cycle while internally redirecting
server {
listen 443 ssl;
server_name example.com;
root /home/dclo/example;
index /index.html;
error_page 404 /index.html;
# ... ssl configuration
}