分类 其他 下的文章

一、nginx目录索引

nginx中内置了目录索引命令 auto_index ,十分方便就能给目录生成web索引:

location /ftp/ {
    alias /data/html;
    autoindex on;
}

效果如下:

PIC20180902_214954.png

两个可选的命令是 autoindex_exact_sizeautoindex_localtime ,分别表示是否精确显示文件大小(以字节方式)和是否显示本地时间,两个都 不开启 的情况下是这样的:

PIC20180902_215504.png

二、使用fancyindex索引

nginx自带的索引功能很单一,界面也很原始。后面有人做了个fancyindex的插件用来强化这个功能,已经被官方采用。官方文档地址,fancyindex代码地址:ngx-fancyindex

2.1 编译fancyindex

安装fancyindex要重新编译nginx,首先下载fancyindex源码

git clone https://github.com/aperezdc/ngx-fancyindex

解压nginx进入目录,重新执行 configure 命令,指定参数 --add-module 添加fancyindex模块,nginx安装的具体流程可以参考源码编译安装nginx

./configure --user=www --group=www \
    --prefix=/usr/local/nginx-1.12.2 \
    --with-http_stub_status_module \
    --with-http_ssl_module \
    --add-module=../ngx-fancyindex # 添加fancyindex模块

执行make和make install即可完成安装。

2.2 使用fancyindex

fancyindex的指令:

location /ftp/ {
    alias /data/software/nginx/;
    fancyindex on; # 使用fancyindex
    fancyindex_exact_size off; # 不显示精确大小
}

其效果如下:

fancyindex.png

点击表头的 File NameFile Size 或者 Date 能对文件进行排序。

2.3 其他用法

fancyindex提供了自定义页头和页脚,分别通过指令 fancyindex_headerfancyindex_footer 完成。

例如在页脚加上一个超链接到百度首页:

location /ftp/ {
    alias /data/software/nginx/;
    fancyindex on;
    fancyindex_exact_size off;
    fancyindex_footer "fancy_footer.html";
}

然后在 网站根目录下 加上一个 fancy_footer.html

<div id="footer">
    <a href="https://www.baidu.com">百度一下<a>
</div>

重新载入后的页面:

PIC20180902_221620.png

一、镜像和容器

docker中的镜像和容器对应linux环境中的程序和进程,不运行时是一个静态的二进制文件,运行就成了系统中的一个进程。docker运行时被称作容器,静态的文件则被称作镜像。

镜像是docker三大核心中最为重要的,因为运行docker首先得要有镜像。而镜像的来源有多种,可以从官方仓库获取,也可以手动制作。默认情况下启动一个镜像,如果不存在于本地,会从镜像仓库获取。

1.1 获取镜像

如果想要从镜像仓库获取镜像,使用 docker pull 命令就可以完成,例如:

ma@Y485:~$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete 
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest

仓库中的镜像包含两部分:镜像名和版本,版本字段可以省略,默认使用latest。
例如我们要获取ubuntu 14.04镜像,可以使用下面的命令完成:

ma@Y485:~$ docker pull ubuntu:14.04
14.04: Pulling from library/ubuntu
8284e13a281d: Pull complete 
26e1916a9297: Pull complete 
4102fc66d4ab: Pull complete 
1cf2b01777b2: Pull complete 
7f7a2d5e04ed: Pull complete 
Digest: sha256:71529e96591eb36a4100cd0cc5353ff1a2f4ee7a85011e3d3dd07cb5eb524a3e
Status: Downloaded newer image for ubuntu:14.04

1.1 查看本地镜像

查看本地已有的镜像可以通过命令 docker imagesdocker image ls 完成:

ma@Y485:~$ docker images 
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
tvial/docker-mailserver   latest              dac8816ec078        3 days ago          517MB
hello-world               latest              2cb0d9787c4d        5 weeks ago         1.85kB
shiningrise/hustoj        latest              bbbf10eeee29        8 weeks ago         490MB
ma@Y485:~$ docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
tvial/docker-mailserver   latest              dac8816ec078        3 days ago          517MB
hello-world               latest              2cb0d9787c4d        5 weeks ago         1.85kB
shiningrise/hustoj        latest              bbbf10eeee29        8 weeks ago         490MB

显示的结果中包含了镜像名,版本号,镜像ID,创建时间和镜像大小等,镜像ID是用来标识镜像的唯一ID。

1.3 搜索仓库中的镜像

使用docker search命令可以在远程仓库中搜索想要的镜像:

ma@Y485:~$ docker search hello-world
NAME                                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   624                 [OK]                
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   108                                     
tutum/hello-world                          Image to test docker deployments. Has Apache…   53                                      [OK]
....

默认会显示镜像名,镜像描述和star等信息,OFFICIAL表示是否为官方镜像,如果标有[OK]则表示是官方的,最后的AUTOMATED表示是否为自动创建的镜像。

