vue項目上線部署的細節

遇到了問題匯總:

1.請求資源地址錯誤: 在webpack配置中的/config/index.js中的 build.assetsPublicPath中修改路徑

  1. 直接訪問index.html空白: 可以通過設置base一般是路由的問題, 如果是根目錄, 應該不會有這樣的問題
  2. 刷新當前路由404: 這個需要在nginx中設置, 把沒有記錄的路徑重定向到index.html,

由于前端路由緣故,單頁面應用應該放到nginx或者apache、tomcat等web代理服務器中,千萬不要直接訪問index.html,同時要根據自己服務器的項目路徑更改react或vue的路由地址。

如果說項目是直接跟在域名后面的,比如:http://www.sosout.com ,根路由就是 '/'。
如果說項目是直接跟在域名后面的一個子目錄中的,比如:http://www.sosout.com/children ,根路由就是 '/children ',不能直接訪問index.html。

以配置Nginx為例,配置過程大致如下:(假設:
1、項目文件目錄: /mnt/html/spa(spa目錄下的文件就是執行了npm run dist 后生成的dist目錄下的文件)
2、訪問域名:spa.sosout.com)
進入nginx.conf新增如下配置:

server { 
     listen 80;
     server_name  spa.sosout.com;
     root /mnt/html/spa;    
     index index.html;    
     location ~ ^/favicon\.ico$ {      
         root /mnt/html/spa;
     }    
         
     location / {        
         try_files $uri $uri/ /index.html;        
         proxy_set_header   Host             $host;        
         proxy_set_header   X-Real-IP        $remote_addr;        
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;        
         proxy_set_header   X-Forwarded-Proto  $scheme;
     }    
     access_log  /mnt/logs/nginx/access.log  main;
}

注意事項:

1、配置域名的話,需要80端口,成功后,只要訪問域名即可訪問的項目
2、如果你使用了react-router的 browserHistory 模式或 vue-router的 history 模式,在nginx配置還需要重寫路由:

server {    
    listen 80;    
    server_name  spa.sosout.com;    
    root /mnt/html/spa;
    index index.html;
    location ~ ^/favicon\.ico$ {        
        root /mnt/html/spa;
    }
        
    location / {        
        try_files $uri $uri/ @fallback;
        index index.html;     
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;   
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
        proxy_set_header   X-Forwarded-Proto  $scheme;
    }
    location @fallback {    
        rewrite ^.*$ /index.html break;
    }   
    access_log  /mnt/logs/nginx/access.log  main;
}

為什么要重寫路由?因為我們的項目只有一個根入口,當輸入類似/home的url時,如果找不到對應的頁面,nginx會嘗試加載index.html,這是通過react-router或vue-router就能正確的匹配我們輸入的/home路由,從而顯示正確的home頁面,如果browserHistory模式或history模式的項目沒有配置上述內容,會出現404的情況。

簡單舉兩個例子,一個vue項目一個react項目:

vue項目

域名:http://tb.sosout.com

image
import App from '../App'// 首頁const home = r => require.ensure([], () => r(require('../page/home/index')), 'home')// 物流const logistics = r => require.ensure([], () => r(require('../page/logistics/index')), 'logistics')// 購物車const cart = r => require.ensure([], () => r(require('../page/cart/index')), 'cart')// 我的const profile = r => require.ensure([], () => r(require('../page/profile/index')), 'profile')// 登錄界面const login = r => require.ensure([], () => r(require('../page/user/login')), 'login')export default [{
  path: '/',
  component: App, // 頂層路由,對應index.html
  children: [{
    path: '/home', // 首頁
    component: home
  }, {
    path: '/logistics', // 物流
    component: logistics,
    meta: {
      login: true
    }
  }, {
    path: '/cart', // 購物車
    component: cart,
    meta: {
      login: true
    }
  }, {
    path: '/profile', // 我的
    component: profile
  }, {
    path: '/login', // 登錄界面
    component: login
  }, {
    path: '*',
    redirect: '/home'
  }]
}]
image
############
# 其他配置
############

http {
    ############
    # 其他配置
    ############
    server {
        listen 80;
        server_name  tb.sosout.com;
        root /mnt/html/tb;
        index index.html;
        location ~ ^/favicon\.ico$ {
            root /mnt/html/tb;
        }
    
        location / {
            try_files $uri $uri/ @fallback;
            index index.html;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto  $scheme;
        }
        location @fallback {
            rewrite ^.*$ /index.html break;
        }
        access_log  /mnt/logs/nginx/access.log  main;
    }
    ############
    # 其他配置
    ############   
}

react項目

域名:http://antd.sosout.com

