docker启动nginx无法访问

项目场景:

项目相关背景:
在学习黑马springcloud课程的多级缓存时,使用docker启动nginx容器


问题描述

项目中遇到的问题:
依此使用下列命令部署nginx容器

docker pull nginx
docker volume create nx_conf #新建nginx的nginx.conf配置挂载的数据卷
docker volume create nx_html #新建ngxin的html静态资源挂载的数据卷
docker run --name nx \
-p xxxx:80 \
-v nx_conf:/etc/nginx \
-v nx_html:/usr/share/nginx/html \
-d nginx

使用docker volume inspect nx_conf查看数据卷位置

nx_conf数据卷目录下nginx.conf文件如下


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

老师发的nginx通用配置文件nginx.conf如下

#user  nobody;
worker_processes  1;
error_log  logs/error.log;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

因为配置了其他访问路径,我把include /etc/nginx/conf.d/*.conf;注释掉,并把老师配置文件中location指令中关于/=/50x.html命令复制到自己的配置文件中
访问结果如下图所示:
在这里插入图片描述


原因分析:

问题的分析:

结果表示nginx服务已经开启,但出现404错误,404错误是请求的网页不存在,查看路径是否配置正确


解决方案:

具体解决方案:

  1. 解决方案1
    取消include /etc/nginx/conf.d/*.conf;注释(启用默认配置),在默认配置nx_conf/nginx.conf的default.conf文件中编辑添加自己的访问路径
  2. 解决方案2
    location指令root html改成root /usr/share/nginx/html;
  3. 解决方案3(推荐)
    使用docker run命令挂载数据卷时使用-v nx_conf:/etc/nginx/conf.d挂载默认文件目录

总结

本人对nginx文件目录不熟悉导致的404错误,记录此次遇到问题,找出原因,解决方案的步骤以加强本人对此问题的印象。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值