nginx: [emerg] "http" directive is not allowed here in /etc/nginx/sites-enabled/default:1

Nginx

Nginx Problem Overview


I'm new to NGINX and I'm trying to setup minimal working thing. So I trying to run aiohttp mini-app with nginx and supervisor (by [this][1] example). But I can't configure Nginx right and getting the following error:

nginx: [emerg] "http" directive is not allowed here in /etc/nginx/sites-enabled/default:1

Here is full default.conf file:

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;
    }
  }

}

It looks correct. server directive is in http as it should be. And http is parent directive. What I'm doing wrong? [1]: http://aiohttp.readthedocs.io/en/stable/deployment.html

Nginx Solutions


Solution 1 - Nginx

I am assuming that you have http in your /etc/nginx/nginx.conf file which then tells nginx to include sites-enabled/*;

So then you have

 http
    http
       server

As the http directive should only happen once just remove the http directive from your sites-enabled config file(s)

Solution 2 - Nginx

You may insert a part which should be inside http{} section into your nginx.conf and in /etc/nginx/sites-available/default leave just server{} section.

Solution 3 - Nginx

I've been having similar issue. I needed to include upstream directive but I could't touch the /etc/nginx/nginx.conf file where the http directive is defined. The only thing I could do was to replace /etc/nginx/conf.d/default.conf (cloud/kubernetes automation stuff...)

The solution is quite simple but since it might not be obvious (wasn't to me), here is how you can write your /etc/nginx/conf.d/default.conf file if you need to include upstream spec. (upstream and server directives are not enclosed by anything in this file)

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

upstream api {
    server ...;
}

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

    location / {
        proxy_pass      http://api;
        ...
    }
}

Solution 4 - Nginx

So, actually the problem was in the second server keyword. I used an example from aiohttp docs, and looks like they mistyped with "server example.com" instead of server_name example.com.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaulView Question on Stackoverflow
Solution 1 - NginxShawn C.View Answer on Stackoverflow
Solution 2 - NginxLeonid UsachovView Answer on Stackoverflow
Solution 3 - NginxMatus DubravaView Answer on Stackoverflow
Solution 4 - NginxPaulView Answer on Stackoverflow