nginx容器同时提供配置文件和网页文件

本文详细介绍了如何创建Nginx容器,并配置虚拟主机以托管静态和动态内容。步骤包括创建容器、配置nginx.conf、映射宿主机目录到容器内,以及演示了如何通过不同端口访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建nginx容器

需要同时提供配置文件和网页文件

[root@localhost ~]# docker run -d --name nginx 1225514226/nginx:v2.0 
bf7d4417b6d858520dad5f5d13498ae7cbdf5c071614189c7ce05bcb9f8e5976
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED         STATUS         PORTS     NAMES
bf7d4417b6d8   1225514226/nginx:v2.0   "/usr/local/nginx/sb…"   3 seconds ago   Up 2 seconds             nginx
[root@localhost ~]# docker cp  nginx:/usr/local/nginx/conf .
[root@localhost ~]# docker cp nginx:/usr/local/nginx/html .
[root@localhost ~]# ls
anaconda-ks.cfg  apache  conf  html
[root@localhost ~]# mkdir config
[root@localhost ~]# mv conf html config/
[root@localhost ~]# cd config/
[root@localhost config]# ls
conf  html
[root@localhost config]# ls conf/
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@localhost config]# mkdir html/test
[root@localhost config]# echo "hello world" > html/test/index.html
[root@localhost config]# ls html/
images  index.html  jquery.js  QAuIByrkL.js  test  VNkyVaVxUV.css  wNGu2CtEMx.js
[root@localhost config]#

nginx配置文件配置虚拟主机

[root@localhost config]# vim conf/nginx.conf
·····省略部分······
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
  
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
                }
        }

    server {
        listen      8080;
        server_name test.example.com;

        location / {
            root   html/test;
            index  index.html index.htm;
                }
        }
····省略部分····

删除刚刚创建的容器

[root@localhost config]# docker rm -f nginx
nginx

##删除后文件还在
[root@localhost config]# ls
conf  html
[root@localhost config]# ls conf/
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@localhost config]# ls html/
images  index.html  jquery.js  QAuIByrkL.js  test  VNkyVaVxUV.css  wNGu2CtEMx.js
[root@localhost config]# 

运行一个容器,将宿主机的配置目录文件映射nginx的相应位置

[root@localhost ~]# docker run -itd --name web -v /root/config/html:/usr/local/nginx/html80:80 -p 8080:8080 1225514226/nginx:v2.0 
a903506316dfc786268aedc9c88867bb32a6945d98fcdf277fff352b3be6d20a
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED         STATUS                                         NAMES
a903506316df   1225514226/nginx:v2.0   "/usr/local/nginx/sb…"   2 seconds ago   Up 2 seconds  0->8080/tcp, :::8080->8080/tcp   web

不同端口在浏览器进行访问

192.168.10.203:80
在这里插入图片描述

192.168.10.203:8080
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值