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

一、背景

Debian/Ubuntu/Deepin安装Nginx

  • 通过官方安装文档方式安装nginx,whereis nginx查看目录如下:
  • 配置文件在/etc/nginx下
  • cat /etc/nginx/nginx.conf,发现很多内容,其中有一句include /etc/nginx/config.d/*.conf,说明会引入config.d下面的所有配置文件,为了不影响主配置文件的大量内容,我们新建子配置文件,目的是修改nginx的默认首页,写成自己的页面用于测试
cd /etc/nginx/config.d
sudo touch index.conf
sudo vim index.conf
  • 一顿操作:wq,然后重启 systemctl restart nginx.service,结果报错 :
/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默认的日志目录在/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实在http块内部的,nginx配置的结构是http>server>location
  • 原因知道了,因为我的config.d下子配置文件里从http开始写的,引入主配置文件以后,成了双层http嵌套,所以报错
  • 最终修改为如下结构即可:
server {
    listen 80;

    root /usr/share/nginx/html;
    index index.html;

    location / {
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容