概述
项目需要生成多个nginx配置文件,每个配置文件对应不同端口。除端口不一致外,其他内容都相同,如果一个一个添加,需要重复使用vim ,echo 等命令,不停的记忆和计算端口,很容易出错,所以找一个便捷的方式:
1.创建nginx配置文件存放目录,并在nginx.conf加入导入配置
include /usr/local/nginx/conf.d/*.conf;
- 通过for 循环生成nginx-30700 ~ nginx-30800 配置文件。
for i in `seq 30700 30800`;
do
echo "upstream nginx-$i {
ip_hash;
server 127.0.0.1:$i max_fails=3 fail_timeout=30s;
}
server {
listen $i ssl;
server_name www.server.com;
#ssl证书的pem文件路径
ssl_certificate /usr/local/nginx/cert/server.crt;
#ssl证书的key文件路径
ssl_certificate_key /usr/local/nginx/cert/server.key;
error_page 500 502 503 504 50x.html;
location = 50x.html {
root usr/share/nginx/html;
}
location /{
proxy_pass http://nginx-$i/;
proxy_set_header Host \$http_host;
proxy_headers_hash_max_size 1024;
proxy_headers_hash_bucket_size 128;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
client_max_body_size 100M;
}
}">nginx-$i.conf;
done