1. 程式人生 > 其它 >vue-cli打包到部署到nginx伺服器

vue-cli打包到部署到nginx伺服器

轉載出處: https://www.cnblogs.com/mmzuo-798/p/9273754.html

最近公司把雲平臺產品用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資料夾,startnginx啟動服務

4.在瀏覽器輸入localhost:8082就可以看到nginx服務啟動的頁面啦!