零、步骤
- 获取nginx镜像
- 创建nginx.conf,修改配置
- 覆盖nginx的默认配置,运行nginx
在进行尝试的时候,思路不是很清晰,中间搞混了好几次,主要是nginx服务监听的端口,和docker的端口搞混了,还有进行文件路径映射的时候,搞不太清楚nginx.conf中的root目录和docker运行时的-v操作,网上都叫做挂载,而我更喜欢称为映射
一、获取nginx镜像
docker pull nginx
二、创建nginx.conf,修改配置
我的目录
/home
/nginx
startNginx.sh
stopNginx.sh
nginx.conf
default.conf
/data
index.html
/images
/another.jpg
/audios
/透明.mp3
可以在windows上编辑好上传到服务器,再进行修改,nginx.conf
配置如下
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# 表示具体的配置位置,相当于将配置分散,不同的serve在不同的conf文件中
# 默认引入/etc/nginx/conf.d/ 目录下的所有.conf结尾的配置文件,默认有default.conf文件
include /etc/nginx/conf.d/*.conf;
}
default.conf
文件配置
此处虽然端口为8089,但不是客户端浏览器的访问端口,而是docker中nginx容器的内部端口,客户端浏览器访问应使用docker -p 80:8089 进行映射之后的80端口
server {
# nginx服务端口
listen 8089;
# 域名,IP都可以
server_name www.another.ren;
# 编码
charset utf8;
# 如果不使用docker的情况下
# 浏览器访问
# www.another.ren:8089
# 访问返回资源
# www.another.ren:8089/home/data/index.html
location / {
root /home/data;
# 默认访问
index index.html;
}
# 如果不使用docker的情况下
# 浏览器访问
# www.another.ren:8089/images/pictures
# 访问返回资源
# www.another.ren:8089/home/data/images/pictures
location /images {
root /home/data;
}
# 如果不使用docker的情况下
# 浏览器访问
# www.another.ren:8089/audios/透明.mp3
# 访问返回资源
# www.another.ren:8089/home/data/audios/透明.mp3
location /audios {
root /home/data;
}
}
三、覆盖nginx的默认配置,运行nginx
运行nginx的命令startNginx.sh
,写到了脚本中,不用每次都这么麻烦地输入这么多东西,sh startNginx.sh
执行
docker run --name another-nginx -d -p 80:8089 -v /home/nginx/log:/var/log/nginx -v /home/nginx/data:/home/data:ro -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -v /home/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro nginx
- –name 为nginx起别名
- -p 端口映射,80表示宿主机端口,8089表示docker容器的内部端口
- -v 文件映射,-v /home/nginx/nginx.conf:/etc/nginx/nginx.conf 表示 宿主机中的/home/nginx/nginx.conf 映射覆盖nginx容器中的/etc/nginx/nginx.conf 的配置
停止nginx的命令stopNginx.sh
,执行脚本sh stopNginx.sh
# 停止容器,another-nginx为运行时起得别名
docker stop another-nginx
# 移除容器
docker rm another-nginx
# 列出所有容器
docker ps -a
ps
-
目录映射
nginx 版本为1.17.8,其在docker容器中的配置文件的位置信息如下
docker中nginx容器中的目录与宿主机的目录不是一回事
docker中nginx容器默认的root目录为
/usr/share/nginx/html
docker中nginx容器默认的日志目录为
/var/log/nginx
docker中nginx容器默认的根配置目录为
/etc/nginx/nginx.conf
docker中nginx容器默认的子配置目录为
/etc/nginx/conf.d/default.conf
在运行docker的nginx容器时,可以使用-v命令来进行映射
-v 宿主机目录:ningx容器中的目录
-v 宿主机文件:nginx容器中的文件
-
端口映射
-
首先客户端浏览器中访问的端口与服务器(宿主机)保持一致
-
服务器(宿主机)端口与docker之间的映射,-p 80:8089 即服务器使用80端口,docker中的nginx容器内部使用8089端口
-
docker中nginx容器的端口在nginx.conf中进行配置,我配置的为8089,与-p 80:8089 中的后者保持一致
nginx.conf中配置nginx容器端口 —> docker容器的端口映射 —> 浏览器与服务器端口一致
-
-
nginx资源访问路径问题
其实我感觉尝试的过程中,应该有已经配置成功了,但是访问路径错误,一直404的情况!!!
排除端口的问题之后,nginx的路径问题来了
location中的root配置,并不是我所理解的根目录那样,在访问www.another.ren/时,确实是访问到www.another.ren/home/data/index.html,但不是我所理解的那般,
/
替换为/home/data/
,而是在访问/home/data/
时,在/
前拼接上/home/data
,例如location /images { root /home/data; }
此时访问路径中匹配到
/images
时,则会在前面拼接上/home/data
,即/home/data/images/
客户端浏览器访问http://www.another.ren
返回服务器的
/home/data/index.html
访问http://www.another.ren/images/another.jpg
返回服务器的
/home/data/images/another.jpg
访问 http://www.another.ren/audios/透明.mp3
返回服务器的
/home/data/audios/透明.mp3
-
参考