目录

使用 nginx 制作一个 yum mirror

目录

文章简介: 使用 nginx 制作一个 yum mirror

nginx 可以支持缓存文件,故将 nginx 配置成 yum 包 mirror server,带文件自动过期, 并限制最大文件过期时间.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#内容存储到 pkg_mirror.conf 文件中

upstream centos_mirror {
    server mirrors.tuna.tsinghua.edu.cn;
}

# centos 7 yum local source
server {
    listen 7000;
    server_name $host;
    index index.html ;
    root /opt/nginx/cache/;

    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers X-Requested-With;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

    location / {
        proxy_store on;
        proxy_temp_path "/opt/nginx/cache/";
        proxy_set_header Accept-Encoding identity;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host 'mirrors.tuna.tsinghua.edu.cn';
        proxy_next_upstream error http_502;

        if ( !-e $request_filename ) {
            proxy_pass http://centos_mirror;
        }

        expires 6h;
        # proxy_pass http://centos_mirror;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 将文件存储到 docker-compose.yaml 中
version: "3"
services:
  repo_mirror:
    container_name: repo_mirror
    image: nginx:alpine
    ports:
      - 7000:7000
    volumes:
      - ./pkg_mirror.conf:/etc/nginx/conf.d/pkg_mirror.conf
      - ./mirror_cache:/opt/nginx/cache
    logging:
      driver: "json-file"
      options:
        max-size: "512m"
    restart: always
1
docker-compose up -d

测试效果

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Dockerfile
FROM ubuntu:20.04
RUN sed -E -i -e 's/(archive|ports).ubuntu.com/192.168.0.102:7000/g' -e '/security.ubuntu.com/d' /etc/apt/sources.list

RUN apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates && \
    rm -rf /var/lib/apt/lists/* && \
    update-ca-certificates

docker build -t tmp:dev .
# 日志中 apt 使用了 192.168.0.102:7000 安装软件成功,且 mirror_cache 目录中有相关的文件