本篇文章為大家展示了怎么將vue項目部署到nginx服務器中,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
1.router(history)模式vue項目部署到nginx
1)修改router模式為history(默認為hash)
const router = new VueRouter({ routes, mode: 'history' });
對路由模式不清楚的小伙伴,可以看這篇vue-router路由模式詳解
2)修改config/index.js,build下靜態資源路徑,完成后執行npm run build打包
3)修改nginx配置
server { listen 80;//代理端口 server_name 192.168.0.152;//代理名稱(域名、ip) #charset koi8-r; #access_log logs/host.access.log main; location / { root test; //項目存放的地址(當前服務器位置) index /index.html; try_files $uri $uri/ @router; //一個覆蓋所有情況的候選資源:如果 URL 匹配不到任何靜態資源,返回同一個 index.html 頁面 } location @router { rewrite ^.*$ /index.html last; } }
運行結果:
2.vue項目部署到tomcat
1)項目上線,一般需要添加項目名,并且消去vue-router產生的#號,需要在router配置
const router = new VueRouter({ routes, mode: 'history', base: '/test/'//項目名稱 訪問路由頁面都需要加上這個,訪問的根路徑為http://ip:port/test });
2)修改config/index.js,build下靜態資源路徑與base的取值一致
3)tomcat的配置
在tomcat的webapps新建文件夾,文件夾名稱和上面配置的根路徑一致,即為test,然后將打包生成的dist文件夾里面的文件復制到test下,并且新建文件WEB-INF/web.xml。
項目結構為:
WEB-INF目錄下新增web.xml內容為:
//覆蓋所有情況的候選資源:如果 URL 匹配不到任何靜態資源,返回同一個 index.html頁面 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <display-name>Router for Tomcat</display-name> <error-page> <error-code>404</error-code> <location>/index.html</location> </error-page> </web-app>
詳細了解可看vue官方文檔后端配置HTML5 History 模式
4)重新啟動tomcat
上述內容就是怎么將vue項目部署到nginx服務器中,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。