search中提供了一些筛选的选项:

  • -s, --stars=n : 老版本中用来筛选star用的,表示只显示star大于等于n的镜像。新版docker用 --filter=start=n 来筛选。
  • --no-trunc=truc|false : 是否截断描述信息,默认情况下描述超多长度之后会被截断成...显示。
# 查找star大于500的hello-world镜像,且不截断描述信息。
ma@Y485:~$ docker search hello-world --stars=500 --no-trunc
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                DESCRIPTION                                          STARS               OFFICIAL            AUTOMATED
hello-world         Hello World! (an example of minimal Dockerization)   624                 [OK]                

1.4 镜像标签

镜像标签类似于linux中的 alias ,相当于给镜像取了别名:

ma@Y485:~$ docker tag ubuntu:14.04 myubunt
ma@Y485:~$ docker image ls | grep ubuntu
myubuntu                  latest              971bb384a50a        4 weeks ago         188MB
ubuntu                    14.04               971bb384a50a        4 weeks ago         188MB

新创建的标签镜像和实际的镜像ID都是相同的,指向了同一个镜像。

1.5 删除镜像

docker rmi 可以删除本地已经存在的镜像,删除镜像可以使用 镜像名+标签镜像id 完成。

例如当前 hello-world 镜像的id为 2cb0d9787c4d ,把它删除:

ma@Y485:~$ docker rmi 2cb0d9787c4d
Untagged: hello-world:latest
Untagged: hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Deleted: sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34
Deleted: sha256:ee83fc5847cb872324b8a1f5dbfd754255367f4280122b4e2d5aee17818e31f5

值得注意的是:如果容器镜像已经被运行了,即使容器处于停止的状态,镜像也是无法删除的。

ma@Y485:~$ docker rmi 2cb0d9787c4d
Error response from daemon: conflict: unable to delete 2cb0d9787c4d (must be forced) - image is being used by stopped container 5549f206097

这里提示镜像无法删除,应为有一个容器 5549f2060977 正在使用它,查看此时运行的容器:

ma@Y485:~$ docker ps # 查看所有正在运行中的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ma@Y485:~$ docker ps -aq # 查看运行中或者已经停止的容器,-q表示只显示容器id
85d2d2aa258a
d383073e1d32
5549f2060977

可以看到有一个已经停止了的容器 5549f2060977 ,它在占用 hello-world 镜像,删除经向前要把这个容器也删除了才行,删除容器使用 docker rm

ma@Y485:~$ docker rm 5549f2060977 # 删除容器
5549f2060977
ma@Y485:~$ docker rmi 2cb0d9787c4d # 删除镜像
Untagged: hello-world:latest
Untagged: hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Deleted: sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34
Deleted: sha256:ee83fc5847cb872324b8a1f5dbfd754255367f4280122b4e2d5aee17818e31f5

当镜像存在多个标签的时候,删除镜像只是删除了镜像的当前标签,实际的镜像并没有删除:

ma@Y485:~$ docker image ls | grep ubuntu # 当前有两个ubuntu镜像,有一个是镜像标签,实际都是一个镜像
myubuntu                  latest              971bb384a50a        4 weeks ago         188MB
ubuntu                    14.04               971bb384a50a        4 weeks ago         188MB
ma@Y485:~$ docker rmi myubuntu # 删除myubuntu标签
Untagged: myubuntu:latest
ma@Y485:~$ docker image ls | grep ubuntu 
ubuntu                    14.04               971bb384a50a        4 weeks ago         188MB

最后能看到,删除 myubuntu 之后,镜像并没有删除,原来的ubuntu和镜像依旧存在。

1.6 查看镜像信息

每个镜像都有自己的详细信息,包括作者、版本以及镜像大小等相关信息,使用命令 docker inspect 即可查看:

