zoukankan      html  css  js  c++  java
  • docker 安装nginx

    第一步,上http://hub.docker.com查找最新nginx发布版本

    image

    第二步,拉取镜像,简单运行镜像,进入容器查看需要外挂的文件。

    # 拉取版本号为1.20.1的nginx镜像
    docker pull nginx:1.20.1
    # 以拉取的镜像运行一个配置了80:80的容器,容器名字为 nginx
    docker run --name nginx -p 80:80 nginx:1.20.1
    # 进入名字为nginx的容器查看容器目录,
    docker exec -it nginx /bin/bash
    ls
    

    image
    先根据别的容器使用经验,先去/usr/share/nginx下面看下。找到nginx默认的html路径。确定该路径需要外挂
    image
    /usr/share/nginx下面没有找到配置文件,回根路径,在/etc里面找到nginx配置路径,同时发现了module文件夹。
    image
    在使用https://cn.bing.com搜索一下别人的外挂文件经验确定下来,需要外挂的有两个文件夹。

    第三步,退出容器,复制指定文件至宿主机目录

    # 退出容器
    exit
    docker cp nginx:/etc/nginx /data
    docker cp nginx:/usr/share/nginx/html /data/nginx
    # 进入宿主机外挂文件夹下看下文件,发现modules指向无效引用。还好我宿主机**/usr/lib/nginx/modules**下没有东西
    cd /data/nginx && ll
    

    image

    # 复制**modules**文件夹至宿主机
    # 这里报错 cannot overwrite non-directory "/data/nginx/modules" with directory "/data/nginx",需要先删除宿主机现在的modules引用才行
    rm -r /data/nginx/modules
    # 复制实际文件夹出来
    docker cp nginx:/usr/lib/nginx/modules /data/nginx
    

    第四步:关闭容器并删除容器,指定外挂文件夹启动容器

    # 关闭容器名(或者容器id)为nginx的容器
    docker stop nginx
    # 删除容器
    docker rm nginx
    # 指定外挂文件夹启动容器
    docker run --name nginx 
    -p 80:80 
    -v /data/nginx/html:/usr/share/nginx/html 
    -v /data/nginx/modules:/usr/lib/nginx/modules 
    -v /data/nginx:/etc/nginx 
    nginx:1.20.1
    

    看到启动成功,使用curl测试一下
    image
    image

    扩展

    初始启动指令只配置了一个端口映射,而nginx作为一个代理软件,后续肯定回新增端口代理的。所以需要为容器新增端口映射
    docker 新增容器端口映射

    配置文件代理,顺便验证stream流模块是否正常

    user  nginx;
    worker_processes  auto;
    
    error_log  /var/log/nginx/error.log notice;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    stream {
      server {
          listen  8090;
          proxy_pass 127.0.0.1:8080;
      }
    }
    
    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;
    
        server {
            listen       8080;
            server_name  localhost;
            location / {
                root   /usr/share/nginx/html;
                autoindex on;
            }
        }
    }
    
  • 相关阅读:
    【并查集】连接格点C++
    【拓扑排序】威虎山上的分配C++
    CF39D Cubical PlanetC++
    【拓扑排序】排队C++
    [USACO09OCT]Invasion of the Milkweed】乳草的侵占C++
    免杀常用手段
    DELPHI 线程类
    动态注册OCX
    DELPHI 常用文件路径
    套接字(TCP)
  • 原文地址:https://www.cnblogs.com/jiangdewen/p/15133285.html
Copyright © 2011-2022 走看看