【问题标题】:Why request without suffix of filename extension html will download file?为什么不带文件扩展名 html 后缀的请求会下载文件?
【发布时间】:2017-05-27 13:43:05
【问题描述】:

我想测试 nginx 的 echo 模块。我想回显我在 浏览器中输入的 url。

我的nginx配置:

   index  index.php index.html index.htm index.nginx-debian.html ;

   location / {               
       try_files $uri $uri/  /index.php  =404;              
   }
   location  /hello {
   echo $request_uri;
   }

输入网址:http://127.0.0.1/hello/.

return : 返回一个文件 并且文件有内容 : /hello/


输入网址:http://127.0.0.1/hello/hi

return : 返回一个文件 并且文件有内容 : /hello/hi


输入网址:http://127.0.0.1/hello/hi.html

返回:打印 /hello/hi.html浏览器中。


我的问题: 为什么不带html后缀的url会变成下载文件? 如何解决? 我只想在浏览器中打印网址。

【问题讨论】:

    标签: html nginx configuration


    【解决方案1】:

    nginx 从扩展中确定Content-Type。这些包含在一个名为mime-types 的文件中。您可以通过在 location 块中放置 default-type 指令来覆盖此行为。例如:

    location  /hello {
        types {}
        default_type    text/html;
        echo $request_uri;
    }
    

    请参阅this doucument 了解更多信息。

    【讨论】:

      【解决方案2】:

      浏览器最终是否渲染页面/下载文件取决于其他因素,例如http header中的'Content-type' /'Content-Disposition'

      Content-Disposition takes one of two values, `inline' and
      `attachment'.  `Inline' indicates that the entity should be
      immediately displayed to the user, whereas `attachment' means that
      the user should take additional action to view the entity.
      

      您可以在访问时检查和比较 http 响应 /hello/hi 或 /hello/hi.html,检查这两个标头中的至少一个可能未正确设置,在这种情况下,更可能 content-type 不是 'text/html' 这里

      一种解决方案是为您的路径指定内容类型,可能类似于

      location  /hello {
        default_type "text/html";
        echo $request_uri;
      }
      

      location  /hello {
        add_header  Content-Type 'text/javascript;charset=utf-8';
        echo $request_uri;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-10
        • 1970-01-01
        • 2019-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        相关资源
        最近更新 更多