nginx:这里不允许[emerg]"server"指令

sam*_*x73 77 nginx

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

CONF:

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;
}
}
Run Code Online (Sandbox Code Playgroud)

在运行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
Run Code Online (Sandbox Code Playgroud)

Ric*_*ith 139

那不是nginx配置文件.这是部分的的nginx配置文件.

nginx配置文件(通常称为nginx.conf)的样子:

events {
    ...
}
http {
    ...
    server {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

server块被包含在一个http块内.

通常,配置分布在多个文件中,通过使用include指令引入其他片段(例如从sites-enabled目录中).

使用sudo nginx -t测试完整的配置文件,它开始于nginx.conf并使用额外的片段拉include指令.有关更多信息,请参阅此文

  • 这个答案是正确的,并且已经被正确地投票 - 我试图进一步澄清,可能会帮助像我这样的其他菜鸟。下面就这样回答了这个问题。 (2认同)

Ale*_*nen 22

反向代理的有效 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;
   }
 }
}

Run Code Online (Sandbox Code Playgroud)

如果您想通过 SSL,这里是代码片段。也就是说,如果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;
  }
}
Run Code Online (Sandbox Code Playgroud)

你也可以在 docker 中提供它

 docker run --name nginx-container --rm --net=host   -v /home/core/nginx/nginx.conf:/etc/nginx/nginx.conf nginx
Run Code Online (Sandbox Code Playgroud)


Roh*_*kar 10

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

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

cd /etc/nginx

/etc/nginx$ sudo nano nginx.conf

此外,在此文件中,您可以包含其他文件 - 可以将 SERVER 指令作为独立的 SERVER 块 - 不需要在 HTTP 或 HTTPS 块内,如上面接受的答案中所述。

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

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

http{
    include /etc/nginx/conf.d/*.conf; #includes all files of file type.conf
}
Run Code Online (Sandbox Code Playgroud)

除此之外,您可以从 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
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

引自 Nginx CookBook page - 5。

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


boj*_*jan 6

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

loccation /sense/movies/ {
                  mp4;
        }
Run Code Online (Sandbox Code Playgroud)

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

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/xxx.xx:1
Run Code Online (Sandbox Code Playgroud)