docker安装nginx

本文介绍了如何使用Docker拉取Nginx镜像,创建挂载目录,启动和配置容器。遇到无法访问问题时,检查了防火墙设置,并解决了配置文件路径错误的问题。通过配置location和root,实现了不同URL对应不同的HTML目录。

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

1、拉取镜像

docker pull nginx:1.23.0

2、创建挂载目录

mkdir -p /mnt/nginx/{conf.d,logs,html,conf}

3、创建容器(用于拷贝相关文件到挂载目录,看第四步。这个弄完第四步可以删掉)

docker run --name nginx8081 -p 8081:80 -d nginx:1.23.0

4、拷贝文件到挂载目录(因为这样做,就不用进到容器里去修改nginx相关文件了)

docker cp nginx8081:/etc/nginx/conf.d /mnt/nginx/conf.d/
docker cp nginx8081:/var/log/nginx/ /mnt/nginx/logs/
docker cp nginx8081:/usr/share/nginx/html/ /mnt/nginx/html/
docker cp nginx8081:/etc/nginx/nginx.conf /mnt/nginx/conf

5、启动新容器(建议往下拉看须知 ~ 其他操作第2步再决定执行的命令

docker run  --name nginx  -p 8088:80 -v /mnt/nginx/conf.d:/etc/nginx/conf.d -v /mnt/nginx/logs:/var/log/nginx -v /mnt/nginx/html:/usr/share/nginx/html -v /mnt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  --privileged=true -d 你的镜像id

6、启动成功后,通过http://192.168.226.128:8088/,访问不到我nginx,无语

1)先确保你的防火墙是否开放端口8088,我为了方便直接把防火墙关了

查看已开放的端口

firewall-cmd --list-ports

查询端口是否开放

firewall-cmd --query-port=8001/tcp

查看防火墙状态

systemctl status firewalld  

关闭防火墙

systemctl stop firewalld  

移除端口

firewall-cmd --permanent --remove-port=8080/tcp

2)docker运行镜像后,通过docker logs -f 容器id  ,发现有一行提示说找不到default.conf

 然后找到挂载目录,发现多了一层conf.d目录,把default.conf文件移到第一个conf.d目录下即可。而conf.d目录下的conf.d目录 删掉即可。然后重新启动容器

7、在html目录下新建一个index.html页面,就可以通过你的ip地址:8088 就能访问到了

须知 ~ 其他操作

1、重新加载nginx配置

docker exec 容器id nginx -s reload

 2、nginx 监听了多个端口,但只有 80 端口起效果,如果想要多个端口起效果,则将 -p 80:80 换成 --net host,即

docker run  --name nginx2 --net host -v /mnt/nginx/conf.d:/etc/nginx/conf.d -v /mnt/nginx/logs:/var/log/nginx -v /mnt/nginx/html:/usr/share/nginx/html -v /mnt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  --privileged=true -d 41b0e86104ba

3、配置location的root 根目录时需要注意,不是写挂载的目录,而是写容器里的nginx里面的目录。目录地址看第四步。示例如下

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;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;

   #主要看server里面的代码,我在挂载目录的html目录下新建了hello1和hello2,会在容器 
   #内生成对应的目录。location的作用是匹配URL中的URI,root是定义文件根目录,如: 
   #访问 http://192.168.226.128/hello2/ 生成的文件地址是: 
   #/usr/share/nginx/html/hello2,默认找index 配置的index.html。如果换为 
   #http://192.168.226.128/hello33/ ,它匹配第一个 location,
   #地址将组装为:/usr/share/nginx/html/hello1/hello33/

        server {
            listen 80; 
            server_name 192.168.226.128; 
  
            location / {
               root   /usr/share/nginx/html/hello1;
               index  index.html;
            }

            location /hello2{
              root   /usr/share/nginx/html;
              index  index.html;
            }

            error_page 500 502 503 504 /50x.html; 
              location = /50x.html {
	          root html;
            }
         }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值