Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"http" directive is not allowed here Nginx

Tags:

nginx

lua

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 ?

like image 294
user9099802 Avatar asked Dec 05 '25 13:12

user9099802


1 Answers

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

like image 137
Tarun Lalwani Avatar answered Dec 08 '25 06:12

Tarun Lalwani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!