在自己的安装过程中挂载,主要出现的问题,访问端口出现当前无法使用此页面或者403的情况
起初跟着网上的教程,发现访问都会或多或少出现问题,在最后解决了。以下基础命令都没有写出来,适合有经验的小伙伴作为参考。
1、下载镜像
docker pull nginx:1/17.8
2、安装临时的nginxtest容器
docker run -d --name nginxtest -p 81:80 nginx:1.17.8
3、创建所需要的挂载文件夹,我创建在/home/xx/nginx下,xx:表示的我的电脑名称。存放位置可以自己决定。没有使用mkdir -p ./nginx/{www,logs,conf} 方式创建,默认会成为root用户创建,避免其他权限,我直接手动一个个创建。cd /home/xx/
mkdir logs #日志文件夹
mkdir www #页面文件夹
mkdir conf #配置文件夹
4、将nginxtest容器的配置文件复制到自己的创建的文件夹中,先获取容器ID
docker cp 13246fab6580:/etc/nginx/nginx.conf /home/xxx/nginx/conf
5、关闭nginxtest容器后,创建80端口的nginx
docker run -d -p 80:80 --name nginx -v /home/xx/nginx/www:/usr/share/nginx/html -v /home/xx/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/xx/nginx/logs:/var/log/nginx -v /home/xx/nginx/conf.d:/etc/nginx/conf.d nginx:1.17.8
6、在没有错的情况下访问localhost:80 会出现我前面提到的情况,接下来是重点打开自己挂载的下的nginx.conf文件,配置
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
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;
# 以下这部分是后续加上的,映射的80端口 ***************
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#这里如果不配置,出现403
root /usr/share/nginx/html;
index index.html index.htm;
}
}
include /etc/nginx/conf.d/*.conf;
}
7、上面标注的出现403的现象是root对应的路径不对,应该是指向/usr/share/nginx/html的,这个路径,大家可以查看创建nginx容器中命令所挂载的地址就可以发现。
-v /home/nginx/html:/usr/share/nginx/html
8、在/home/xx/www下创建index.html
<!DOCTYPE html>
<html>
<head>
<title>网页标题</title>
</head>
<body>
<h1>Hello Nginx.......</h1>
</body>
</html>
9、访问页面