如何解决 /etc/nginx/sites-enabled/default:1 中不允许使用“http”指令

Clo*_*ner 8 nginx aiohttp

我遵循此步骤,但不知何故这会引发错误..

默认配置是

http {
  upstream alert {
    # fail_timeout=0 means we always retry an upstream even if it failed
    # to return a good HTTP response

    # Unix domain servers
    server unix:/tmp/alert_1.sock fail_timeout=0;
    server unix:/tmp/alert_2.sock fail_timeout=0;
    server unix:/tmp/alert_3.sock fail_timeout=0;
    server unix:/tmp/alert_4.sock fail_timeout=0;

    # Unix domain sockets are used in this example due to their high performance,
    # but TCP/IP sockets could be used instead:
    # server 127.0.0.1:8081 fail_timeout=0;
    # server 127.0.0.1:8082 fail_timeout=0;
    # server 127.0.0.1:8083 fail_timeout=0;
    # server 127.0.0.1:8084 fail_timeout=0;
  }
  server {
    listen 80;
    client_max_body_size 4G;

    server_name myiphere;

    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://alert;
    }

    location /static {
      # path for static files
      root /mylocation/static;
    }

  }
}
Run Code Online (Sandbox Code Playgroud)

nginx: [emerg] /etc/nginx/sites-enabled/default:1 中不允许使用“http”指令 nginx: 配置文件 /etc/nginx/nginx.conf 测试失败

And*_*lin 6

检查你的/etc/nginx/nginx.conf。如果您在那里打开http指令,然后再次将其打开到sites-enabled/文件夹conf文件中,您最终将得到嵌套http指令。


小智 6

我的回复晚了大约9个月,但我刚刚遇到了同样的问题。

我决定添加我的回复,因为最后两个答案仅暗示了解决方案,并且可能仍然会让某些人感到困惑。

更直接的答案是:

从上面的配置中删除 http 声明,因为它很可能已在您的/etc/nginx/nginx.conf文件中定义(您可以检查该文件以验证声明)。

所以,你的配置最终应该是这样的:


    upstream alert {
        # fail_timeout=0 means we always retry an upstream even if it failed
        # to return a good HTTP response
    
        # Unix domain servers
        server unix:/tmp/alert_1.sock fail_timeout=0;
        server unix:/tmp/alert_2.sock fail_timeout=0;
        server unix:/tmp/alert_3.sock fail_timeout=0;
        server unix:/tmp/alert_4.sock fail_timeout=0;
    
        # Unix domain sockets are used in this example due to their high performance,
        # but TCP/IP sockets could be used instead:
        # server 127.0.0.1:8081 fail_timeout=0;
        # server 127.0.0.1:8082 fail_timeout=0;
        # server 127.0.0.1:8083 fail_timeout=0;
        # server 127.0.0.1:8084 fail_timeout=0;
      }
      server {
        listen 80;
        client_max_body_size 4G;
    
        server_name myiphere;
    
        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://alert;
        }
    
        location /static {
          # path for static files
          root /mylocation/static;
        }
    
      }

Run Code Online (Sandbox Code Playgroud)