ma@Y485:~$ docker inspect hello-world
[
    {
        "Id": "sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34",
        "RepoTags": [
            "hello-world:latest"
        ],
        "RepoDigests": [
            "hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2018-07-11T00:32:08.432822465Z",
        "Container": "6b6326f6afc81f7850b74670aad2bf550c7f2f07cd63282160e5eb564876087f",
        "ContainerConfig": {
            "Hostname": "6b6326f6afc8",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/hello\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:6bc48d210ad4c6bbb74e02e6196a9133b57107033c09e92cac12616cad30ebcf",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "17.06.2-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/hello"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:6bc48d210ad4c6bbb74e02e6196a9133b57107033c09e92cac12616cad30ebcf",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 1848,
        "VirtualSize": 1848,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/96168c171bf2d3370860383cb4be12c68141940410bdd823a0ca04e19c77d0ee/merged",
                "UpperDir": "/var/lib/docker/overlay2/96168c171bf2d3370860383cb4be12c68141940410bdd823a0ca04e19c77d0ee/diff",
                "WorkDir": "/var/lib/docker/overlay2/96168c171bf2d3370860383cb4be12c68141940410bdd823a0ca04e19c77d0ee/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:ee83fc5847cb872324b8a1f5dbfd754255367f4280122b4e2d5aee17818e31f5"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

docker history 命令可以查看镜像的历史信息:

ma@Y485:~$ docker history hello-world
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
2cb0d9787c4d        5 weeks ago         /bin/sh -c #(nop)  CMD ["/hello"]               0B                  
<missing>           5 weeks ago         /bin/sh -c #(nop) COPY file:3c3ca82dfdb40d30…   1.85kB              

二、镜像仓库

2.1 Docker Hub

docker镜像仓库是一个集中存放镜像的地方,docker pull 就是直接在这个仓库获取的镜像。

docker官方维护了一个公共的镜像仓库Docker Hub,里面有许多优质的镜像。

除了下载镜像,这里还能上传个人制作的个人镜像。

2.1 阿里云镜像加速器

docker安装后默认的仓库地址Docker Hub,对于国内用户来说速度比较慢,可以考虑替换为国内的镜像仓库。

国内的镜像加速器比较好的有:DaoCloud, 时速云, 阿里云等,这里介绍阿里云镜像加速器的用法。

登陆后,阿里云会分配私有的加速器地址,里面有加速器在不同系统下的使用方法,按照教程设置即可。

Ubuntu下的替换教程:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://aketvpgu.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

其他系统可以查阅:

git和svn不同,没有一个中央服务器用于存放代码,作为一个分布式的版本控制工具,每台主机都是一个仓库。

如果多个人之间要分享代码,就需要一台远程的仓库协助完成,git中通过git remote指令来完成这些操作。

使用git remote -v可以看到所有的远程主机:

> git remote -v
origin    git@github.com:maqianos/gitstudy.git (fetch)
origin    git@github.com:maqianos/gitstudy.git (push)

- 阅读剩余部分 -

一、分支介绍

分支是git的特性之一,它使得代码可以有多中不同的方向和代码路线,协作过程中彼此之间互不影响,大大增加了代码库的灵活性。

一般来说,项目的开发模型为:给定主分支master用于统筹所有代码,其他的功能拆散在子分支上开发,最后都合并到master。例如一个用于用户登录功能的代码库,用户A开启分支完成账户注册,用户B开启分支完成用户授权,两者在开发期间互不影响,开发完成后合并到主分支上合并完成总功能。

中间蓝色的表示主分支,上下两条表示子分支,子分支在初始时分离,最后都合并于主分支。

git中和分支相关的操作使用branch命令完成,最简单的用法就是git branch,它用于查看当前所在分支,加上-v选项显示详细信息。

显示的分支中默认只显示本地分支,如需显示远程分支要加上-a选项。

- 阅读剩余部分 -

习惯了svn的svn co,觉得git checkout不方便遂通过git的aliascheckout重命名为co

这个操作以前一直都是相安无事,今天在一台新机器上运行时遇到以下问题:

> git co
fatal: cannot exec ''git-co'': Permission denied
> git ss  # ss = status -s
fatal: cannot exec ''git-ss'': Permission denied

网上找了半天没有找到解决方案,并且发现不使用简写执行命令没有问题。

于是猜测可能是版本太低的原因,当前的版本是1.7,升级到2.17问题就不存在了,升级参考CentOS6.5源码安装Git

svn更新项目时出错Previous operation has not finished; run ''cleanup'' if it was interrupted

出现问题的原因是上一次更新或者提交代码操作未完成,此时需要根据提示进行cleanup操作:

- 阅读剩余部分 -

通过git resetgit checkout进行版本回退之后再次git pull抛出以下错误:

You are not currently on a branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

意思是当前的版本已经不在master分支了,解决的办法:

  1. git status查看所有变化的文件,把有改动的先删除。
  2. git checkout master回到主分支。
  3. git pull拉取最新代码。

一、概述

nginx作为当前最流行的开源web服务器之一,被无数公司或个人在使用。正因为开源,所以任何人都可以获取到它的源码,这其中就包含了世界各地的黑客。他们无时无刻不在想着如何找出nginx的漏洞,以至于在网上随便一搜都能找到无数nginx相关的漏洞:

默认情况下,http响应的Server头部都会携带上服务器的名字和版本信息:

> curl -I http://127.0.0.1
HTTP/1.1 502 Bad Gateway
Server: nginx/1.12.2   # 携带有服务端和版本号
Date: Mon, 12 Mar 2018 01:25:00 GMT
...

而一旦黑客知道了服务端软件的版本信息,很容易就能通过对应版本的漏洞来攻击服务器,引发安全问题。所以针对生产环境的服务器,有必要隐藏或者修改软件版本信息,以避免黑客的指向性攻击。

- 阅读剩余部分 -