【问题标题】:Error - nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:1错误 - nginx: [emerg] "server" 指令在 /etc/nginx/nginx.conf:1 中是不允许的
【发布时间】:2020-01-23 03:18:07
【问题描述】:

我用 Nginx、PHP-7.4-fpm、PhpMyAdmin 和 MariaDB 创建了一个 docker-compose.yml 文件,我的 docker-compose.yml 的执行已经部署成功但是 Nginx 容器有错误:

nginx: [emerg] "server" 指令在 /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;
}

我不明白为什么会出现这个错误,请您帮帮我吗?

提前谢谢你

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

    在您的上下文中,您需要将服务器包装在 http 块中。或者移动你的配置文件不是通过替换默认配置文件,而是替换位于 /etc/nginx/conf.d/ 的默认服务器配置所以你可以使用类似的东西

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

    用于 nginx 的卷。这将重写您的默认服务器配置。 如果您打算使用多个配置文件,也可以将其复制到启用站点的文件夹。

    或者你可以修改你的 nginx 配置,首先用 http 块包装它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      相关资源
      最近更新 更多