<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

        vue、react等單頁(yè)面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:22:21
        文檔

        vue、react等單頁(yè)面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器

        vue、react等單頁(yè)面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器:最近好多伙伴說(shuō),我用vue做的項(xiàng)目本地是可以的,但部署到服務(wù)器遇到好多問(wèn)題:資源找不到,直接訪問(wèn)index.html頁(yè)面空白,刷新當(dāng)前路由404。現(xiàn)在我們一起討論下單頁(yè)面如何部署到服務(wù)器? 由于前端路由緣故,單頁(yè)面應(yīng)用應(yīng)該放到nginx或者apache、to
        推薦度:
        導(dǎo)讀vue、react等單頁(yè)面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器:最近好多伙伴說(shuō),我用vue做的項(xiàng)目本地是可以的,但部署到服務(wù)器遇到好多問(wèn)題:資源找不到,直接訪問(wèn)index.html頁(yè)面空白,刷新當(dāng)前路由404。現(xiàn)在我們一起討論下單頁(yè)面如何部署到服務(wù)器? 由于前端路由緣故,單頁(yè)面應(yīng)用應(yīng)該放到nginx或者apache、to

        最近好多伙伴說(shuō),我用vue做的項(xiàng)目本地是可以的,但部署到服務(wù)器遇到好多問(wèn)題:資源找不到,直接訪問(wèn)index.html頁(yè)面空白,刷新當(dāng)前路由404。。現(xiàn)在我們一起討論下單頁(yè)面如何部署到服務(wù)器?

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

        如果說(shuō)項(xiàng)目是直接跟在域名后面的,比如:http://www.sosout.com ,根路由就是 '/'。

        如果說(shuō)項(xiàng)目是直接跟在域名后面的一個(gè)子目錄中的,比如:http://www.sosout.com/children ,根路由就是 '/children ',不能直接訪問(wèn)index.html。

        以配置Nginx為例,配置過(guò)程大致如下:(假設(shè):

        1、項(xiàng)目文件目錄: /mnt/html/spa(spa目錄下的文件就是執(zhí)行了npm run dist 后生成的dist目錄下的文件)

        2、訪問(wèn)域名:spa.sosout.com)

        進(jìn)入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;
        }
        
        

        注意事項(xiàng):

        1、配置域名的話,需要80端口,成功后,只要訪問(wèn)域名即可訪問(wèn)的項(xiàng)目

        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;
        }
        
        

        為什么要重寫路由?因?yàn)槲覀兊捻?xiàng)目只有一個(gè)根入口,當(dāng)輸入類似/home的url時(shí),如果找不到對(duì)應(yīng)的頁(yè)面,nginx會(huì)嘗試加載index.html,這是通過(guò)react-router就能正確的匹配我們輸入的/home路由,從而顯示正確的home頁(yè)面,如果browserHistory模式或history模式的項(xiàng)目沒(méi)有配置上述內(nèi)容,會(huì)出現(xiàn)404的情況。

        簡(jiǎn)單舉兩個(gè)例子,一個(gè)vue項(xiàng)目一個(gè)react項(xiàng)目:

        vue項(xiàng)目:

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

        import App from '../App'
        // 首頁(yè)
        const home = r => require.ensure([], () => r(require('../page/home/index')), 'home')
        // 物流
        const logistics = r => require.ensure([], () => r(require('../page/logistics/index')), 'logistics')
        // 購(gòu)物車
        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, // 頂層路由,對(duì)應(yīng)index.html
         children: [{
         path: '/home', // 首頁(yè)
         component: home
         }, {
         path: '/logistics', // 物流
         component: logistics,
         meta: {
         login: true
         }
         }, {
         path: '/cart', // 購(gòu)物車
         component: cart,
         meta: {
         login: true
         }
         }, {
         path: '/profile', // 我的
         component: profile
         }, {
         path: '/login', // 登錄界面
         component: login
         }, {
         path: '*',
         redirect: '/home'
         }]
        }]
        

        ############
        # 其他配置
        ############
        
        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項(xiàng)目:

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

        /**
        * 疑惑一:
        * React createClass 和 extends React.Component 有什么區(qū)別?
        * 之前寫法:
        * let app = React.createClass({
        * getInitialState: function(){
        * // some thing
        * }
        * })
        * ES6寫法(通過(guò)es6類的繼承實(shí)現(xiàn)時(shí)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'; // 創(chuàng)建route所需
        import Config from '../config/index';
        import layout from '../component/layout/layout'; // 布局界面
        
        import login from '../containers/login/login'; // 登錄界面
        
        /**
         * (路由根目錄組件,顯示當(dāng)前符合條件的組件)
         * 
         * @class Roots
         * @extends {Component}
         */
        class Roots extends Component {
         render() {
         // 這個(gè)組件是一個(gè)包裹組件,所有的路由跳轉(zhuǎn)的頁(yè)面都會(huì)以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');
        }
        
        // 基礎(chǔ)組件-按鈕
        const button = (location, cb) => {
         require.ensure([], require => {
         cb(null, require('../containers/general/buttonIndex').default)
         }, 'button');
        }
        
        // 基礎(chǔ)組件-圖標(biāo)
        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');
        }
        
        // 系統(tǒng)設(shè)置
        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');
        }
        
        // 登錄驗(yàn)證
        const requireAuth = (nextState, replace) => {
         let token = (new Date()).getTime() - Config.localItem('USER_AUTHORIZATION');
         if(token > 7200000) { // 模擬Token保存2個(gè)小時(shí)
         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} /> // 默認(rèn)加載的組件,比如訪問(wèn)www.test.com,會(huì)自動(dòng)跳轉(zhuǎn)到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}> // 所有的訪問(wèn),都跳轉(zhuǎn)到Roots
         <IndexRoute component={login} /> // 默認(rèn)加載的組件,比如訪問(wèn)www.test.com,會(huì)自動(dòng)跳轉(zhuǎn)到www.test.com/home
         </Route>
         <Redirect from="*" to="/home" />
         </Router>
        );
        
        export default RouteConfig;
        
        

        ############
        # 其他配置
        ############
        
        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;
         }
        
         ############
         # 其他配置
         ############ 
        }
        
        

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        vue、react等單頁(yè)面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器

        vue、react等單頁(yè)面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器:最近好多伙伴說(shuō),我用vue做的項(xiàng)目本地是可以的,但部署到服務(wù)器遇到好多問(wèn)題:資源找不到,直接訪問(wèn)index.html頁(yè)面空白,刷新當(dāng)前路由404。現(xiàn)在我們一起討論下單頁(yè)面如何部署到服務(wù)器? 由于前端路由緣故,單頁(yè)面應(yīng)用應(yīng)該放到nginx或者apache、to
        推薦度:
        標(biāo)簽: VUE 需要 服務(wù)器
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 免费大片黄在线观看yw| 99久久精品免费视频| 暖暖日本免费在线视频| 亚洲一线产区二线产区精华| 最好看的中文字幕2019免费| 亚洲福利电影在线观看| 无码人妻一区二区三区免费手机| 亚洲国产精品成人综合久久久| 国产免费丝袜调教视频| 一边摸一边爽一边叫床免费视频| 国产免费黄色大片| 免费人成网上在线观看| aⅴ免费在线观看| 亚洲不卡中文字幕| 国产成人免费网站在线观看 | 国产精品视频免费| 亚洲看片无码在线视频| 超pen个人视频国产免费观看| 精品女同一区二区三区免费播放| 亚洲精品亚洲人成在线观看下载| 两个人的视频www免费| 亚洲福利一区二区| 暖暖日本免费在线视频| aaa毛片视频免费观看| 亚洲视频在线不卡| 黄网址在线永久免费观看| 欧洲美女大片免费播放器视频| 亚洲日产无码中文字幕| 国产免费不卡视频| 色婷婷精品免费视频| 精品久久久久成人码免费动漫| 亚洲欧洲无卡二区视頻| 亚洲中文字幕成人在线| 18女人毛片水真多免费| 亚洲AV性色在线观看| 亚洲AV无码国产在丝袜线观看| 成年美女黄网站18禁免费 | 亚洲四虎永久在线播放| 国产最新凸凹视频免费| 中文字幕免费不卡二区| 亚洲午夜理论片在线观看|