nginx 前端打包部署


首先配置前端環境.env.preview文件中VUE_APP_API_BASE_URL指向后端地址

之后,使用命令 yarn run build 打包后前端根目錄出現dict文件夾
yarn run build 或者 npm run build


使用Nginx代理部署前端,下載最新版本 Nginx (opens new window),下面由Windows為例介紹

server {
        listen       81;
        server_name  localhost;
        location / {
            root   "C:\前端打包存放位置\dist";
			try_files $uri $uri/ @router;
            index  index.html index.htm;
			error_page 405 =200 http://$host$request_uri;
        }
		#代理后端接口
		location /api/ {
			proxy_pass http://后端IP:82/;   #轉發請求的地址
			proxy_connect_timeout 6000;     #鏈接超時設置
			proxy_read_timeout 6000;        #訪問接口超時設置
		}
		location @router {
            rewrite ^.*$ /index.html last;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
最后一步:將Nginx啟之

PS:啟動Nginx直接點擊exe文件,閃一下就沒了,實則已啟動,再去訪問就行啦

可能會遇到啟動前端后,訪問后端時,前端控制台出現404情況,這個是因為代理地址沒配置正確,地址最后面必須加: /

關掉nginx進程

taskkill /f /t /im nginx.exe


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM