一、背景
Debian/Ubuntu/Deepin安裝Nginx
- 通過官方安裝文檔方式安裝nginx,whereis nginx查看目錄如下:
- 配置文件在/etc/nginx下
- cat /etc/nginx/nginx.conf,發(fā)現(xiàn)很多內(nèi)容,其中有一句
include /etc/nginx/config.d/*.conf,說明會(huì)引入config.d下面的所有配置文件,為了不影響主配置文件的大量?jī)?nèi)容,我們新建子配置文件,目的是修改nginx的默認(rèn)首頁,寫成自己的頁面用于測(cè)試
cd /etc/nginx/config.d
sudo touch index.conf
sudo vim index.conf
- 一頓操作:wq,然后重啟 systemctl restart nginx.service,結(jié)果報(bào)錯(cuò) :
/etc/nginx/conf.d$ systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
-
journalctl -xe并沒有看到有效信息,nginx默認(rèn)的日志目錄在/var/log/nginx下,sudo cat error.log可以看到"http" directive is not allowed here in /etc/nginx/conf.d/index.conf:1
二、解決
- 回憶之前的主conf文件,
include /etc/nginx/config.d/*.conf實(shí)在http塊內(nèi)部的,nginx配置的結(jié)構(gòu)是http>server>location - 原因知道了,因?yàn)槲业腸onfig.d下子配置文件里從http開始寫的,引入主配置文件以后,成了雙層http嵌套,所以報(bào)錯(cuò)
- 最終修改為如下結(jié)構(gòu)即可:
server {
listen 80;
root /usr/share/nginx/html;
index index.html;
location / {
}
}
