【问题标题】:Nginx set SSL certificate values from environment variablesNginx 从环境变量中设置 SSL 证书值
【发布时间】:2021-11-23 21:31:18
【问题描述】:

我正在尝试从环境变量中为 Dockerised 反向代理设置 Nginx ssl 服务器值,这样我就可以避免配置多个文件。

我遇到的问题是 Nginx 要么不识别环境变量,要么给我语法错误,说明我没有用 ; 终止该行。

这是我得到的错误:

nginx_1 | nginx: [emerg] directive "ssl_certificate" is not terminated by ";" in /etc/nginx/conf.d/default.conf:10

我的ngnx.conf

server { 
    listen 443 ssl;
    listen [::]:443 ssl;

    include /etc/nginx/snippets/ssl-params.conf;

    server_name         website.com www.website.com;
    access_log          /vol/log/nginx/website.access.log;
    error_log           /vol/log/nginx/website.error.log;
    ssl_certificate     /etc/ssl/$ENV{FULLCHAIN} ;
    ssl_certificate_key /etc/ssl/$ENV{PRIVKEY} ;
    
    location /static {
        alias /vol/static;
    }

    location / {
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-NginX-Proxy true;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_pass              http://app:5000/;
        proxy_ssl_session_reuse off;
        proxy_set_header        Host $http_host;
        proxy_cache_bypass      $http_upgrade;
        proxy_redirect          off;
        client_max_body_size    10M;
    }
}

我也尝试了以下变体,但似乎没有什么是有效的:

/etc/ssl/$ENV{FULLCHAIN} /etc/ssl/$FULLCHAIN /etc/ssl/"$ENV{FULLCHAIN}"

我已经检查了环境变量是 sh 并且它们存在,只有 Nginx 看不到它们。

这样做的正确方法是什么,或者我最好在构建时做一些 bash fu 来编辑?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    您可以创建一个带环境变量的 sh 文件并保存为文件 .cer、pem... 并将此文件添加为入口点。

    示例 docker-set-certificates.sh>

    #!/bin/bash
    set -e
    
    mkdir -p /etc/ssl
    echo "$SSL_Server_Certificate" >> /etc/ssl/SSL_Server_Certificate.cer
    echo "$SSL_Server_key" >> /etc/ssl/SSL_Server_key.pem
    echo "$SSL_Client_Certificate" >> /etc/ssl/SSL_Client_Certificate.cer
    
    exec "$@"
    

    在 dockerFile>

    ENTRYPOINT [ "/etc/ssl/docker-set-certificates.sh" ]
    CMD ["nginx", "-g", "daemon off;"]
    

    完成!!

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 2016-01-04
      • 2014-08-12
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 2021-07-30
      • 2016-09-27
      • 1970-01-01
      相关资源
      最近更新 更多