+
95
-

回答

用这个,我测试了一下可行,cdnimg.example.com改成你自己的域名,bucketname.oss-cn-beijing.aliyuncs.com换成你自己的oss的bucket url:

http {
    # 定义缓存区域
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=40g inactive=60m use_temp_path=off;

    server {
        listen 80;
        server_name cdnimg.example.com;

        location / {
            # 设置缓存键,使用URL作为键
            proxy_cache_key "$scheme$request_method$host$request_uri";

            # 使用缓存区域
            proxy_cache my_cache;

            # 设置缓存有效期为永久(除非达到最大缓存大小)
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            # 修改发送给后端服务器的Host头
            proxy_set_header Host bucketname.oss-cn-beijing.aliyuncs.com;

            # 修改发送给后端服务器的Referer头
            proxy_set_header Referer "http://www.example.com";
            # 代理请求到后端服务器
            proxy_pass http://bucketname.oss-cn-beijing.aliyuncs.com;

        }
    }
}

网友回复

我知道答案,我要回答