nginx:[emerg]";服务器";此处不允许使用指令

nginx:[emerg]";服务器";此处不允许使用指令,nginx,Nginx,我已重新配置nginx,但无法使用以下配置重新启动: 形态: 运行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,但无法使用以下配置重新启动:

形态:

运行
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
配置文件。它是
nginx
配置文件的一部分

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

events {
    ...
}
http {
    ...
    server {
        ...
    }
}
server
块包含在
http
块中

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


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

反向代理的有效nginx.conf示例;以防有人像我一样陷入困境。 其中,
10.x.x.x
是您运行nginx代理服务器并通过浏览器连接到的服务器,
10.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传递,请参阅下面的代码段。也就是说,如果
10.y.y.y
正在运行HTTPS Web服务器。这里
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

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

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

cd/etc/nginx

/etc/nginx$sudo nano nginx.conf

此外,在该文件中,您还可以包括其他文件,这些文件可以具有作为独立服务器块的服务器指令,这些文件不需要位于HTTP或HTTPS块中,如上面接受的答案所述

我重复-如果需要在主配置文件中定义服务器块,则必须在作为nginx主配置文件的
/etc/nginx/nginx.conf
文件中的封闭HTTP或HTTPS块中定义该服务器块

另请注意-如果您在路径
/etc/nginx/conf.d
处的文件中定义了一个不包含在HTTP或HTTPS块中的服务器块,则可以。此外,要实现此功能,您需要在主配置文件中包含此文件的路径,如下所示:-

http{
    include /etc/nginx/conf.d/*.conf; #includes all files of file type.conf
}
除此之外,您还可以从主配置文件中注释掉

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中保留任何配置文件,也不需要将它们符号链接到
/etc/nginx/sites enabled/
,请注意这对我来说是有效的-如果有人认为这不适合他们或这种配置是非法的等,请留下评论,以便我自己更正-谢谢

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

因此不需要INCLUDE指令
INCLUDE/etc/nginx/sites available/some_file.conf

引自Nginx食谱第5页

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


在配置导入的文件中,可能只是输入错误。例如,我在配置文件的深处输入了一个错误:

loccation /sense/movies/ {
                  mp4;
        }
(位置而不是位置),这会导致错误:

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

这句话是正确的,而且被正确地提高了投票率——我试图进一步澄清,可能会帮助像我这样的其他人。我就这样回答了下面这个问题。
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/xxx.xx:1