如何在nginx中提供html文件,而不在此别名设置中显示扩展名

ask*_*ike 20 html alias nginx

我在nginx中设置这个别名以正确显示我的网站时遇到了很多麻烦.

我关注的网站应该可以访问,mywebsite.com/mr并且与位于的网站不同mywebsite.com/.该网站位于/fullpath(为简单起见缩短)该网站需要提供三种内容:

  1. 索引文件位于/fullpath/index.html.
  2. 其他html文件(不在.html浏览器中显示扩展名).
  3. 静态资产(js/css/img)位于/fullpath和子目录中.

我已经尝试改变比赛的顺序,try_files并找到了他们都工作的情况,但不是在同一时间:

location /mr {
  default_type "text/html";
  alias /fullpath;

  # with this one 1 and 3 work
  # try_files $uri/index.html $uri.html $uri; 

  # with this one 2 and 3 work
  # try_files $uri $uri.html $uri/index.html;

  # with this one 1 and 2 work
  try_files $uri.html $uri/index.html $uri;
}
Run Code Online (Sandbox Code Playgroud)

当一个人不工作时404.有谁知道我怎么能正确地提供各种文件?

Dan*_*ack 29

显然别名和try_files 不能一起工作.但是,我认为你不需要使用别名.

location /mr {
  default_type "text/html";
  try_files  /fullpath/$uri /fullpath/$uri.html /fullpath/$uri/index.html  /fullpath/index.html;
}
Run Code Online (Sandbox Code Playgroud)

哪个会尝试:

  • 确切的文件.
  • 添加了.html的文件.
  • 路径中的索引.
  • 默认索引.

我认为root指令确实适用于try文件但无法测试.

server{
    location /mr {

        root /home/mysite/fullpath;

        default_type "text/html";
        try_files  $uri $uri.html $uri/index.html index.html;
    }
}
Run Code Online (Sandbox Code Playgroud)


Tro*_*eld 7

我使用了@Danack发布的组合,这使我得到了我正在寻找的结果(直接提供html文件):

location /health-check {
    default_type "text/html";
    alias /path/to/my/file.html;
}
Run Code Online (Sandbox Code Playgroud)