求 这种nginx缓存配置怎么写

122 1

反代本机5000端口并缓存响应码为302的内容
如果重定向的链接包含example.com(也就是域名为*.example.com)那么就缓存10小时,缓存键为 $scheme$request_method$host$request_uri$http_user_agent$http_authorization
否则缓存10分钟,缓存键为  $scheme$request_method$host$request_uri$http_authorization

这种NGINX配置怎么写?

我的签名没了 呜呜呜~ 本人是第二个靠自己努力获得认证的人
最新回复 ( 1 )
  • 2
    0
    http {
        include       mime.types;
        default_type  application/octet-stream;

        # 定义缓存区域
        proxy_cache_path /path/to/cache levels=1:2 keys_zone=cache_zone:10m max_size=1g inactive=60m use_temp_path=off;

        server {
            listen       80;
            server_name  your_domain.com;

            location / {
                proxy_pass http://localhost:5000;

                # 配置缓存
                proxy_cache cache_zone;
                proxy_cache_valid 302 10m;
                proxy_cache_key $scheme$request_method$host$request_uri$http_authorization;

                # 条件缓存
                set $cache_time 10m;
                if ($upstream_http_location ~* "example\.com") {
                    set $cache_time 10h;
                    proxy_cache_key $scheme$request_method$host$request_uri$http_user_agent$http_authorization;
                }

                proxy_cache_valid 302 $cache_time;
                proxy_ignore_headers "Set-Cookie" "Cache-Control";

                # 缓存的其他配置
                proxy_cache_bypass $http_cache_control;
                add_header X-Cache-Status $upstream_cache_status;
            }
        }
    }


    解释:
    proxy_cache_path: 配置缓存路径和缓存区域。
    proxy_pass: 将请求代理到本地5000端口。
    proxy_cache: 指定使用的缓存区域。
    proxy_cache_valid: 设置默认的缓存时间为10分钟。
    proxy_cache_key: 配置默认的缓存键。
    set $cache_time: 设置默认缓存时间变量为10分钟。
    if: 如果重定向链接包含example.com,则缓存10小时,并修改缓存键。
    proxy_ignore_headers: 忽略 Set-Cookie 和 Cache-Control 头以确保缓存生效。
    proxy_cache_bypass: 如果请求头中包含 Cache-Control 则跳过缓存。
    add_header: 添加缓存状态到响应头中,便于调试。

    注意:
    proxy_cache_path 中的路径需要你自行修改为合适的路径。
    确保你的NGINX已经安装了 ngx_http_proxy_module 模块。
    如果你的应用程序有特定的需求,可以根据实际情况调整配置。
  • 游客
    3

    您需要登录后才可以回帖

    登录 注册

发新帖