拉取最新的nginx镜像
docker pull nginx
创建宿主机上的映射文件目录,并授权
[root@1-0001 ~]# mkdir -p /opt/nginx/html
[root@1-0001 ~]# mkdir -p /opt/nginx/conf.d
[root@1-0001 ~]# mkdir -p /opt/nginx/logs
[root@1-0001 ~]# chmod -R +777 /opt/nginx
启动一个不挂载的容器
-d 后台启动
docker run -d --name nginx -p 80:80 nginx
从容器中将配置文件复制到宿主机中
[root@1-0001 ~]# docker cp nginx:/etc/nginx/nginx.conf /opt/nginx/nginx.conf
[root@1-0001 ~]# docker cp nginx:/etc/nginx/conf.d /opt/nginx/conf.d/
[root@1-0001 ~]# docker cp nginx:/usr/share/nginx/html /opt/nginx/html/
删除未挂载容器
[root@1-0001 ~]# docker stop nginx
[root@1-0001 ~]# docker rm nginx
# 直接删除正在运行的容器
[root@1-0001 ~]# docker rm -f nginx
启动挂载容器
[root@1-0001 ~]# docker run --name nginx -p 8400:8400 -v /opt/nginx/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/conf.d:/etc/nginx/conf.d -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/html:/usr/share/nginx/html -d nginx
文件配置
将前端项目dist和dist1分别放到/opt/nginx/html目录下
[root@1-0002 html]# ls
50x.html dist dist1 dist1.zip dist.zip index.html
[root@1-0002 html]# pwd
/opt/nginx1/html
修改后的配置文件/opt/nginx/conf.d/default.conf,如下
server {
listen 8400;
listen [::]:8400;
server_name localhost;
root /usr/share/nginx/html;
#access_log /var/log/nginx/host.access.log main;
location /a {
alias /usr/share/nginx/html/dist;
index index.html index.htm;
try_files $uri $uri/ /dist/index.html;
}
location /cfd {
alias /usr/share/nginx/html/dist1;
index index.html index.htm;
try_files $uri $uri/ /dist1/index.html;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
重启容器
[root@1-0002 html]# docker restart nginx