Vue項目打包并部署nginx服務器的詳細步驟
使用場景:
我們常使用前后端分離項目時,會需要將前端vue打包然后部署。
一.打包
vue項目其實可以直接通過一下語句進行打包:
npm run build
默認打包情況如下:


當我們需要將打包名稱以及靜態(tài)資源位置進行修改時便需要進行相應的配置:
1.首先在項目根目錄下創(chuàng)建vue.config.js文件

配置內(nèi)容如下所示(附帶跨域問題解決):
module.exports = {
//打包
publicPath: './',
outputDir: 'test', //打包輸出目錄
assetsDir: './static', //放置生成的靜態(tài)資源
filenameHashing: true, // 生成的靜態(tài)資源在它們的文件名中包含了 hash 以便更好的控制緩存
lintOnSave: false, //設置是否在開發(fā)環(huán)境下每次保存代碼時都啟用 eslint驗證
productionSourceMap: false,// 打包時不生成.map文件
// 解決跨域配置
devServer: { //記住,別寫錯了devServer//設置本地默認端口 選填
port: 8080,
proxy: { //設置代理,必須填
'/api': { //設置攔截器 攔截器格式 斜杠+攔截器名字,名字可以自己定
target: 'http://localhost:9090', //代理的目標地址(后端設置的端口號)
changeOrigin: true, //是否設置同源,輸入是的
pathRewrite: { //路徑重寫
'/api': '' //選擇忽略攔截器里面的單詞
}
/*也就是在前端使用/api可以直接替換為(http://localhost:9090)*/
}
}
},
}2.查看路由中(router/index.js)是否使用history,是的話修改為hash?;蛘邔ode直接注掉,因為默認使用hash。
const router = new VueRouter({
/*mode: 'history',*/
mode: 'hash',
routes:[]
})
export default router然后再次使用npm run build進行打包就會出現(xiàn)test文件夾,已經(jīng)其中靜態(tài)文件會放置到static中。
到此打包已經(jīng)結(jié)束。
3.找到打包后文件的路徑
雙擊打包好的index.html文件,就可以看到是首頁了。
二.部署(nginx)
首先需要安裝nignx,這個毋庸置疑這里就不介紹。(或者后續(xù)會在nginx板塊放置具體安裝步驟)
直接在nginx.conf中進行配置即可:
server {
listen 8021;
server_name localhost;
location /test{
alias /home/hyq/vue_file;
index index.shtml index.html index.htm;
}配置具體含義:
#user nobody;
worker_processes 1;
?
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
?
#pid logs/nginx.pid;
?
?
events {
worker_connections 1024;
}
?
http {
include mime.types;
default_type application/octet-stream;
?
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
?
#access_log logs/access.log main;
?
sendfile on;
#tcp_nopush on;
?
#keepalive_timeout 0;
keepalive_timeout 65;
?
#gzip on;
ssi on;
ssi_silent_errors on;
ssi_types text/shtml;
#定義的服務器列表
upstream cms {
#這里代表代理的項目端口為127.0.0.1:8111端口(weight等配置自行查詢)
server 127.0.0.1:8111 weight=5 max_fails=3 fail_timeout=20s;
}
server {
listen 8096; #nginx使用8096
server_name localhost; #服務名稱
?
location /menhu/cms {
proxy_pass http://cms;
#請求轉(zhuǎn)向cms 定義的服務器列表。也就是訪問localhost:8096/menhu/cms 會轉(zhuǎn)向到上方服務器列 #表中的127.0.0.1:8111
}
?
location /qgxzzfzhgljdpt {
root D:\BDWorkParce3\LPT_MENHU\wwwroot_release; #根目錄
index index.shtml index.html index.htm; #設置默認頁
#訪問localhost:8096/qgxzzfzhgljdpt 會打開 D:\BDWorkParce3\LPT_MENHU\wwwroot_release\qgxzzfzhgljdpt下級中的index.shtml/index.html/index.htm默認頁
}
location ^~ /template {
return 404;
}
location = /c/ {
return 404;
}
location = /css/ {
return 404;
}
location = /images/ {
return 404;
}
location = /include/ {
return 404;
}
location = /js/ {
return 404;
}
location = /style/ {
return 404;
}
location = /upload/ {
return 404;
}
location = /html/ {
return 404;
}
location = /root/ {
return 404;
}
location ~ .*.(svn|Git|git) {
return 404;
}
?
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
?
}
########### 每個指令必須有分號結(jié)束。#################
#user administrator administrators; #配置用戶或者組,默認為nobody nobody。
#worker_processes 2; #允許生成的進程數(shù),默認為1
#pid /nginx/pid/nginx.pid; #指定nginx進程運行文件存放地址
error_log log/error.log debug; #制定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #設置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認為on
multi_accept on; #設置一個進程是否同時接受多個網(wǎng)絡連接,默認為off
#use epoll; #事件驅(qū)動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連接數(shù),默認為512
}
http {
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型,默認為text/plain
#access_log off; #取消服務日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
access_log log/access.log myFormat; #combined為日志格式的默認值
sendfile on; #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。
sendfile_max_chunk 100k; #每個進程每次調(diào)用傳輸數(shù)量不能大于設定的值,默認為0,即不設上限。
keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊。
?
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #熱備
}
error_page 404 https://www.baidu.com; #錯誤頁
server {
keepalive_requests 120; #單連接請求上限次數(shù)。
listen 4545; #監(jiān)聽端口
server_name 127.0.0.1; #監(jiān)聽地址
location ~*^.+$ { #請求的url過濾,正則匹配,~為區(qū)分大小寫,~*為不區(qū)分大小寫。
#root path; #根目錄
#index vv.txt; #設置默認頁
proxy_pass http://mysvr; #請求轉(zhuǎn)向mysvr 定義的服務器列表
deny 127.0.0.1; #拒絕的ip
allow 172.18.5.54; #允許的ip
}
}
}然后啟動或者重啟nginx即可。
訪問:服務器地址:8021/test 即可。
總結(jié)
到此這篇關于Vue項目打包并部署nginx服務器的文章就介紹到這了,更多相關Vue項目打包部署nginx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue+Element實現(xiàn)網(wǎng)頁版?zhèn)€人簡歷系統(tǒng)(推薦)
這篇文章主要介紹了Vue+Element實現(xiàn)網(wǎng)頁版?zhèn)€人簡歷系統(tǒng),需要的朋友可以參考下2019-12-12
vue實現(xiàn)樣式之間的切換及vue動態(tài)樣式的實現(xiàn)方法
這篇文章主要介紹了vue中如何實現(xiàn)樣式之間的切換及vue動態(tài)樣式的實現(xiàn)方法,本文給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下2017-12-12
Vue-Access-Control 前端用戶權限控制解決方案
Vue-Access-Control是一套基于Vue/Vue-Router/axios 實現(xiàn)的前端用戶權限控制解決方案。這篇文章主要介紹了Vue-Access-Control:前端用戶權限控制解決方案,需要的朋友可以參考下2017-12-12
elementUI樣式修改未生效問題詳解(掛載到了body標簽上)
vue+elementUI項目開發(fā)中,經(jīng)常遇到修改elementUI組件樣式無效的問題,這篇文章主要給大家介紹了關于elementUI樣式修改未生效問題的相關資料,掛載到了body標簽上,需要的朋友可以參考下2023-04-04

