亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

前言

由于現在需要部署Nginx的Docker,希望nginx配置文件里面有關server_name在啟動容器前動態修改。
但是由于nginx的配置文件不支持使用環境變量。網上找了好些方案,最終選擇使用envsubst的方式改寫nginx配置文件。

學習envsubst

envsubst就是將環境變量替換文件里面指定標記的值。
例如有如下文件env.conf,內容如下

[test]
ip = ${ip}
port = ${port}
url = http://${ip}:${port}/index.html
phone = ${phone}

當執行export ip=192.168.1.5,export port=8081,export phone=13522223334寫入環境變量。
然后執行envsubst < env.conf > env.new.conf,就可以生成如下的env.new.conf

[test]
ip = 192.168.1.5
port = 8081
url = http://192.168.1.5:8081/index.html
phone = 13522223334

還可以指定只替換部分環境變量,source env.env && envsubst '$ip;$phone' < env.conf,這樣只會替換ip和phone這兩個變量。
上面只替換部分環境變量,在linux測試只能用單引號,用雙引號無效,分隔符試過, . ; |這四種都可以,我估計還有更多分隔符。

應用nginx配置文件

docker-compose.yml文件如下

version: "3"
 
services:
  nginx:
    image: nginx:1.20.1-alpine
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    environment:
      - NGINX_HOST=www.janbar.com
      - NGINX_PORT=80
    volumes:
      - /root/janbar.temp:/etc/nginx/conf.d/janbar.temp
    command: /bin/sh -c "envsubst < /etc/nginx/conf.d/janbar.temp > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
    network_mode: bridge
    restart: always

/root/janbar.temp文件內容如下

server {
    listen       ${NGINX_PORT};
    listen  [::]:${NGINX_PORT};
    server_name  ${NGINX_HOST};

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

按照上述docker-compose.yml配置文件最終生成docker容器里面的配置文件如下cat
/etc/nginx/conf.d/default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  www.janbar.com;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

總結

經過上述騷操作,最終可以通過環境變量的方式更新nginx的docker容器內部配置文件。大功告成!

分享到:
標簽:配置 nginx
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定