2017-05-27 25 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; 
} 
相關問題