突然,Nginx 在配置文件的第一行显示“[emerg]“server”指令不允许”

突然,Nginx 在配置文件的第一行显示“[emerg]“server”指令不允许”

我对服务器管理还很陌生,在尝试修复无法在 Debian 远程机器上运行的 nginx conf 文件时,我感到非常头疼。每当我尝试调试文件时nginx -c sites-enabled/estadaodados.com -t,我都会得到以下答案:

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

这是我的 nginx.conf 文件:

user www-data;
worker_processes 2;
pid /var/run/nginx.pid;

events {
  worker_connections 768;
  multi_accept on;
  use epoll;
}

http {    
  set_real_ip_from 127.0.0.1;
  real_ip_header X-Forwarded-For;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 20;
  client_max_body_size 15m;
  client_body_timeout 60;
  client_header_timeout 60;
  client_body_buffer_size  1K;
  client_header_buffer_size 1k;
  large_client_header_buffers 4 8k;
  send_timeout 60;
  reset_timedout_connection on;
  types_hash_max_size 2048;
  server_tokens off;
  server_names_hash_bucket_size 64;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
  log_format main '$remote_addr - $remote_user [$time_local] '
  '"$request" $status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';

  gzip on;
  gzip_static on;
  gzip_disable "MSIE [1-6].*(MSIE?.*SV1)";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_min_length 512;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;

  # Virtual Host Configs
  include /etc/nginx/sites-enabled/*;
}

这是我的 conf 文件,位于 sites-enabled 文件夹中(我刚刚删除了所有旧的和广泛的配置,但即使这一小块代码仍然不起作用):

server {                                                                                                                                                                                                                                                                                                                                                     
  server_name estadaodados.com;
  listen *:80;
  root /home/edados/www/main;
  port_in_redirect off;
  server_tokens off;
  autoindex off;
  index index.html index.htm index.php;
}

我遗漏了什么?

答案1

在调试 nginx 配置时,您需要提供以blocknginx.conf开头的主文件的路径http。因为您直接提供了 vhost 配置,所以nginx -t它会抛出错误。

你应该做nginx -c /path/to/nginx.conf -t

相关内容