Cloud Learner
Cloud Learner

Reputation: 141

How to solve "http" directive is not allowed here in /etc/nginx/sites-enabled/default:1

I follow this step but somehow this throws error..

default config is

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

  }
}

nginx: [emerg] "http" directive is not allowed here in /etc/nginx/sites-enabled/default:1 nginx: configuration file /etc/nginx/nginx.conf test failed

Upvotes: 9

Views: 16554

Answers (4)

UnMoscerinoNelWeb
UnMoscerinoNelWeb

Reputation: 39

Normally nginx.conf (in linux is under /etc/nginx/nginx.conf) contains an inclusion of configuration file within the "http" declaration.

The end of nginx.conf http configuration

Since it refer to .conf file contained into your conf.d directory (check out the screenshot), you can add a file ending with .conf extension and named as you wish, for example: ASyouWISH.conf.

In my case I added there my websocket configuration without "http" declaration, since it is included into my .conf file.

Upvotes: 1

Alli
Alli

Reputation: 86

My response is about 9months late, but I have just experienced the same issue.

I decided to add my response because the last 2 answers only hinted at the solution and might still be confusing for some people.

A more straightforward answer is:

Remove the http declaration from your config above, since it most likely has been defined in your /etc/nginx/nginx.conf file (You can check that file to verify the claim).

So, your config should eventually look like this:


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

Upvotes: 6

gotouei
gotouei

Reputation: 1

I had the same error with you met.I set a nginx config not directly in /ect/nginx/nginx.conf but in http{} in /ect/nginx/nginx.conf. So you can remove the /ect/nginx/nginx.conf and create a new /ect/nginx/nginx.conf with your own config code.

Upvotes: 0

Andrea Golin
Andrea Golin

Reputation: 3559

Check your /etc/nginx/nginx.conf. If you are opening http directive there, and then opening it again into your sites-enabled/ folder conf file you will end up with nested http directive.

Upvotes: 6

Related Questions