2017-05-27 83 views
1

我想测试nginx的回声模块。我想回显我在浏览器中输入的网址。为什么请求没有后缀的文件扩展名的HTML将下载文件?

我的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/.

回报:返回文件和文件具有内容:/hello/


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

回报:返回文件和文件具有内容:/hello/hi


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

回报:打印/hello/hi.html浏览器


我的问题: 为什么没有HTML后缀的网址将成为下载文件? 如何解决它? 我只是想在浏览器打印网址。

回答

3

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

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

this doucument更多。

1

无论是浏览器将显示页面/下载文件最终取决于其他因素,例如,在HTTP头中

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响应/你好/ 'Content-type'/'Content-Disposition'喜或/hello/hi.html,检查这两个标题中的至少一个标题可能没有正确设置,在这种情况下,它更可能是content-type不是'text/html'0123为您的路径指定内容类型,可能类似于

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

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