【问题标题】:nginx: [emerg] "server" directive is not allowed herenginx: [emerg] "server" 指令在这里不允许
【发布时间】:2017-06-05 14:13:22
【问题描述】:

我已经重新配置了 nginx,但我无法使用以下配置重新启动它:

配置:

server {
   listen 80;
   server_name www.example.com;
   return 301 $scheme://example.com$request_uri;
}


server {
   listen 80;
   server_name example.com;

   access_log /var/log/nginx/access.log;
   error_log  /var/log/nginx/error.log;

   location /robots.txt {
       alias /path/to/robots.txt;
       access_log off;
       log_not_found off;
   }

   location = /favicon.ico { access_log off; log_not_found off; }

   location / {
      proxy_pass_header Server;
      proxy_set_header Host $http_host;
      proxy_redirect off;
          proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Scheme $scheme;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_connect_timeout 30;
      proxy_read_timeout 30;
      proxy_pass http://127.0.0.1:8000;
   }

   location /static {
      expires 1M;
      alias  /path/to/staticfiles;
   }
}

运行sudo nginx -c conf -t 测试配置后返回以下错误,我无法弄清楚真正的问题是什么

nginx: [emerg] "server" directive is not allowed here in    /etc/nginx/sites-available/config:1
nginx: configuration file /etc/nginx/sites-available/config test failed

【问题讨论】:

    标签: nginx


    【解决方案1】:

    这不是nginx 配置文件。它是nginx 配置文件的一部分

    nginx 配置文件(通常称为nginx.conf)如下所示:

    events {
        ...
    }
    http {
        ...
        server {
            ...
        }
    }
    

    server 块包含在 http 块中。

    配置通常分布在多个文件中,通过使用include 指令拉入额外的片段(例如来自sites-enabled 目录)。

    使用sudo nginx -t 测试完整的配置文件,该文件从nginx.conf 开始,并使用include 指令引入其他片段。请参阅this document 了解更多信息。

    【讨论】:

    • 这个答案是正确的,并且得到了正确的支持 - 我试图进一步澄清,可能会帮助像我这样的其他菜鸟。因此在下面回答了这个问题。
    【解决方案2】:

    用于反向代理的有效 nginx.conf 示例;万一有人像我一样被卡住了。 其中10.x.x.x 是您正在运行 nginx 代理服务器并使用浏览器连接到的服务器,10.y.y.y 是您真正的 Web 服务器正在运行的位置

    events {
      worker_connections  4096;  ## Default: 1024
    }
    http {
     server {
       listen 80;
       listen [::]:80;
    
       server_name 10.x.x.x;
     
       location / {
           proxy_pass http://10.y.y.y:80/;
           proxy_set_header Host $host;
       }
     }
    }
    
    

    如果你想进行 SSL 传递,这里是 sn-p。也就是说,10.y.y.y 正在运行 HTTPS 网络服务器。这里10.x.x.x,或者 nignx 运行的地方正在监听 443 端口,所有到 443 的流量都被定向到你的目标 web 服务器

    events {
      worker_connections  4096;  ## Default: 1024
    }
    
    stream {
      server {
        listen     443;
        proxy_pass 10.y.y.y:443;
      }
    }
    

    你也可以在 docker 中提供它

     docker run --name nginx-container --rm --net=host   -v /home/core/nginx/nginx.conf:/etc/nginx/nginx.conf nginx
    

    【讨论】:

      【解决方案3】:

      nginx.conf 文件的路径是 Nginx 的主要配置文件 - 该文件也是在需要时应包含其他 Nginx 配置文件的路径的文件是 /etc/nginx/nginx.conf

      您可以通过在终端输入此文件来访问和编辑此文件

      cd /etc/nginx

      /etc/nginx$ sudo nano nginx.conf

      在此文件的进一步内容中,您可以包含其他文件 - 可以将 SERVER 指令作为独立的 SERVER BLOCK - 这些文件不需要位于 HTTP 或 HTTPS 块中,正如上面接受的答案中所阐明的那样。

      我重复一遍 - 如果您需要在 PRIMARY Config 文件本身中定义一个 SERVER BLOCK,那么该 SERVER BLOCK 必须在 /etc/nginx/nginx.conf 文件中的封闭 HTTP 或 HTTPS 块中定义,该文件是主要配置文件Nginx。

      还请注意,如果您在位于路径 /etc/nginx/conf.d 的文件中定义一个不将其直接包含在 HTTP 或 HTTPS 块中的 SERVER BLOCK ,则它是可以的。此外,为了完成这项工作,您需要在 PRIMARY Config 文件中包含此文件的路径,如下所示:-

      http{
          include /etc/nginx/conf.d/*.conf; #includes all files of file type.conf
      }
      

      除此之外,您可以从 PRIMARY Config 文件中注释掉这一行

      http{
          #include /etc/nginx/sites-available/some_file.conf; # Comment Out 
          include /etc/nginx/conf.d/*.conf; #includes all files of file type.conf
      }
      

      并且不需要在/etc/nginx/sites-available/ 中保留任何配置文件,也不需要将它们符号链接到/etc/nginx/sites-enabled/,请注意这对我有用 - 以防有人认为它不适合他们或这种配置是非法的等等,请留下评论,以便我纠正自己-谢谢。

      编辑:- 根据官方 Nginx CookBook 的最新版本,我们不需要在 -/etc/nginx/sites-enabled/ 中创建任何配置,这是较旧的做法,现在已弃用。

      因此不需要包含指令include /etc/nginx/sites-available/some_file.conf;

      引自 Nginx CookBook 页面 - 5 。

      "在某些软件包存储库中,此文件夹被命名为启用站点,并且 配置文件从名为 site-available 的文件夹链接; 该约定已被弃用。”

      【讨论】:

        【解决方案4】:

        配置导入的文件中的任何地方可能只是一个错字。例如,我在我的配置文件深处打了一个错字:

        loccation /sense/movies/ {
                          mp4;
                }
        

        (位置而不是位置),这会导致错误:

        nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/xxx.xx:1
        

        【讨论】:

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