nginx配置:"http" directive is not allowed here in

一、背景

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 / {
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容