起因
nginx中大量相似的配置,仅仅前端项目的文件名,端口不同,因此考虑用shell生成
注意,因为我的nginx.conf文件不用改动,仅仅需要生成 default.conf文件,因此,以下文件为default.conf文件。
shell文件内容,根据自己需要进行增删
一、 生成结果展示
# financeapi server配置 由脚本生成
server {
listen 1001;
location /financeapi/fsb/uploadFile/ {
return 307 http://10.10.10.10/financeapi/fsb/uploadFile/;
}
location /financeapi/ {
if ($request_uri ~* "financeapi(/.*$)") {
set $path_remainder $1;
}
proxy_pass http://$remote_addr:8079/$path_remainder;
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_cookie_path / "/; HTTPOnly; SameSite=Lax; Max-Age=86400";
}
location / {
root /home/docker/frontApp/finance-web/dist;
index index.html;
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_cookie_path / "/; HTTPOnly; SameSite=Lax; Max-Age=86400";
}
location = /auth/oauth2/authorize {
rewrite ^ http://auth.lj.com/auth/oauth2/authorize;
}
}
# financeapi server配置 生成成功
# csmapi server配置 由脚本生成
server {
listen 1002;
...手动省略
}
# csmapi server配置 生成成功
... 其他省略
二、shell脚本
gen_nginx.sh 内容
#!/bin/bash
# 获取服务器配置
# source ./server-config.sh
source /etc/nginx/conf.d/server-config.sh
# nginx配置文件路径
# DEFAULT_CONF_PATH="./tmp_default.conf"
DEFAULT_CONF_PATH="/etc/nginx/conf.d/default.conf"
# 清空已存在的所有内容
echo '' > $DEFAULT_CONF_PATH
# 开始生成 nginx配置
for index in "${!servers[@]}"
do
server_element=${servers[$index]}
IFS=':' read -r api_path back_app_port index_root <<< "$server_element"
cat <<EOF >> $DEFAULT_CONF_PATH
# $api_path server配置 由脚本生成
server {
listen $((START_PORT+index));
location /$api_path/fsb/uploadFile/ {
return 307 $FILE_SERVER_URL/$api_path/fsb/uploadFile/;
}
location /$api_path/ {
if (\$request_uri ~* "$api_path(/.*\$)") {
set \$path_remainder \$1;
}
proxy_pass $PROTOCOL\$remote_addr:$back_app_port/\$path_remainder;
$PROXY_HEADER
}
location / {
root /home/docker/frontApp$index_root;
index index.html;
try_files \$uri \$uri/ /index.html;
$PROXY_HEADER
}
location = /auth/oauth2/authorize {
rewrite ^ $PROTOCOL$AUTH_DOMAIN/auth/oauth2/authorize$is_args$args;
}
}
# $api_path server配置 生成成功
EOF
done
配置文件内容,因为考虑尽量不依赖第3方工具,因此还是采用shell文件进行配置
#!/bin/bash
# 服务器配置数组
# 有新增的服务,修改以下配置
# ========================start========================================
# 序号=nginx开放端口:apiPath:indexRoot
servers=(
# DT2.5项目 start
"financeapi:8079:/finance-web/dist"
"csmapi:8003:/csm-web/dist"
"serverapi:8003:/mall-pc-web/dist"
"saasapi:8081:/saas-web/dist"
"tradeapi:8003:/trade-web/dist"
# DT2.5项目 end
)
# ========================end========================================
# 其他参数配置
# 认证登录http前缀
AUTH_DOMAIN="auth.lj.com"
# 测试环境网关URL
FILE_SERVER_URL="http://10.10.10.10"
# 协议
PROTOCOL="http://"
# 公共header部分
PROXY_HEADER='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_cookie_path / "/; HTTPOnly; SameSite=Lax; Max-Age=86400";'
# 起始端口
START_PORT=1001
三、 随Docker启动而执行
因为考虑到编排方便,用的是docker-conpose,
重点为“command” 那段配置,且 “nginx -g 'daemon off;” 作用为:
当default.conf文件生成后,nginx服务在启动
version: "3"
services:
nginx:
image: nginx:1.21.1
container_name: os_nginx
hostname: os_nginx
privileged: true
ports:
- 8080:80
- 1000-1099:1000-1099 #公共应用端口
volumes:
- ./deploy/nginx/sys/resolv.conf:/etc/resolv.conf
- ./deploy/nginx/html/index.html:/home/docker/html/index.html
- ./deploy/nginx/conf.d/:/etc/nginx/conf.d/
- /app/dockerService/custService/frontend_updater/app/frontApp/:/home/docker/frontApp/
command: sh -c "chmod +x /etc/nginx/conf.d/gen_nginx.sh && /etc/nginx/conf.d/gen_nginx.sh && nginx -g 'daemon off;'"
networks:
oversea_bridge:
ipv4_address: 172.160.0.5