Nginx – Can’t restart nginx server with 2nd sites-available conf

nginx

I had one nginx configuration set up and working fine in /etc/nginx/sites-available/default-staging. Today I've tried to add another for the sendy email marketing app based on this configuration.

After running the service nginx restart command failed I checked online and found that nginx -c /etc/nginx/sites-available/sendy-newsletter -t should highlight any sytax errors, it gave the following result.

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-available/sendy-newsletter:1
nginx: configuration file /etc/nginx/sites-available/sendy-newsletter test failed

Two things confused me about this, first off I don't understand why the server block would not be allowed, after checking online it seems that this can be caused if sites-enabled config files are not included in the hhtp block of my nginx.conf, but they are:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 2048;
    multi_accept on;
        use epoll;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

Secondly when I removed the sendy-newsletter configuration from sites-available and sites-enabled folders, as expected I could restart the nginx server successfully but strangely when I ran nginx -c /etc/nginx/sites-available/default-staging -t I get the same error message as before but the server works!?

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-available/default-staging:1
nginx: configuration file /etc/nginx/sites-available/default-staging test failed

Here is the full configuration I have in /etc/nginx/sites-available/sendy-newsletter, it is sym linked and the same in sites-enabled

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

    server_name newsletter.example.com;

    autoindex off;
    index index.php index.html;

    root /var/www/sendy-newsletter/public;
#    access_log /var/www/sendy-newsletter/logs/access.log;
#    error_log /var/www/sendy-newsletter/error.log;

    # Don't allow search engines to index any 
    add_header X-Robots-Tag "noindex, noarchive";

    location / {
        try_files $uri $uri/ $uri.php?$args;
    }

    location /l/ {
        rewrite ^/l/([a-zA-Z0-9/]+)$ /l.php?i=$1 last;
    }

    location /t/ {
        rewrite ^/t/([a-zA-Z0-9/]+)$ /t.php?i=$1 last;
    }

    location /w/ {
        rewrite ^/w/([a-zA-Z0-9/]+)$ /w.php?i=$1 last;
    }

    location /unsubscribe/ {
        rewrite ^/unsubscribe/(.*)$ /unsubscribe.php?i=$1 last;
    }

    location /subscribe/ {
        rewrite ^/subscribe/(.*)$ /subscribe.php?i=$1 last;
    }

    location ~ \.php$ {
        # --PHP5-FPM CONFIG START (keep fastcgi_param HTTPS OFF)--
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        ##fastcgi_param HTTPS $fastcgi_https;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        # --PHP5-FPM CONFIG START--

        # --HHVM CONFIG START--
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #include        fastcgi_params;
        try_files $uri $uri/ @handler; 
        # --HHVM CONFIG END--

        include fastcgi_params;
    }

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
        access_log off;
        log_not_found off;
        expires 30d;
    }
}

UPDATE

I checked my nginx error logs and I'm getting this message after I try to restart nginx.

2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2015/11/18 13:09:44 [emerg] 21899#0: still could not bind()

Also this is the result of running:

$ sudo nginx -c /etc/nginx/nginx.conf -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Also I tried running the syntax test after removing the configuration file I am not using

$ sudo rm default-staging
$ sudo rm ../sites-enabled/default-staging
$ sudo nginx -c /etc/nginx/sites-available/sendy-newsletter -t
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-available/sendy:1
nginx: configuration file /etc/nginx/sites-available/sendy test failed

Best Answer

Of course you're getting an error :) nginx -c is supposed to check your whole web server config, not just the server block. Just run it on the main conf file and you'll see. It is automatically taking into account any linked server directives.

That's why you should delete the (link to the) directive you don't use from the sites-enabled directory before making the check. You can always link it back.