开始之前
以前的博客用的vue+node编写的,webpack做好打包文件之后,在项目文件夹下生成dist(webpack 3.0以上)文件,这个文件夹就是要部署的文件目录,另外,server文件需要单独上传,与dist同目录。
安装Nginx
nginx是一款轻量级高性能HTTP和反向代理服务,在阿里注册了一个域名之后,可以无限的生成二级,三级,四级域名,对应不同项目目录。
在VPS服务器上需要下面三个依赖包:
- SSL功能需要openssl库,下载地址 http://www.openssl.org/
- rewrite模块需要pcre库,下载地址 http://www.pcre.org/
- gzip模块需要zlib库,下载地址 http://www.zlib.net/
- Nginx安装包
进入任意目录下载以上压缩包(版本号改为最新即可):
注意各个目录下的是 configure 还是 config
1 | [root@host zlib-1.2.11]# ./configuer |
安装3个依赖包,分别进入各自解压目录
注意:安装 C++ 编译环境 (上面安装过程中如若有报错,可以看看是不是因为没有安装这个,可提前安装)
1 | yum install gcc-c++ |
1 | [root@host ~]# cd /usr/local/nginx |
安装好的Nginx路径在 /usr/local/nginx
配置文件路径:
1 | /usr/local/nginx/conf/nginx.conf |
运行Nginx
1 | [root@host~] |
安装成功的话,浏览器输入 IP 地址或域名即可见到欢迎页面。
使用server命令启动nginx命令
现在nginx启动、关闭比较麻烦,关闭要找到PID号,然后杀死进程,启动要进入到 /usr/local/nginx/sbin 目录下使用命令,为此我们通过设置System V脚本来使用server命令启动、关闭、重启nginx服务。
1.在 /etc/init.d 目录下创建nginx启动脚本文件
1 | [root@host ~] |
2.将以下代码复制粘贴,保存。 注意 NGINX_BIN、CONFIGFILE、PIDFILE 三个目录要对应好,默认是对应好的。在网上找了好多相关脚本代码,都有很多问题,好像是和 CentOS 版本有关,下面脚本我在 CentOS 7 下使用正常,直接从服务器下载用sublime编辑,效率比在vps上vi编译高多了
1 |
|
3.修改脚本权限
1 | chmod a+x /etc/init.d/nginx |
4.注册成服务
1 | chkconfig --add nginx |
5.设置开机启动
1 | chkconfig nginx on |
这样就可以在任意目录通过service启动、关闭nginx
1 | [root@host ~]# service nginx start |
配置nginx
进入nginx下的conf,用编译器打开配置文件nginx.conf,这里下载到本地用sublime打开,方便修改,一般都是修改server里面的内容
1 | cd /usr/local/nginx/conf |
前后端分离,Ajax api接口跨域,node代理和nginx代理
这个博客数据请求服务是用node.js写的,会出现跨域,本地测试的时候在build/config/index.js加入以下语句实现代理
node服务器,在本地测试的时候用的跨域设置
1.比如`
ajax接口"/getArticle"
,本地”localhost:8080/getArticle”,有接口跨域的问题。`
2.localhost:8080 => http:
//localhost:3000/api,本地开启服务器实现代理。
在nginx服务器配置文件nginx.conf:
1 | http { |
比如ajax接口”/getArticle”,本地”localhost:80/getArticle”,有跨域的问题。
localhost:80 => http://127.0.0.1:3000 本地开启服务器实现代理
nginx反向代理更多配置查询http://www.nginx.cn/115.html
注意nginx上写发与本地测试不一样,本地代理服务失效,如果直接location 下写,还是无法通过api获取数据库数据,因为找不到地址,
因此要对location匹配正则,获得正确地址
1 | location ~*/get*|post* { |
向后端express, api请求数据
HTML 5 History模式
vue-router
默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。
相当于请求的是虚拟地址,需要后台设置nginx支持,即就是将所有的前端目录下的请求,统统传到前端index文件上,做路由分发。
这个vue项目使用了HTML 5 History 模式,为了把url的”#”去掉,即在路由设置history模式
因此会遇到问题,进入首页的时候可以正常访问,一旦在浏览器地址栏输入其他地址或者刷新就会出现404问题,因为vue路由编译的dist并没有真正把资源提供给nginx或者其他服务器,所以,需要转交vue-router来做前端路由
所以还需要向nginx添加
1 | location / { |
这个也是来自VUE官方文档,对nginx的配置,其他类型服务器也有对应设置
至此整个部署已经完毕,nginx.conf设置server部分如下
1 | server { |
重启服务器,页面就可以正常访问了,API也能正常拿到
参考资料