user9099802
user9099802

Reputation: 253

"http" directive is not allowed here Nginx

I start and I want to connect to my database with Nginx. Here is my sites-enables / default file:

http {
  upstream database {
   postgres_server 192.168.0.166 dbname=stardb 
                                user=username password=yourpass;
  }

  server {
    listen 80 default_server;
    listen [::]:80 default_server;

    location /ip {
      postgres_pass     database;

      postgres_query    HEAD GET  "SELECT http_host as ip FROM establishment_view where aet = 'NXS_DEV_FLO2'";
    }
  }
}

I get this error:

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

I saw that this error was common but even looking at the other posts I can not solve my problem. And I have this line in my nginx.conf file :

include /etc/nginx/sites-enabled/*;

Did I need to remove the line http in my sites_enables/default file ?

Upvotes: 3

Views: 5749

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

When you do include it is as good as pasting the content from the matching files. So if my main nginx.conf is

http {
   line 1
   line 2;
   include sites-enabled/*.conf;
}

And sites-enabled/default.conf is below

http {
   server { 
      line 3;
   }
}

The effective config becomes

http {
   line 1;
   line 2;
   http {
      server {
          line 3; 
      }
   }
}

Now http directive is not allowed inside any other directive, you can use it at root level. So the error that "http" directive is not allowed here. You should run nginx -T to see full combine config that it uses.

The fix is simple, remove the http enclosing block from default file, as you already are inside a http block

Upvotes: 6

Related Questions