系分项目运维日记

运维日记

在代理服务器上配置ngnix服务器反向代理转发API请求遇到如下两个坑

  • 安装ngnix时遇到一些依赖问题
  • vue router history mode在非服务器根目录下conf的配置
    下载安装包
    首先去ngnix官网下载安装包,可以用curl或wget直接下到服务器上
    1
    wget http://nginx.org/download/nginx-1.9.9.tar.gz

解压

1
tar -zxvf nginx-1.9.9.tar.gz

然后先进入解压好的文件夹内编译安装

1
2
cd nginx-1.9.9/
./configure

这时候还没make编译但是报了个少pcre的错误
如下

1
2
3
4
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

然后去pcre官网下安装包,同理用wget或者curl下载到服务器上,然后解压安装,这是官网链接https://ftp.pcre.org/pub/pcre/
同理解压后安装如下

1
2
3
4
5
cd pcre-8.42/
./configure --prefix=/usr/local/pcre
make
make check
sudo make install

上面的check选项用于测试是否编译出错
安装完毕后重新安装ngnix

1
./configure --prefix=/usr/local/nginx  --with-pcre=/usr/local/pcre/

这个时候仍然缺少zlib库

1
2
3
4
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

直接用apt安装

1
2
sudo apt install zlib1g
sudo apt install zlib1g-dev

然后搜了一下资料把其他相关依赖也装了

1
sudo apt install libpcre3 libpcre3-dev libpcrecpp0v5 libssl-dev

之后安装ngnix

1
2
3
./configure
make
sudo make install

去安全组那边开放80端口

Alt text

然后去ngnix目录启动ngnix

1
2
cd /usr/local/ngnix/sbin
sudo ./ngnix

关于Vue的history mode在配置ngnix时的一些坑

vue router默认是hash模式,但是我们在项目中用url的params来确定桌子号故必须用history模式,一开始我按照官网教程编译完直接使用try_files指令配置ngnix但是一直找不到资源,后来翻国外论坛看到了,大概如下配置
首先要更改Vue Router的配置的base路径(下面的/cloud/路径即为服务器中该项目前端资源的路径)

1
2
base: '/cloud/',
// base: __dirname,

然后更改config/index.js里的配置文件

1
2
assetsPublicPath: '/cloud/', 
// assetsPublicPath: '/',

最后在ngnix的配置文件中使用alias命令定位资源

1
2
3
4
5
6
7
location ^~ /cloud/ {
alias /home/ubuntu/EasyOrderMeal/WebOrder/dist/;
index index.html index.htm;
try_files $uri $uri/ /cloud/index.html;
#root html;
#proxy_pass http://localhost:8080;
}