【问题标题】:nginx "env" directive is not allowed here这里不允许使用 nginx“env”指令
【发布时间】:2016-08-26 14:13:36
【问题描述】:

我将nginxdocker-compose 一起使用,并希望docker-composeSERVER_NAME 作为环境变量传递给nginx。我创建了以下 nginx 配置文件:

myconf.conf:

# Environment variables are used to dynamically set SERVER_NAME
# from docker-defined environment variable. This logic  is stolen from:
#
# https://docs.apitools.com/blog/2014/07/02/using-environment-variables-in-nginx-conf.html
# https://github.com/wantedly/nginx-image-server/blob/master/files/nginx.conf

env SERVER_NAME;

upstream django {
    server workflows-django:8000;
}

server {
    listen 80;
    perl_set $server_name_from_env 'sub { return $ENV{"SERVER_NAME"}; }';
    server_name $server_name_from_env;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    perl_set $server_name_from_env 'sub { return $ENV{"SERVER_NAME"}; }';

    server_name $server_name_from_env;
    ssl_certificate /etc/ssl/private/bostongene.crt;
    ssl_certificate_key /etc/ssl/private/bostongene.key;
    charset utf-8;

    client_max_body_size 75M;

    location /media {
        alias /srv/workflows/media;
    }

    location /static {
        alias /srv/workflows/static;
    }

    location / {
        # We can talk to upstream django either via uwsgi or just http proxy

        # uwsgi:
        uwsgi_pass django;
        include /etc/nginx/uwsgi_params;


        # http proxy:
        #proxy_set_header Host $host;
        #proxy_pass http://django
    }
}

由于某种原因,nginx 正在诅咒 env 指令:

2016/08/26 13:02:39 [emerg] 1#0: "env" directive is not allowed here in /etc/nginx/sites-enabled/default:7

这是我的 Dockerfile,您可以在其中看到此配置用作 nginx 默认站点:

Dockerfile:

FROM debian:latest

RUN apt-get update && apt-get install -y nginx \
                        ca-certificates \
                        gettext-base

COPY myconf.conf /etc/nginx/sites-available/default
COPY myconf.crt /etc/ssl/private/
COPY myconf.key /etc/ssl/private/

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

EXPOSE 80 443

CMD ["/usr/sbin/nginx", "-g", "daemon off;"]

为什么?


ANSWER:让我解释一下,详细的问题是什么。 myconf.conf 文件被用作/etc/nginx/sites-available/default,它不是根 nginx 配置。根 nginx 配置是nginx.conf,它在http 上下文中包含/etc/nginx/sites-available/default

http {

    ...
    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

所以我的env 语句实际上是在http 上下文中,这是不正确的 - 它应该只写在根上下文中。

【问题讨论】:

  • @ivan_pozdeev 我已经阅读了该帖子 10 次以上 - 没有直接的答案。似乎我的配置中的env 没有任何问题,只是 nginx 搞砸了,以至于它无法识别来自核心模块的自己的指令:stackoverflow.com/questions/15416957/…
  • 如果你对你的配置有什么问题而不是你应该做什么特别感兴趣,这里有一个答案......

标签: nginx docker-compose


【解决方案1】:

https://bot.ngx.cc/logs/%23nginx/2015/01-January/%23nginx.01-31.log(谷歌搜索“nginx bug”env directive is not allowed here”)给了我答案。

正如server directive1 的存在所证明的那样,这不是您的主要配置文件,而是要包含的一个块,很可能在http context 中。而env 仅在main 上下文中有效。


1如链接所示,server 指令有几种类型,在main 上下文中均无效

【讨论】:

  • 伊万,你是对的!我用这个脚本替换了/etc/nginx/sites-available/default,但它确实是由 nginx.conf 在http 上下文中导入的。我应该修改 nginx.conf 而不是默认值。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 2021-09-10
  • 2014-10-08
  • 1970-01-01
相关资源
最近更新 更多