Arvin
Arvin

Reputation: 594

Error : nginx: [emerg] unknown directive "listen:" in /etc/nginx/conf.d/default.conf, when mounting local default to nginx via docker compose

Getting error when mounting the default.conf to /etc/nginx/conf.d/default.conf:ro returns error as unknown directive

docker-compose.yml

version: "3"
services:
  nginx:
    image: nginx:stable-alpine
    ports:
        - "3000:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
  node-app:
    environment:
      - PORT=3000
    depends_on:
      - mongo
  mongo:
    image: mongo
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
    volumes:
      - mongo-db:/data/db
  redis:
    image: redis
volumes:
  mongo-db:

default.conf

server {
    listen: 80;

    location /api {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://node-app:3000;
        proxy_redirect off;
    }
}

Log from nginx container

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration

/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh

10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf

10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version

/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh

/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh

/docker-entrypoint.sh: Configuration complete; ready for start up

2022/04/04 05:14:22 [emerg] 1#1: unknown directive "listen:" in /etc/nginx/conf.d/default.conf:2

nginx: [emerg] unknown directive "listen:" in /etc/nginx/conf.d/default.conf:2

Upvotes: 0

Views: 1450

Answers (1)

Timo Stark
Timo Stark

Reputation: 3071

Remove the : behind location. This is simply a configuration "syntax" error. Try this:

server {
    #listen: 80;
    listen 80;
    location /api/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $http_host;
        proxy_set_header X-NGINX-Proxy true;
        proxy_pass http://node-app:3000;
        proxy_redirect off;
    }
}

Upvotes: 1

Related Questions