why does nginx fail to start?

2019-09-16 01:32发布

问题:

Yet starting nginx fails and my log shows:

[emerg] 55#55: "server" directive is not allowed here in /etc/nginx/nginx.conf:1

What am I doing wrong? Not sure if it matters but this is inside a docker container.

回答1:

/etc/nginx/nginx.conf is not the place to store your server setups. This file should contain information on the user it will run under and other global settings. Typically it will contain stuff like this:

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

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;
    client_max_body_size 500M;

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

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

    gzip on;
    gzip_disable "msie6";

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

Where you should put your individual server setups, is in /etc/nginx/sites-enabled/. As you can see, all files inside that folder are being included in the http block of your nginx.conf file.

So store the sample config you have posted inside /etc/nginx/sites-enabled/some-random-name.conf, and restore the nginx.conf file to the config I posted above, for instance, and you should be good to go.



回答2:

Try debugging first with

sudo nginx -t

If this passes but nginx restart fails, try checking your nginx logs at narrow down the bug

/var/log/nginx/error.log


标签: nginx