最近公司把雲平台產品用vue 前后端分離的框架來寫,前面大部分開發都比較順利,后面打包部署出了bug
現在記錄下自己遇到的哪些坑
1,我直接npm run build 打包出來,打開dist目錄下面的index.html 空白頁,頁面上出現
no "NODE ENV"是我打包時沒有指定打包到哪個環境
正確打包是 npm run build:prod
2.關於vue-router 中mode:'history'的設置,導致我頁面打包后也是路由無法跳轉
而且出現的bug是語法錯誤,unexpected toke <
無奈我去掉了history ,重新打包,就可以了
3.關於Nginx的配置nginx.conf
這里先復習下nginx命令,安裝nginx點擊這里可以查看
start nginx 啟動並服務
tasklist /fi "imagename eqnginx.exe" 查看是否啟動
nginx -s reload 改變配置文件時,需重啟nginx工作進程
關閉進程
nginx -s stop 關閉服務
nginx -s quit 安全關閉
taskkill /F/IM nginx.exe 關閉所有nginx服務
下面整理下正確打包,部署到nginx的方法
新手小白建議在mode:hash 下先運行
第一步:打包(mode:hash)
1.打包后第一個問題是打開dist中的index.html沒有顯示頁面,空白頁面。
一、打開config的文件夾中的index.js
如圖把assetsPUblicPath修改為‘./’
然后再次打包發現路由的內容不法渲染並且無任何錯誤,
二、把路由中的model注釋掉
三、再次打開dist中的index.html,可以看到頁面,路由也是正確的則表示可以把dis目錄拿到nginx部署了
第一步:打包(mode:history)
一、打包是直接寫/根路徑,而不是相對路徑
二、路由path前面全部要加上/,history模擬的是跳轉地址路徑
三、nginx加上下面的配置,其他和hash方式全部一樣
第二步:如何部署nginx
1.把dis目錄復制到nginx下的html文件夾下面
2.打開nginx.conf配置文件
代碼如下:
#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; server { listen 8082; #自定義你服務器的端口號 server_name localhost; #自定義你的IP或者域名,localhost:127.0.0.1 本地的IP #charset koi8-r; #access_log logs/host.access.log main; root E:\work\aiotcloud\yihao01-web-management\dist; #dist目錄指向你本地的工作目錄,相當重要 index index.html index.htm; #打開默認文件為index.html
#官網介紹設置這條可以解決history路由的問題 location / { try_files $uri $uri/ /index.html; }
#####################################
# 下面這些地址全部是nginx代理后台服務器的地址再轉發給前端,解決跨域問題
#######################################
# 模塊1 location /securityAuthentication/{ proxy_pass http://192.168.0.16:6804;
add_header Content-Type "text/plain;charset=utf-8"; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST'; } # 模塊2 location /admin/{ proxy_pass http://192.168.0.16:6810;
add_header Content-Type "text/plain;charset=utf-8"; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST'; } # 模塊3 location /permission/{ proxy_pass http://192.168.0.16:6810;
add_header Content-Type "text/plain;charset=utf-8"; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST'; } # 模塊4 location /configurationManagement/{ proxy_pass http://192.168.0.16:6812;
add_header Content-Type "text/plain;charset=utf-8"; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST'; } # 模塊5 location /device/{ proxy_pass http://192.168.0.16:6806;
add_header Content-Type "text/plain;charset=utf-8"; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST'; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
# #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
3.win+R打開命令窗口,cd 到nginx文件夾,start nginx啟動服務
4.在瀏覽器輸入localhost:8082 就可以看到nginx服務啟動的頁面啦!