2017-01-20 58 views
14

我已經重新配置nginx的,但我不能讓它使用下面的配置來重新啓動:nginx的:EMERG]「服務器」指令,在這裏不允許使用

的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; 
} 
} 

運行sudo nginx -c conf -t後測試將返回以下的錯誤,我無法弄清楚什麼是真正的問題

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 

回答

28

這不是一個nginx配置文件中的配置。它是nginx配置文件的部分

nginx配置文件(通常稱爲nginx.conf)將看起來像:

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

server塊是http塊內包圍。

配置通常通過使用include指令來引入其他片段(例如,從sites-enabled目錄)分佈到多個文件中。

使用sudo nginx -t測試完整的配置文件,該文件從nginx.conf開始,並使用include指令提取其他片段。有關更多信息,請參見this document

相關問題