vue、react等单页面项目应该这样子部署到服务器
- 更新时间:2020-07-16 08:33:00
- 编辑:傅高谊
参考资料
- Vue.js项目开发实战 PDF 电子书 / 186.6 MB / 张帆 推荐度:
- 前端工程师必备技能:Vue移动开发实战技巧 PDF 电子书 / 122 MB / 李利德 推荐度:
- React学习手册 PDF 电子书 / 47.1 MB / Alex Banks 推荐度:
- Vue.js实战 PDF 电子书 / 196.7 MB / 梁灏 推荐度:
- react快速上手开发 PDF 电子书 / 9.17 MB / 斯托扬·斯特凡诺夫 推荐度:
正文内容
vue、react等单页面项目应该这样子部署到服务器
最近好多伙伴说,我用vue做的项目本地是可以的,但部署到服务器遇到好多问题:资源找不到,直接访问index.html页面空白,刷新当前路由404。。。现在我们一起讨论下单页面如何部署到服务器?
由于前端路由缘故,单页面应用应该放到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就能正确的匹配我们输入的/home路由,从而显示正确的home页面,如果browserHistory模式或history模式的项目没有配置上述内容,会出现404的情况。
简单举两个例子,一个vue项目一个react项目:
vue项目:
域名:http://tb.sosout.com
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' }] }]
############ # 其他配置 ############ 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
/** * 疑惑一: * 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;
############ # 其他配置 ############ 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; } ############ # 其他配置 ############ }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持码农之家。
相关教程
-
详细介绍React-Native 组件之 Modal的使用
本篇文章主要介绍了React-Native 组件之 Modal的使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2020-02-12
-
React Native采用Fetch方式发送跨域POST请求的实例内容
这篇文章主要介绍了详解React Native 采用Fetch方式发送跨域POST请求,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2019-12-31
-
react native实现往服务器上传网络图片详解
下面小编就为大家带来一篇react native实现往服务器上传网络图片的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2020-05-08
-
由ReactJS的Hello world说开来代码实例
这篇文章主要介绍了ReactJS的Hello world程序编写及其相关知识,React是Facebook开发并开源的JS框架,人气在当下急剧攀升,需要的朋友可以参考下
发布时间:2020-02-27
-
React connect的写法总结
这篇文章主要介绍了记React connect的几种写法(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2020-05-08
-
webpack 2的react开发配置详解
本篇文章主要介绍了webpack 2的react开发配置实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2019-07-29
-
分享react中使用css的7中方式
这篇文章主要介绍了react中使用css的7中方式(最全总结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2020-03-16
-
React Native环境搭建步骤分享
本篇文章主要介绍了React Native 环境搭建的教程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2019-12-30
-
React+Spring实现跨域问题及如何解决
这篇文章主要介绍了React+Spring实现跨域问题的完美解决方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
发布时间:2020-06-03
-
分享React 项目迁移 Webpack Babel7的实现
这篇文章主要介绍了React 项目迁移 Webpack Babel7的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
发布时间:2020-02-04
-
Vue.js v2.x 官方教程
Vue是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合。另一方面,当与现代化的工具链以及各种支持类库结合使用时,Vue 也完全能够为复杂的单页应用提供驱动。 易用 会HTML、CSS、JavaScript就可以构建应用。 灵活 可以在一个库和一套完整框架之间自如伸缩。 高效 20kB运行大小,超快虚拟 DOM,最省心的优化 目录 基础 深入了
大小:5.11 MBVue手册
-
前端工程师必备技能:Vue移动开发实战技巧
Vue.js是一个渐进式的JavaScript 框架,与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计。Vue 的核心库只关注视图层,它不仅易于上手,还便于与第三方库或既有项目整合。作为201
大小:122 MBVue实战电子书
-
水晶石技法VUE 10完全学习手册
《水晶石技法 VUE 10完全学习手册》共14章,以实例为主,共提供了50多个制作实例。第1~3章对VUE进行了总体介绍。第4~12章通过大量的实例,分别介绍了VUE的各个功能模块,包括大气、材质、地形、生态系统、函数、动画、渲染,以及VUE和其他软件的结合使用(比如,3ds Max、Poser等)。第13章安排了两个综合实例,让读者在较为全面地了解了VUE的功能之后,综合使用各个知识点来完成更为大型的实例。在本书最后的第14章中,列出了VUE使用过程中的一些常见
大小:114191 MB VUE10学习手册
-
JavaScript+Vue+React全程实例
这书根据基础知识与开发实践活动紧密结合的观念,选萃当今简易、好用和时兴的百余个JavaScript编码案例,协助阅读者学习培训把握JavaScript开发语言。本书內容详实、重中之重突显、浅显易懂,包含了JavaScript前端开发开发的各个方面。
大小:59150 MB MJS实例
-
Vue2实践揭秘
Vue2实践揭秘 以Vue2的实践应用为根基,从实际示例入手,详细讲解Vue2的基础理论应用及高级组件开发,通过简明易懂的实例代码,生动地让读者快速、全方位地掌握Vue2的各种入门技巧以及一些
大小:204.6 MBVue2电子书