【问题标题】:nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1nginx: [emerg] "http" 指令在 /etc/nginx/conf.d/default.conf:1 中是不允许的
【发布时间】:2020-12-30 16:01:46
【问题描述】:

我正在尝试使用 nginx 和 docker-compose 设置服务器,但每次尝试“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

我已经尝试使用 html {} 来结束所有内容,删除服务器 {},另一个端口而不是 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;
    }
}

【问题讨论】:

  • 远程进入容器并检查所有文件,确保正确的文件位于正确的位置并包含预期的内容。
  • 我什至不能这样做,因为容器在这些错误之后不会启动。

标签: docker nginx


【解决方案1】:

当您尝试覆盖默认 nginx 配置文件时会发生这种情况,该文件不接受某些根属性,例如 httpuser。如果您需要这些额外的配置,您可以尝试将您所拥有的内容复制到 /etc/nginx/nginx.conf

所以不要这样:COPY default.conf /etc/nginx/conf.d/default.conf

这样做:COPY nginx.conf /etc/nginx/nginx.conf

注意:通过复制到/etc/nginx/conf.d/,您将覆盖默认配置。

【讨论】:

    【解决方案2】:

    我想为上面提出的解决方案添加另一个解决方案。如上所述,替换 nginx.confdefault.conf 有效。我在docker-compose.yml:

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

    然而,我在nginx.conf 文件中遇到了必须使用环境变量,这需要在/etc/nginx/templates/ 中创建一个模板,然后envsubst /etc/nginx/conf.d 中的输出,如 here 中的解释。

    我遇到了以下错误:

    • 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

    这是nginx.conf文件的工作解决方案(文件名在这里无关紧要,因为复制到容器时会更改,请参阅下面的docker-compose,但为了清楚起见,我喜欢将其命名为nginx.conf) :

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

    还有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
    

    【讨论】:

      【解决方案3】:

      通过覆盖 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;
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2016-02-08
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        • 2021-09-08
        • 1970-01-01
        • 2020-02-09
        • 1970-01-01
        相关资源
        最近更新 更多