0%

依赖

项目路径

  • 根路径: /usr/local/share/openresty-docroot
  • 该目录下包含多个子目录,运行时的 document root 为其中的某一个目录,具体哪一个根据 HTTP 请求头 Accept 决定
  • 默认为 default 目录

nginx 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
worker_processes  1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8081;
set $docroot "";
root /usr/local/share/openresty-docroot/$docroot/;
error_log "/var/log/nginx.log";

rewrite_by_lua_block {
ngx.var.docroot = 'default'
local accept = ngx.req.get_headers()["Accept"]

local version = string.gsub(accept, 'application/prs.gbcloud.', '')
version = string.gsub(version, '+json', '')
if (version ~= accept)
then
ngx.var.docroot = version
end
}
}
}

测试

  • 假设 /usr/local/share/openresty-docroot 下面有 defaultv1.2.3v1.2.4 三个目录,每个目录包含一个 index.html 文件,里面的内容分别是 defaultv1.2.3v1.2.4

目录结构

/usr/local/share/openresty-docroot ├── default │   └── index.html ├── v1.2.3 │   └── index.html └── v1.2.4 └── index.html

测试结果

  • 在 postman 里面不传递 Accept 头的时候,访问 http://localhost:8081 会得到 default
  • 传递 Accept: application/prs.gbcloud.v1.2.3+json 的时候,会得到 v1.2.3
  • 传递 Accept: application/prs.gbcloud.v1.2.4+json 的时候,会得到 v1.2.4

  • rsync 的时候 --include --exclude 不会使用绝对路径,全都是相对于 rsync 源目录的路径

  • --include 需要在 --exclude 前面

  • include 和 exclude 的路径前加 "/" 指的是相对 rsync 源目录路径的目录,如果不加,所有子目录下匹配上该路径的都会被匹配

  • 例子:

    • rsync /data/c --include="/e/" --exclude="e" root@127.0.0.1:/data/d,/data/c/d/e 会被同步, /data/c/e 不会被同步
    • rsync /data/c --exclude="e" root@127.0.0.1:/data/d, /data/c/d/e 和 /data/c/e 不会被同步,所有 /data/c 下目录名为 e 的都不会被同步(包括子目录下的)
    • rsync /data/c --include='/c/d/e/' --exclude="e" root@127.0.0.1:/data/d, /data/c/d/e 会被同步,所有 /data/c 下其他目录名为 e 的都不会被同步(包括子目录下的)

upgrade-swoole.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash

# 参考资料:
# https://blog.csdn.net/hutianyou123/article/details/78133309 (CentOS6.5升级手动安装GCC4.8.2 与 CentOS 6.4 编译安装 gcc 4.8.1)
# https://www.cnblogs.com/coyu/p/5750627.html (CentOS yum升级GCC到4.8)

# swoole 4.3.3 依赖 4.8 以后的 gcc, 需要先升级 gcc
wget http://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.bz2
tar -jxvf gcc-4.8.2.tar.bz2

cd gcc-4.8.2
bash ./contrib/download_prerequisites

mkdir gcc-build-4.8.2
cd gcc-build-4.8.2
../configure -enable-checking=release -enable-languages=c,c++ -disable-multilib
make -j4
sudo make install

wget http://people.centos.org/tru/devtools-2/devtools-2.repo
mv devtools-2.repo /etc/yum.repos.d
yum install devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++

mv /usr/bin/gcc /usr/bin/gcc-4.4.7
mv /usr/bin/g++ /usr/bin/g++-4.4.7
mv /usr/bin/c++ /usr/bin/c++-4.4.7
ln -s /opt/rh/devtoolset-2/root/usr/bin/gcc /usr/bin/gcc
ln -s /opt/rh/devtoolset-2/root/usr/bin/c++ /usr/bin/c++
ln -s /opt/rh/devtoolset-2/root/usr/bin/g++ /usr/bin/g++
gcc --version


# 安装 swoole 4.3.3
wget https://github.com/swoole/swoole-src/archive/v4.3.3.tar.gz
tar -xvf v4.3.3.tar.gz
cd swoole-src-4.3.3
phpize
./configure
make
sudo make install

php --re swoole | grep version

1
2
3
location ^~ /abc/ {
return 301 http://192.168.2.123:8503$uri;
}

含义

  • ^~ 正则匹配后面的路径 (这里是 /abc/),这里 location 的作用是匹配所有 uri 前缀是 /abc/ 的请求

  • 301 永久重定向

  • $uri 加上这个防止重定向之后 uri 丢失