nginx server directive is not allowed here

gideon asked:

I know there are dupes of this out there, but I can’t seem to solve this in my situation.

I’m following an article on setting up nginx as reverse proxy with apache.

And I get this error:

nginx: [emerg] "server" directive is not allowed here in 
nginx: configuration file /etc/nginx/nginx.conf test failed

My /etc/nginx/nginx.conf looks like this:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream; 
    access_log  /var/log/nginx/access.log  main;  
    charset   utf-8;
    keepalive_timeout  65;
    server_tokens       off;
    tcp_nopush          on;
    tcp_nodelay         off;

    server {
          listen 80;
          server_name  _;          
          root   /usr/share/nginx/html;
          index  index.html index.htm;     
include  v.hosts/*.conf;

And this I have /etc/nginx/v.hosts/ looks like this:

server {
      listen 80;

       access_log  off;
       error_log off;

      location / {
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        Host            $host;
        proxy_redirect          off;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;

Clues and help will be well appreciated 🙂

My answer:

The problem is here:

include  v.hosts/*.conf;

You have closed the http block before the include directive, thus ending the configuration. This is why none of the included files work.

To fix the issue, include the files within the http block:

    include  v.hosts/*.conf;

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.