Home  >  Article  >  Operation and Maintenance  >  How nginx automatically generates configuration files in docker containers

How nginx automatically generates configuration files in docker containers

王林
王林forward
2023-05-25 18:25:061038browse

Implementation ideas

The last command run is probably like this:

docker run -d -p 80:80 -e xxx=xx 镜像名称 镜像中脚本路径

The script here will replace the cmd command in the dockerfile, so we have to build A shell script that automatically generates and starts nginx.

#!/bin/bash

#从环境变量里面获取lt开头,为了与其他环境变量区别开,例如lt_analysis=172.17.0.1:8083
result=""
for a in $(env | grep ^lt)
do
 old_ifs="$ifs"
 ifs="_"
 arr=($a)
 b=${arr[1]}
 ifs="="
 arr=($b)
 ifs="$old_ifs"
 result="${result}
  location /${arr[0]}/ {
    proxy_pass  http://${arr[1]}/${arr[0]}/;
    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;
  }"
done
#将nginx配置文件中nginx_conf中置换成变量result
sed -i "s|nginx_conf|$(echo ${result})|g" /etc/nginx/nginx.conf
cd /usr/sbin
./nginx

One thing that needs to be explained is that the entire configuration file does not need to be generated in the business. It is only necessary to generate the location and then replace the location marked in the original configuration file. The following is the location marked in the original configuration file.

http {
  ...
  
  server {
    ...

    location / {
      root  html;
      index index.html index.htm;
    }

    nginx_conf

    #error_page 404       /404.html;
    ...

I thought I put this shell script and the default configuration file into the nginx dockerfile image, and then it succeeded, but... After running the above command, the container did not start up. Check the container log and see the information. But it is ***syntax error: “(” unexpected***. My shell script has been tested on centos and can be run, so why is this error reported? After investigation, it turns out that the dockerfile uses the basic image as the official nginx , the official image uses ubuntu instead of bash to execute the shell script using dash, which is really a trap. I had no choice but to modify the dockerfile. The following is to use the basic image centos.

from centos

env nginx_version 1.10.3
env openssl_version 1.0.2k
env pcre_version 8.40
env zlib_version 1.2.11
env build_root /usr/local/xx/nginx

# 为了减小最终生成的镜像占用的空间,这里没有执行yum update命令,可能不是好的实践
# 为了加快构建速度,这里使用了163的安装源
#run yum -y update \
run yum -y install curl \
  && mv /etc/yum.repos.d/centos-base.repo /etc/yum.repos.d/centos-base.repo.backup \
  && curl http://mirrors.163.com/.help/centos7-base-163.repo -o /etc/yum.repos.d/centos7-base-163.repo \ 
  && yum -y install gcc gcc-c++ make perl zip unzip \
  && mkdir -p $build_root \
  && cd $build_root \
  && curl https://ftp.pcre.org/pub/pcre/pcre-$pcre_version.zip -o $build_root/pcre-$pcre_version.zip \
  && curl https://www.openssl.org/source/openssl-$openssl_version.tar.gz -o $build_root/openssl-$openssl_version.tar.gz \
  && curl http://www.zlib.net/zlib-$zlib_version.tar.gz -o $build_root/zlib-$zlib_version.tar.gz \
  && curl https://nginx.org/download/nginx-$nginx_version.tar.gz -o $build_root/nginx-$nginx_version.tar.gz \
  && tar vxzf nginx-$nginx_version.tar.gz \
  && unzip pcre-$pcre_version.zip \
  && tar vxfz zlib-$zlib_version.tar.gz \
  && tar vxfz openssl-$openssl_version.tar.gz \
  && cd nginx-$nginx_version \
  && build_config="\
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --http-client-body-temp-path=/var/cache/nginx/client_temp \
    --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
    --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
    --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
    --with-openssl=$build_root/openssl-$openssl_version \
    --with-pcre=$build_root/pcre-$pcre_version \
    --with-zlib=$build_root/zlib-$zlib_version \
    --with-http_ssl_module \
    --with-http_v2_module \ 
    --with-threads \
    " \
  && mkdir -p /var/cache/nginx \
  && ./configure $build_config \
  && make && make install \
  && rm -rf $build_root \
  && yum -y remove gcc gcc-c++ make perl zip unzip \
  && yum clean all

#替换nginx默认文件
copy nginx.conf /etc/nginx/
#添加自动生成配置文件的shell脚本
copy 脚本名称 /root/

#暴露端口
expose 80 443

cmd ["nginx", "-g", "daemon off;"]

Reminder: Docker containers do not support the background Run, when the command is executed, the container will naturally exit. Here we need to set the nginx configuration file

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid    logs/nginx.pid;
daemon off;  //这里添加,关闭后台运行
events {
  worker_connections 1024;
}


http {

The above is the detailed content of How nginx automatically generates configuration files in docker containers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete