我正在学习有关Nginx的知识,我已经碰壁了。我正在尝试运行一个小型的静态html站点,该站点具有如下所示的非常简单的树结构:
/var/www/mydomain.com/pages/category1/0.html
/var/www/mydomain.com/pages/category1/1.html
/var/www/mydomain.com/pages/category1/2.html
/var/www/mydomain.com/pages/category1/3.html
/var/www/mydomain.com/pages/category1/4.html
/var/www/mydomain.com/pages/category1/5.html
/var/www/mydomain.com/pages/category1/6.html
/var/www/mydomain.com/pages/category1/7.html
/var/www/mydomain.com/pages/category1/8.html
/var/www/mydomain.com/pages/category1/9.html
/var/www/mydomain.com/pages/category2/0.html
/var/www/mydomain.com/pages/category2/1.html
/var/www/mydomain.com/pages/category2/2.html
/var/www/mydomain.com/pages/category2/3.html
/var/www/mydomain.com/pages/category2/4.html
/var/www/mydomain.com/pages/category2/5.html
/var/www/mydomain.com/pages/category2/6.html
/var/www/mydomain.com/pages/category2/7.html
/var/www/mydomain.com/pages/category2/8.html
/var/www/mydomain.com/pages/category2/9.html
/var/www/mydomain.com/pages/category3/0.html
/var/www/mydomain.com/pages/category3/1.html
/var/www/mydomain.com/pages/category3/2.html
/var/www/mydomain.com/pages/category3/3.html
/var/www/mydomain.com/pages/category3/4.html
/var/www/mydomain.com/pages/category3/5.html
/var/www/mydomain.com/pages/category3/6.html
/var/www/mydomain.com/pages/category3/7.html
/var/www/mydomain.com/pages/category3/8.html
/var/www/mydomain.com/pages/category3/9.html
如果我通过执行www.mydomain.com/pages/category1/1.html
或www.mydomain.com/pages/category2/2.html
等操作进入网址,那么一切都将正常进行,到目前为止一切正常(所有链接都很好)。
每个html页面都使用看起来与此类似的相对hrefs:
href="../category1/0.html"
href="../category2/0.html"
href="../category3/0.html"
href="0.html"
href="1.html"
href="2.html"
href="3.html"
href="4.html"
href="5.html"
href="6.html"
href="7.html"
href="8.html"
href="9.html"
因此,当您的请求类似于www.mydomain.com/pages/category1/1.html
时,一切正常。
现在,这是我的问题,当某人访问www.mydomain.com
时,我希望该站点也能正常工作,因此我想到的第一个想法就是像这样修改服务器index
指令:
server {
server_name mydomain.com;
root /var/www/mydomain.com;
index pages/category1/0.html;
# index.html fallback
location / {
try_files $uri $uri/ index.html;
}
}
但是,如果我这样做,那么所有链接都将变得无效...我的意思是,它将对www.mydomain.com
响应有效内容,但从那里开始,所有相对URL都将无效。
我确定这是一个非常基本的情况,所以请您提出建议,通过修改我上面的nginx配置可以解决这个问题?
Ps。我知道可以使用适当的后端技术而不是平面静态html来更轻松地解决此问题,但是我想暂时将其保持尽可能简单,因此,如果可以解决此问题,请调整nginx的配置就可以了。
当前为了使其正常工作,您必须将配置更改为:
server {
server_name mydomain.com;
root /var/www/mydomain.com/pages/category1; #this must be a path
index 0.html;
# index.html fallback
location / {
try_files $uri $uri/ =404;
}
}