nginx:[emerg]"http"指令在/ etc/nginx/sites-enabled/default中不允许:1

Pau*_*aul 22 nginx

我是NGINX的新手,我正在尝试设置最小的工作量.所以我试图用nginx和supervisor运行aiohttp mini-app(通过这个例子).但我无法正确配置Nginx并收到以下错误:

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

这是完整的default.conf文件:

http {
  upstream aiohttp {
    # Unix domain servers
    server unix:/tmp/example_1.sock fail_timeout=0;
    server unix:/tmp/example_2.sock fail_timeout=0;
    server unix:/tmp/example_3.sock fail_timeout=0;
    server unix:/tmp/example_4.sock fail_timeout=0;
  }

  server {
    listen 80;
    client_max_body_size 4G;

    server example.com;

    location / {
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_redirect off;
      proxy_buffering off;
      proxy_pass http://aiohttp;
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

看起来很正确.server指令是http应该的.http是父指令.我做错了什么?

Sha*_* C. 40

我假设你有http/etc/nginx/nginx.conf文件,然后告诉nginxinclude sites-enabled/*;

那么你有

 http
    http
       server
Run Code Online (Sandbox Code Playgroud)

因为http指令应该只发生一次,只需从启用站点的配置文件中删除http指令

  • 你是对的,`/etc/nginx/nginx.conf` 有这一行。我删除了 `http` 指令,现在收到“此处不允许服务器指令”错误。 (3认同)

小智 12

您可以http{}nginx.conf中插入一个应该在section部分内的部分,并在part部分中/etc/nginx/sites-available/default留下server{}.


Mat*_*ava 5

我一直有类似的问题。我需要包含upstream指令,但我无法触及定义指令的/etc/nginx/nginx.conf文件http。我唯一能做的就是替换/etc/nginx/conf.d/default.conf(云/kubernetes 自动化的东西......)

解决方案非常简单,但由于它可能并不明显(对我来说不是),/etc/nginx/conf.d/default.conf如果您需要包含upstreamspec. (upstream并且server指令未包含在此文件中的任何内容中)

/etc/nginx/conf.d/default.conf

upstream api {
    server ...;
}

server {
    listen              80;
    server_name         example.com;
    ...

    location / {
        proxy_pass      http://api;
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)