nginx部署vue只能訪問預設頁面的問題
在通過nginx啟動vue以後我們在訪問頁面的時候只能訪問預設頁面和通過專案內跳轉其他頁面,如果重新整理就會404
通過預設頁面內部訪問:
直接重新整理:
可以看到nginx並不識別vue的其他頁面,這跟conf檔案的配置有關
location / {
root /home/nx/dist;
index index.html index.htm index.jsp;
}
這是我們基礎的配置,按照字義解讀就是隻訪問了/dist檔案下的 index.html、index.htm、index.jsp頁面,而其他頁面在訪問的時候被nginx當作自身的服務訪問而找不到,我們修改一下配置檔案
location / {
root /home/nx/dist;
#index index.html index.htm index.jsp;
#proxy_pass http://admin;
#proxy_redirect default;
#proxy_connect_timeout 10;
try_files $uri $uri/ /index.html;
expires 7d;
}
將預設的index註釋掉,換成了try_files,它會去掃描內部目錄然後再進行內部重定向。
nginx的try_files:https://www.cnblogs.com/boundless-sky/p/9459775.html
expires 是nginx控制快取的一種方式,7d=7天
nginx配置expires:https://blog.csdn.net/hjh15827475896/article/details/53434298