最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年01月03日 14:08:04   作者:sosout  
這篇文章主要介紹了vue、react等單頁(yè)面項(xiàng)目應(yīng)該這樣子部署到服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近好多伙伴說(shuō),我用vue做的項(xiàng)目本地是可以的,但部署到服務(wù)器遇到好多問(wèn)題:資源找不到,直接訪問(wèn)index.html頁(yè)面空白,刷新當(dāng)前路由404。。?,F(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)目沒有配置上述內(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;
  }

  ############
  # 其他配置
  ############  
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue解決跨域路由沖突問(wèn)題思路解析

    vue解決跨域路由沖突問(wèn)題思路解析

    Vue.js(讀音 /vju&#720;/, 類似于 view) 是一套構(gòu)建用戶界面的漸進(jìn)式框架。本文給大家詳細(xì)介紹了vue解決跨域路由沖突問(wèn)題思路解析,需要的朋友參考下吧
    2017-11-11
  • Vue 3.0的attribute強(qiáng)制行為理解學(xué)習(xí)

    Vue 3.0的attribute強(qiáng)制行為理解學(xué)習(xí)

    這篇文章主要為大家介紹了Vue 3.0的attribute強(qiáng)制行為理解學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Vue突然報(bào)錯(cuò)doesn‘t?work?properly?without?JavaScript?enabled的解決方法

    Vue突然報(bào)錯(cuò)doesn‘t?work?properly?without?JavaScript?enabled

    最近在做項(xiàng)目的時(shí)候遇到了些問(wèn)題,所以這篇文章主要給大家介紹了關(guān)于Vue突然報(bào)錯(cuò)doesn‘t?work?properly?without?JavaScript?enabled的解決方法,需要的朋友可以參考下
    2023-01-01
  • vue實(shí)現(xiàn)將一個(gè)數(shù)組內(nèi)的相同數(shù)據(jù)進(jìn)行合并

    vue實(shí)現(xiàn)將一個(gè)數(shù)組內(nèi)的相同數(shù)據(jù)進(jìn)行合并

    今天小編就為大家分享一篇vue實(shí)現(xiàn)將一個(gè)數(shù)組內(nèi)的相同數(shù)據(jù)進(jìn)行合并,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • vue 驗(yàn)證兩次輸入的密碼是否一致的方法示例

    vue 驗(yàn)證兩次輸入的密碼是否一致的方法示例

    這篇文章主要介紹了vue 驗(yàn)證兩次輸入的密碼是否一致的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Vue3實(shí)現(xiàn)pdf預(yù)覽功能

    Vue3實(shí)現(xiàn)pdf預(yù)覽功能

    在開發(fā)中,PDF預(yù)覽和交互功能是一個(gè)常見的需求,無(wú)論是管理系統(tǒng)、在線教育平臺(tái),還是企業(yè)內(nèi)部的知識(shí)庫(kù),能夠高效地展示和操作PDF文件都能極大地提升用戶體驗(yàn),本文將詳細(xì)介紹如何在Vue項(xiàng)目中實(shí)現(xiàn)pdf預(yù)覽功能,需要的朋友可以參考下
    2025-03-03
  • vuex入門教程,圖文+實(shí)例解析

    vuex入門教程,圖文+實(shí)例解析

    這篇文章主要介紹了vuex入門教程,圖文+實(shí)例解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2022-03-03
  • Vue實(shí)現(xiàn)下載文件而非瀏覽器直接打開的方法

    Vue實(shí)現(xiàn)下載文件而非瀏覽器直接打開的方法

    對(duì)于瀏覽器來(lái)說(shuō),文本、圖片等可以直接打開的文件,不會(huì)進(jìn)行自動(dòng)下載,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)下載文件而非瀏覽器直接打開的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • vue項(xiàng)目打包以及優(yōu)化的實(shí)現(xiàn)步驟

    vue項(xiàng)目打包以及優(yōu)化的實(shí)現(xiàn)步驟

    項(xiàng)目完成,我們會(huì)將項(xiàng)目進(jìn)行上線,為了提升性能,我們往往會(huì)進(jìn)行一些優(yōu)化處理,本文主要介紹了vue項(xiàng)目打包以及優(yōu)化的實(shí)現(xiàn)步驟,感興趣的可以了解一下
    2021-07-07
  • vue2.0 實(shí)現(xiàn)頁(yè)面導(dǎo)航提示引導(dǎo)的方法

    vue2.0 實(shí)現(xiàn)頁(yè)面導(dǎo)航提示引導(dǎo)的方法

    下面小編就為大家分享一篇vue2.0 實(shí)現(xiàn)頁(yè)面導(dǎo)航提示引導(dǎo)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03

最新評(píng)論

廊坊市| 旬阳县| 东光县| 固安县| 宁强县| 临猗县| 津市市| 环江| 禹州市| 安国市| 嵊州市| 金乡县| 盐山县| 万全县| 铅山县| 日照市| 保山市| 阿克陶县| 滁州市| 钟祥市| 鄂州市| 石柱| 裕民县| 神木县| 江油市| 犍为县| 新野县| 信阳市| 砀山县| 辽阳县| 盐城市| 孝义市| 金阳县| 南乐县| 北京市| 全州县| 洞头县| 关岭| 章丘市| 娱乐| 镇康县|