nginx "env" directive is not allowed here

Boris Burkov

I use nginx with docker-compose and want docker-compose to pass SERVER_NAME to nginx as an environment variable. I created the following nginx configuration file:

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
    }
}

For some reason, nginx is cursing about that env directive:

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

Here's my Dockerfile, where you can see that this config is used as nginx default site:

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;"]

Why?


ANSWER: let me exlain, what was the problem in detail. myconf.conf file was used as /etc/nginx/sites-available/default, which is NOT the root nginx config. The root nginx config isnginx.conf and it includes /etc/nginx/sites-available/default in http context:

http {

    ...
    ##
    # Virtual Host Configs
    ##

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

so my env statement is in fact in http context, which is incorrect - it should be written in root context only.

ivan_pozdeev

https://bot.ngx.cc/logs/%23nginx/2015/01-January/%23nginx.01-31.log (googled with "nginx bug "env directive is not allowed here"") has given me the answer.

As testified by the presence of the server directive1, this is not your main configuration file but a chunk to be included, most probably in the http context. While env is only valid in the main context.


1as the link shows, there are a few types of the server directive, none of which are valid in the main context

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

NGINX - "server" directive is not allowed here

From Java

nginx: [emerg] "server" directive is not allowed here

From Dev

nginx directive is not allowed here in unicorn's example nginx.conf

From Dev

Can't Start Nginx: "server" directive is not allowed here

From Dev

"set" directive is not allowed here

From Dev

nginx: [emerg] "worker_processes" directive is not allowed here in /opt/tools/nginx/conf/nginx.conf:1

From Dev

nginx: [emerg] "location" directive is not allowed here in /etc/nginx/conf.d/default.conf:1

From Dev

Nginx Config for Cors - add_header directive is not allowed

From Dev

"AllowOverride not allowed here" in WAMP

From Dev

AllowOverride not allowed here

From Dev

.htaccess: LogLevel not allowed here

From Dev

variable declaration not allowed here

From Dev

Array initializer is not allowed here

From Dev

Column not allowed here error

From Dev

"AllowOverride not allowed here" in WAMP

From Dev

variable declaration not allowed here

From Dev

Is this preprocessor directive acceptable here?

From Dev

Error: 'void' type not allowed here

From Dev

Apache error "AliasMatch not allowed here"

From Dev

cdef statement not allowed here for structure

From Dev

Why is variable declaration not allowed here?

From Dev

Android manifest attribute not allowed here

From Dev

Error: Parse Objects not allowed here

From Dev

Why is lambda function not allowed here?

From Dev

About "group function is not allowed here"

From Dev

Apache error "AliasMatch not allowed here"

From Dev

column not allowed here oracle with getText

From Dev

Error: 'void' type not allowed here

From Dev

@FindBy Annotations not allowed here for CSS

Related Related

HotTag

Archive