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



我已经重新配置了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配置文件。它是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.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网络服务器。10.x.x.x正在侦听端口 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文件的路径是 Nginx 的主要配置文件 - 该文件也应在需要时包含其他 Nginx 配置文件的路径,/etc/nginx/nginx.conf

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

cd /etc/nginx

/etc/nginx$ sudo nano nginx.conf

在此文件中,您可以包含其他文件 - 这些文件可以具有SERVER指令作为独立的SERVER BLOCK-它们不必在HTTP或HTTPS块中,如上面接受的答案中所述。

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

另请注意 - 如果您定义 ,服务器块直接不将其包含在 HTTP 或 HTTPS 块中,则没关系,位于路径/etc/nginx/conf.d的文件中。同样要完成这项工作,您需要将此文件的路径包含在 PRIMARY 配置文件中,如下所示:-

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

此外,您可以从 PRIMARY 配置文件中注释掉 ,该行

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 食谱,我们不需要在 -/etc/nginx/sites-enabled/中创建任何配置,这是旧的做法,现在已贬值。

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

引用自 Nginx 食谱页面 - 5 .

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

配置导入的文件内可能存在拼写错误。

我的配置文件深处有这个错字:

loccation /sense/movies/ {
mp4;
}

loccation而不是location,这会导致错误:

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

替换这个:

include /etc/nginx/conf.d/*.conf;

在你nginx.conf中:

include /etc/nginx/conf.d/includes-optional/cpanel-proxy-vendors/*.conf;

或者这个:

/etc/nginx/conf.d/includes-optional/site-available/*.conf;

我发生了同样的错误,以下是我的配置: 操作系统和nginx版本:

[root@primary2 ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[root@primary2 ~]# uname -rm
3.10.0-693.el7.x86_64 x86_64
[root@primary2 ~]# /etc/nginx/sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --with-http_ssl_module
[root@primary2 ~]# 

NGINX配置文件:

/etc/nging/conf/nginx.conf

[root@primary2 conf]# pwd
/etc/nginx/conf
[root@primary2 conf]# cat nginx.conf
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
include /etc/nginx/conf.d/*.conf;
events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;
#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#access_log  logs/access.log  main;
sendfile        on;
#tcp_nopush     on;
#keepalive_timeout  0;
keepalive_timeout  65;
#gzip  on;
server {
listen       80;
server_name  localhost;
#charset koi8-r;
#access_log  logs/host.access.log  main;
location / {
root   html;
index  index.html index.htm;
}
#error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
#    proxy_pass   http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
#    deny  all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;
#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
}
[root@primary2 conf]# 

/etc/nginx/conf.d/abc.conf

[root@primary2 conf.d]# pwd
/etc/nginx/conf.d
[root@primary2 conf.d]# cat abc.conf
server {
listen 9003;
server_name www.xyz.com;
location / {
proxy_pass http://10.0.9.216:9002;
}
}
[root@primary2 conf.d]# 

错误发生在我身上:

[root@primary2 conf]# /etc/nginx/sbin/nginx -t
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/conf.d/abc.conf:1
nginx: configuration file /etc/nginx/conf/nginx.conf test failed
[root@primary2 conf]# 

在我将/etc/nginx/conf/nginx.conf 修改为以下内容后:

[root@primary2 conf]# pwd
/etc/nginx/conf
[root@primary2 conf]# cat nginx.conf
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;
#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#access_log  logs/access.log  main;
sendfile        on;
#tcp_nopush     on;
#keepalive_timeout  0;
keepalive_timeout  65;
#gzip  on;
server {
listen       80;
server_name  localhost;
#charset koi8-r;
#access_log  logs/host.access.log  main;
location / {
root   html;
index  index.html index.htm;
}
#error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
#    proxy_pass   http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
#    deny  all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;
#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
include /etc/nginx/conf.d/*.conf;
}
[root@primary2 conf]# 

错误消失了。

[root@primary2 conf]# /etc/nginx/sbin/nginx -t
nginx: the configuration file /etc/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/conf/nginx.conf test is successful
[root@primary2 conf]# /etc/nginx/sbin/nginx -s reload
[root@primary2 conf]# 

最新更新