Docker中使用nginx服务

# 一、安装Nginx环境

1、docker安装自不用说,运行 docker version,如果有版本号就有环境了。

2、查找nginx可用镜像列表:

docker search nginx

比如我看到 nginx 这个镜像使用的STARS最多,我就用这个镜像啦!

3、摘取镜像:

docker pull nginx

4、运行镜像:

docker run -d -p 80:80 --name my_nginx nginx

5、使用浏览器或curl工具测试80端口,应该完全正常了。

# 二、配置本地运行

1、本地创建目录:

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

2、创建nginx配置文件1:

vim /data/nginx/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;

    include /etc/nginx/conf.d/*.conf;
}

3、创建nginx配置文件2:

vim /data/nginx/conf.d/default.conf
server {  
    listen       6080;  
    server_name  localhost;  
  
    #access_log  /var/log/nginx/log/host.access.log  main;  
  
    location / {  
        root   /usr/share/nginx/html; 
        index  index.html index.htm;  
        autoindex  on;  
    try_files $uri /index/index/page.html;  
        #try_files $uri /index/map/page.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;  
    }    
}

注意:这里的 root 路径不要修改,这是镜像中的路径,不是你本机(宿主)上的路径。

本配置文件中,我们启动用 6080 端口。

4、把你的HTML文件,如前端打包文件复制到 /data/nginx/html 目录下

5、开始启动了,通过参数把你本机(宿主)的路径挂载到docker容器环境中

docker run --name nginx6080 -d -p 6080:6080 
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf 
-v /data/nginx/conf.d:/etc/nginx/conf.d 
-v /data/nginx/logs:/var/log/nginx 
-v /data/nginx/html:/usr/share/nginx/html 
nginx

# 三、维护 nginx 环境

1、修改 default.conf 后,如何运行 nginx -s reload 呢?

docker exec -it nginx6080 nginx -s reload

2、如何查看容器内的文件?

docker ps

把列表中 nginx6080 的容器ID 24b86b33ef43 抄到如下语句中:

docker exec -it 24b86b33ef43 /bin/bash

此时通过 ls 等命令,可以看到容器内有 /etc/nginx 的相关目录和配置文件,还有 /usr/share/nginx/html 等。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值