212

I have reconfigured nginx but I can't get it to restart using the following configuration:

server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 80;
    server_name example.com;

    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    location /robots.txt {
        alias /path/to/robots.txt;
        access_log off;
        log_not_found off;
    }

    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 30;
        proxy_read_timeout 30;
        proxy_pass http://127.0.0.1:8000;
    }

    location /static {
        expires 1M;
        alias  /path/to/staticfiles;
    }
}

After running sudo nginx -c conf -t to test the config, the following error is returned.

nginx: [emerg] "server" directive is not allowed here in    /etc/nginx/sites-available/config:1
nginx: configuration file /etc/nginx/sites-available/config test failed
Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
samix73
  • 2,802
  • 4
  • 17
  • 29

6 Answers6

362

That is not an nginx configuration file. It is part of an nginx configuration file.

The nginx configuration file (usually called nginx.conf) will look like:

events {
    ...
}
http {
    ...
    server {
        ...
    }
}

The server block is enclosed within an http block.

Often the configuration is distributed across multiple files, by using the include directives to pull in additional fragments (for example from the sites-enabled directory).

Use sudo nginx -t to test the complete configuration file, which starts at nginx.conf and pulls in additional fragments using the include directive.

See this document for more information.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • 2
    This anwser is correct and has been rightly upvoted - im trying to clarify further , may help other noobs like me . Have thus answered this question below . – Rohit Dhankar Oct 21 '19 at 05:14
46

Here is an example of a valid nginx.conf file for reverse proxy.

10.x.x.x is the nginx proxy server. 10.y.y.y is the real web server.

events {
    worker_connections  4096;  ## Default: 1024
}
http {
    server {
        listen 80;
        listen [::]:80;

        server_name 10.x.x.x;
 
        location / {
            proxy_pass http://10.y.y.y:80/;
            proxy_set_header Host $host;
        }
    }
}

Here is how to do SSL pass through, if 10.y.y.y is running a HTTPS webserver. 10.x.x.x is listening on port 443, and all traffic to port 443 is directed to your target web server.

events {
    worker_connections  4096;  ## Default: 1024
}

stream {
    server {
        listen     443;
        proxy_pass 10.y.y.y:443;
    }
}

Here is how to serve it in Docker.

docker run --name nginx-container --rm --net=host   -v /home/core/nginx/nginx.conf:/etc/nginx/nginx.conf nginx
Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Alex Punnen
  • 5,287
  • 3
  • 59
  • 71
15

The path to the nginx.conf file which is the primary Configuration file for Nginx - which is also the file which shall INCLUDE the Path for other Nginx Config files as and when required is /etc/nginx/nginx.conf.

You may access and edit this file by typing this at the terminal

cd /etc/nginx

/etc/nginx$ sudo nano nginx.conf

Further in this file you may Include other files - which can have a SERVER directive as an independent SERVER BLOCK - which need not be within the HTTP or HTTPS blocks, as is clarified in the accepted answer above.

I repeat - if you need a SERVER BLOCK to be defined within the PRIMARY Config file itself than that SERVER BLOCK will have to be defined within an enclosing HTTP or HTTPS block in the /etc/nginx/nginx.conf file which is the primary Configuration file for Nginx.

Also note -its OK if you define , a SERVER BLOCK directly not enclosing it within a HTTP or HTTPS block , in a file located at path /etc/nginx/conf.d . Also to make this work you will need to include the path of this file in the PRIMARY Config file as seen below :-

http{
    include /etc/nginx/conf.d/*.conf; #includes all files of file type.conf
}

Further to this you may comment out from the PRIMARY Config file , the line

http{
    #include /etc/nginx/sites-available/some_file.conf; # Comment Out 
    include /etc/nginx/conf.d/*.conf; #includes all files of file type.conf
}

and need not keep any Config Files in /etc/nginx/sites-available/ and also no need to SYMBOLIC Link them to /etc/nginx/sites-enabled/ , kindly note this works for me - in case anyone think it doesnt for them or this kind of config is illegal etc etc , pls do leave a comment so that i may correct myself - thanks .

EDIT :- According to the latest version of the Official Nginx CookBook , we need not create any Configs within - /etc/nginx/sites-enabled/ , this was the older practice and is DEPRECIATED now .

Thus No need for the INCLUDE DIRECTIVE include /etc/nginx/sites-available/some_file.conf; .

Quote from Nginx CookBook page - 5 .

"In some package repositories, this folder is named sites-enabled, and configuration files are linked from a folder named site-available; this convention is depre‐ cated."

Rohit Dhankar
  • 1,574
  • 18
  • 25
9

There might be a typo inside a file imported by the config.

I had this typo deep inside my config file:

loccation /sense/movies/ {
    mp4;
}

loccation instead of location, and this causes the error:

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/xxx.xx:1
Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
bojan
  • 870
  • 9
  • 7
0

Replace this:

include /etc/nginx/conf.d/*.conf;

In your nginx.conf with this:

include /etc/nginx/conf.d/includes-optional/cpanel-proxy-vendors/*.conf;

Or this:

/etc/nginx/conf.d/includes-optional/site-available/*.conf;
Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
0

The same error happened for me, followings are my configurations: OS and nginx version:

[root@primary2 ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[root@primary2 ~]# uname -rm
3.10.0-693.el7.x86_64 x86_64
[root@primary2 ~]# /etc/nginx/sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --with-http_ssl_module
[root@primary2 ~]# 

NGINX configure files:

/etc/nging/conf/nginx.conf

[root@primary2 conf]# pwd
/etc/nginx/conf
[root@primary2 conf]# cat nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

include /etc/nginx/conf.d/*.conf;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
[root@primary2 conf]# 

/etc/nginx/conf.d/abc.conf

[root@primary2 conf.d]# pwd
/etc/nginx/conf.d
[root@primary2 conf.d]# cat abc.conf
server {
       listen 9003;
       server_name www.xyz.com;

      location / {
        proxy_pass http://10.0.9.216:9002;
       }
}
[root@primary2 conf.d]# 

The error happened for me:

[root@primary2 conf]# /etc/nginx/sbin/nginx -t
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/conf.d/abc.conf:1
nginx: configuration file /etc/nginx/conf/nginx.conf test failed
[root@primary2 conf]# 

After I modified the /etc/nginx/conf/nginx.conf to the following:

[root@primary2 conf]# pwd
/etc/nginx/conf
[root@primary2 conf]# cat nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;



events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
include /etc/nginx/conf.d/*.conf;
}
[root@primary2 conf]# 

the ERROR gone.

[root@primary2 conf]# /etc/nginx/sbin/nginx -t
nginx: the configuration file /etc/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/conf/nginx.conf test is successful
[root@primary2 conf]# /etc/nginx/sbin/nginx -s reload
[root@primary2 conf]# 
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jun 15 '23 at 01:16