首页>软件资讯>常见问题

常见问题

Docker+Nginx搭建小型CDN服务器

发布时间:2025-06-05 11:55:26人气:7

最近购入一个非常小的小机,线路非常不错是香港大陆优化的机器,想着没不了啥,就试试给一个AI站点做个CDN试试


涉及文件

• docker-compose.yml Docker编排文件

• nginx.conf Nginx 配置文件

nginx.conf 文件内容

worker_processes auto;


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"'

                      ' $upstream_cache_status';

    

    access_log  /var/log/nginx/access.log  main;

    

    sendfile        on;

    keepalive_timeout  65;

    

    # 设置缓存

    proxy_cache_path /data/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m;

    proxy_cache_key "$scheme$request_method$host$request_uri";

    proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;

    

    server {

        listen 80;

        server_name www.ai-ku.cn;

        

        # 强制HTTPS(可选)

        # return 301 https://$host$request_uri;

        

        location / {

            proxy_pass http://154.219.105.97;  # 源站地址

            proxy_cache my_cache;

            proxy_cache_valid 200 304 30m;

            add_header Cache-Control "public, max-age=1800";

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_set_header X-Forwarded-Proto $scheme;

        }

        

        location ~* \.(jpg|jpeg|png|gif|css|js|ico|svg|woff|woff2|ttf|eot)$ {

            proxy_pass http://154.219.105.97;

            proxy_cache my_cache;

            proxy_cache_valid 200 30d;

            add_header Cache-Control "public, max-age=2592000";  # 30天

        }

    }

}    

docker-compose.yml 文件内容

version: '3'


services:

  nginx:

    image: nginx:alpine

    container_name: nginx-cdn

    ports:

      - "80:80"  # 自行调整端口

      - "443:443"  # 自行调整端口

    volumes:

      - ./nginx.conf:/etc/nginx/nginx.conf:ro  # 映射配置文件

      - ./cache:/data/cache  # 映射缓存目录

      - ./ssl:/etc/ssl/certs  # 映射SSL证书(可选)

    restart: always    



上一条:Docker容器自启动失效?

下一条:如何通过Docker复现PHP反序列化漏洞