【环境配置】nginx环境搭建以及部署vue项目

1. 安装nginx

安装教程:安装nginx

1.1 pcre错误

下载完nginx包,./configure时遇到如下问题:
./configure: error: the HTTP rewrite module requires the PCRE library.
试着使用网上通用的方法安装pcre,但是没有用,还是会报上面的错误

yum -y install pcre-devel openssl openssl-devel

手动安装pcre:
借鉴文章中安装pcre的方法:
nginx 手动安装 及依赖包安装(pcre+zlib+openssl)

2. 启动nginx

2.1 nginx: [emerg] open() “/etc/nginx/mime.types” failed

在sbin中启动nginx,遇到问题:
open() "/etc/nginx/mime.types" failed
在所示的路径中添加文件,参考链接:
Nginx问题 :open() “/etc/nginx/mime.types“ failed

2.2 nginx: [emerg] open() “/var/log/nginx/error.log” failed

检查/var/log/nginx路径,发现文件夹不存在,在/var/log文件夹下新建nginx文件夹

3. 打包vue项目

3.1 各种各样的cannot find module

Cannot find module 'xxx'
执行cnpm install xxx即可
由于遇到的这个问题太多,就把node_modules文件夹删除后,执行cnpm install,再执行cnpm run build打包成功

4. 部署vue项目

4.1 不能通过路径来访问项目

mode: 'history'注释掉

const router = new Router({
    
    
  base: '/',
  // mode: 'history', // 去掉#,需要路由模式改为history
  routes: routes
})

4.2 HTTP 404: 配置了location以后访问接口还是404

原本的nginx.conf配置:

location /api {
    
    
  proxy_pass  http://域名:端口号;
}

根据:vue 打包后访问接口报错404 解决方案,得知,必须在端口号后面写东西,所以把所有需要用到的接口都用如下方式写出来:

扫描二维码关注公众号,回复: 16067418 查看本文章
location /api/user {
    
    
  proxy_pass  http://域名:端口号/user;
}

4.3 HTTP 413: 上传文件部分413

参考:Nginx 上传大文件超大超时解决办法

  • 报错原因
    • Request entity too large 请求实体太大,超过 get 请求限制。上传文件过大。
  • 解决方案
    • http 请求方式如果是 get,建议改成 post。
    • 修改 nginx 配置文件,配置客户端请求大小和缓存大小。
    • 修改/etc/nginx/nginx.conf:

http{} 中输入

client_max_body_size     50m; //文件大小限制,默认1m
client_header_timeout    1m;
client_body_timeout      1m;
proxy_connect_timeout     60s;
proxy_read_timeout      1m;
proxy_send_timeout      1m;

重启 nginx 服务。

4.4 Error:timeout of 5000ms exceeded

timeout of 5000ms exceeded解决方案
将request.js中的timeout从5000改成60000

猜你喜欢

转载自blog.csdn.net/Kandy0125/article/details/121755063