docker启动nginx无法访问的问题解决
作者:Dedete_
本文主要介绍了docker启动nginx无法访问的问题解决,主要遇到404错误,问题原因是配置文件路径错误和权限问题,下面就来具体介绍一下问题解决,感兴趣的可以了解一下
项目场景:
项目相关背景:在学习黑马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
取消include /etc/nginx/conf.d/*.conf;注释(启用默认配置),在默认配置nx_conf/nginx.conf的default.conf文件中编辑添加自己的访问路径 - 解决方案2
location指令root html改成root /usr/share/nginx/html; - 解决方案3(推荐)
使用docker run命令挂载数据卷时使用-v nx_conf:/etc/nginx/conf.d挂载默认文件目录
总结
本人对nginx文件目录不熟悉导致的404错误,记录此次遇到问题,找出原因,解决方案的步骤以加强本人对此问题的印象。
到此这篇关于docker启动nginx无法访问的问题解决的文章就介绍到这了,更多相关docker启动nginx无法访问内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
