搜尋

首頁  >  問答  >  主體

linux - nginx設定content-type問題

vue專案打包後產生static目錄和一個index.html
然後我把這兩個檔案發到線上

location / {
  try_files $uri $uri/ /index.html;
}

透過上面的程式碼,讓vue中定義的路由全指到index.html
這樣網站可以顯示,但是無法取得後台json資料
原因如下:
nginx中mime_types檔案中設定

type {
    text/html html
}
// 即所有后缀为html的文件的content-type都为text/html

因為所有路由都指向index.html,所以沒有後綴的例如/api/articles的路由都指向index.html,所以回應頭content-type都為text/html,但是我想取得的是application/json

求解,謝謝!

高洛峰高洛峰2819 天前736

全部回覆(2)我來回復

  • 巴扎黑

    巴扎黑2017-05-16 13:29:27

    可以為提供josn的API設定一個location啊,看到你在知乎上問我,
    比如說 location api/

    回覆
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:29:27

    location /api {
        try_files $uri $uri/ /index.json;
    }

    不過,為什麼要弄這麼奇葩的配置…

    回覆
    0
  • 取消回覆