Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

nginx: [emerg] "server" directive is not allowed here

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

conf:

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 configuration the following error is returned i can't figure out what is really the problem

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...