Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1K
Visualizações
nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1

I'm trying to setup a server with nginx and docker-compose, but I get these errors every time I try 'docker-compose up':

webserver | 2019/06/10 13:04:16 [emerg] 1#1: "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
webserver | nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1

I've tried wrapping all up with html {}, removing server {}, another port instead of 80...

nginx Dockerfile

FROM nginx

COPY default.conf /etc/nginx/conf.d/default.conf

default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_set_header  Host $host;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_pass http://app:8080/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This happens when you are trying to overwrite the default nginx config file which does not accept some root properties like http, user. If you need those extra configurations you can try copying what you have to /etc/nginx/nginx.conf

So instead of this: COPY default.conf /etc/nginx/conf.d/default.conf

Do this: COPY nginx.conf /etc/nginx/nginx.conf

Note: By copying to the /etc/nginx/conf.d/, you will be overwriting the default config.

over 4 years ago · Santiago Trujillo Relatório

0

I would like to add another solution to the ones proposed above. As above suggested, substituting nginx.conf or default.conf works. I do it in docker-compose.yml:

    volumes:
      - ./services/nginx.conf:/etc/nginx/nginx.conf

However, I came across the necessity to use environment variables in my nginx.conf file, which requires creating a template in /etc/nginx/templates/, which envsubst then outputs in /etc/nginx/conf.d, like explained here.

I came across the following errors:

  • nginx: [emerg] "events" directive is not allowed here in /etc/nginx/conf.d/default.conf
  • nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf

Here is the working solution for the nginx.conf file (the file name does not matter here, since it is changed when copied to container, see docker-compose below, but I like to name it nginx.conf for clarity):

server {
  listen ${FE_PORT};
  location / {
    proxy_pass http://frontend:${FE_PORT};
  }
}
server {
  listen ${BE_PORT};
  location / {
    proxy_pass http://backend:${BE_PORT};
  }
}
server {
  listen ${SERVICE_PORT};
  location / {
    proxy_pass http://service:${SERVICE_PORT};
  }
}

And docker-compose.yml:

nginx:
    image: nginx
    container_name: nginx
    ports:
      - "${FE_PORT}:${FE_PORT}"
      - "${BE_PORT}:${BE_PORT}"
      - "${SERVICE_PORT}:${SERVICE_PORT}"
    environment:
      - FE_PORT
      - BE_PORT
      - SERVICE_PORT
    volumes:
      - ./nginx.conf:/etc/nginx/templates/default.conf.template
over 4 years ago · Santiago Trujillo Relatório

0

Solved this by overwrting nginx.conf.

Dockerfile

FROM nginx

COPY default.conf /etc/nginx/conf.d/default.conf

default.conf

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream app {
        server app:8080;
    }

    server {
        listen 8080;

        location / {
            proxy_pass         http://app;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }

}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda