Valentin Harrang
Valentin Harrang

Reputation: 1191

Error - nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:1

I created a docker-compose.yml file with Nginx, PHP-7.4-fpm, PhpMyAdmin and MariaDB, the execution of my docker-compose.yml has been deployed successfully but the Nginx container has an error:

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

.docker/docker-compose.yml:

version: '3'

services:
    nginx:
        image: nginx:alpine
        volumes:
            - ../:/code
            - ./config/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
            - ./logs:/var/log/nginx
        ports:
            - "80:80"
        depends_on:
            - php

    php:
        build:
            context: './config/php/'
            args:
                PHP_VERSION: ${PHP_VERSION}
        depends_on:
            - mariadb
        volumes:
            - ../:/code
            - ./config/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
        ports:
            - "9000:9000"

    mariadb:
        image: mariadb:${MARIADB_VERSION}
        restart: always
        ports:
            - "3306:3306"
        environment:
            - MYSQL_DATABASE=${MARIADB_DATABASE}
            - MYSQL_USER=${MARIADB_USER}
            - MYSQL_PASSWORD=${MARIADB_PASSWORD}
            - MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
        volumes:
            - ./database:/var/lib/mysql

    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        ports:
            - "${PHPMYADMIN_HTTP_PORT}:80"
        depends_on:
            - mariadb
        environment:
            - MYSQL_USER=${MARIADB_DATABASE}
            - MYSQL_PASSWORD=${MARIADB_PASSWORD}
            - MYSQL_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}

.docker/config/nginx/nginx.conf:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name monsite.local;
    root /code;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

I don't understand why I have this error, can you help me please ?

Thank you in advance

Upvotes: 0

Views: 2319

Answers (1)

John Black
John Black

Reputation: 43

In your context you need to wrap server in http block. Or move you config file not by replacing the default config file, but rather replacing a default server config located in /etc/nginx/conf.d/ So you can use something like

        - ./nginx.conf:/etc/nginx/conf.d/default.conf

for volumes for nginx. That will rewrite your default server config. You can also copy it to sites-enabled folder if you planning to use multiple config files.

Or you can modify your nginx config, by starting with wrapping it with http block

Upvotes: 1

Related Questions