image
/**
* 疑惑一:
* React createClass 和 extends React.Component 有什么區別?
* 之前寫法:
* let app = React.createClass({
*      getInitialState: function(){
*        // some thing
*      }
*  })
* ES6寫法(通過es6類的繼承實現時state的初始化要在constructor中聲明):
* class exampleComponent extends React.Component {
*    constructor(props) {
*        super(props);
*        this.state = {example: 'example'}
*    }
* }
*/import React, {Component, PropTypes} from 'react'; // react核心import { Router, Route, Redirect, IndexRoute, browserHistory, hashHistory } from 'react-router'; // 創建route所需import Config from '../config/index';
import layout from '../component/layout/layout'; // 布局界面import login from '../containers/login/login'; // 登錄界面/**
 * (路由根目錄組件,顯示當前符合條件的組件)
 * 
 * @class Roots
 * @extends {Component}
 */class Roots extends Component {
    render() {                // 這個組件是一個包裹組件,所有的路由跳轉的頁面都會以this.props.children的形式加載到本組件下
        return (
            <div>{this.props.children}</div>
        );
    }
}// const history = process.env.NODE_ENV !== 'production' ? browserHistory : hashHistory;// 快速入門const home = (location, cb) => {        require.ensure([], require => {
         cb(null, require('../containers/home/homeIndex').default)
     }, 'home');
}    // 百度圖表-折線圖const chartLine = (location, cb) => {        require.ensure([], require => {
         cb(null, require('../containers/charts/lines').default)
    }, 'chartLine');
}// 基礎組件-按鈕const button = (location, cb) => {       require.ensure([], require => {
         cb(null, require('../containers/general/buttonIndex').default)
    }, 'button');
}// 基礎組件-圖標const icon = (location, cb) => {        require.ensure([], require => {
         cb(null, require('../containers/general/iconIndex').default)
    }, 'icon');
}// 用戶管理const user = (location, cb) => {       require.ensure([], require => {
         cb(null, require('../containers/user/userIndex').default)
    }, 'user');
}// 系統設置const setting = (location, cb) => {        require.ensure([], require => {
         cb(null, require('../containers/setting/settingIndex').default)
    }, 'setting');
}// 廣告管理const adver = (location, cb) => {      require.ensure([], require => {
         cb(null, require('../containers/adver/adverIndex').default)
    }, 'adver');
}// 組件一const oneui = (location, cb) => {       require.ensure([], require => {
         cb(null, require('../containers/ui/oneIndex').default)
    }, 'oneui');
}// 組件二const twoui = (location, cb) => {        require.ensure([], require => {
         cb(null, require('../containers/ui/twoIndex').default)
    }, 'twoui');
}// 登錄驗證const requireAuth = (nextState, replace) => {        let token = (new Date()).getTime() - Config.localItem('USER_AUTHORIZATION');        if(token > 7200000) { // 模擬Token保存2個小時
         replace({
            pathname: '/login',
            state: { nextPathname: nextState.location.pathname }
        });
    }
}const RouteConfig = (
    <Router history={browserHistory}>
        <Route path="/home" component={layout} onEnter={requireAuth}>
            <IndexRoute getComponent={home} onEnter={requireAuth} /> // 默認加載的組件,比如訪問www.test.com,會自動跳轉到www.test.com/home
            <Route path="/home" getComponent={home} onEnter={requireAuth} />
            <Route path="/chart/line" getComponent={chartLine} onEnter={requireAuth} />
            <Route path="/general/button" getComponent={button} onEnter={requireAuth} />
            <Route path="/general/icon" getComponent={icon} onEnter={requireAuth} />
            <Route path="/user" getComponent={user} onEnter={requireAuth} />
            <Route path="/setting" getComponent={setting} onEnter={requireAuth} />
            <Route path="/adver" getComponent={adver} onEnter={requireAuth} />
            <Route path="/ui/oneui" getComponent={oneui} onEnter={requireAuth} />
            <Route path="/ui/twoui" getComponent={twoui} onEnter={requireAuth} />
        </Route>
        <Route path="/login" component={Roots}> // 所有的訪問,都跳轉到Roots
            <IndexRoute component={login} /> // 默認加載的組件,比如訪問www.test.com,會自動跳轉到www.test.com/home
        </Route>
        <Redirect from="*" to="/home" />
    </Router>
);

export default RouteConfig;
image
############
# 其他配置
############

http {
    ############
    # 其他配置
    ############
    server {
        listen 80;
        server_name  antd.sosout.com;
        root /mnt/html/reactAntd;
        index index.html;
        location ~ ^/favicon\.ico$ {
            root /mnt/html/reactAntd;
        }

        location / {
            try_files $uri $uri/ @router;
            index index.html;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto  $scheme;
        }
        location @router {
            rewrite ^.*$ /index.html break;
        }
        access_log  /mnt/logs/nginx/access.log  main;
    }

    ############
    # 其他配置
    ############   
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,117評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,860評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,128評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,291評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,025評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,421評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,477評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,642評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,177評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,970評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,157評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,717評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,410評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,821評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,053評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,896評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,157評論 2 375

推薦閱讀更多精彩內容