應用為VUE單頁應用,路由模式為history,web服務器為nginx,正常情況下如果直接通過url訪問一個中間頁(不是index.html)時,會看到nginx返回的404錯誤,這個問題目前我只能通過修改nginx站點配置文件來實現。
具體代碼(只看紅色加粗的部分就行):
server { listen 80; server_name ……; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/……;# BEGIN 解決VUE在中間頁刷新時出現404或500的問題 location / { try_files $uri $uri/ @router; #檢測文件存在性重定向到首頁目錄 防止404 index index.html; } location @router { rewrite ^.*$ /index.html break; } # END 解決VUE在中間頁刷新時出現404或500的問題
#SSL-START SSL相關配置,請勿刪除或修改下一行帶注釋的404規則 #error_page 404/404.html; #SSL-END #ERROR-PAGE-START 錯誤頁配置,可以注釋、刪除或修改 #error_page 404 /404.html; #error_page 502 /502.html; #ERROR-PAGE-END #PHP-INFO-START PHP引用配置,可以注釋或修改 include enable-php-00.conf; #PHP-INFO-END #REWRITE-START URL重寫規則引用,修改后將導致面板設置的偽靜態規則失效 include /www/server/panel/vhost/rewrite/scope.athena.tentest.h024.cn.conf; #REWRITE-END #禁止訪問的文件或目錄 location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } #something else……… }