插件窝 干货文章 Nginx反向代理缓存配置,实现静态网页访问加速

Nginx反向代理缓存配置,实现静态网页访问加速

cache proxy 缓存 Nginx 455    来源:    2025-04-16

Nginx反向代理缓存配置实现静态网页加速

基本概念

Nginx反向代理缓存可以将后端服务器的静态内容缓存在Nginx服务器上,当用户请求相同内容时直接从Nginx缓存中返回,显著提高响应速度并减轻后端服务器压力。

配置步骤

1. 基本反向代理配置

首先配置基本的反向代理:

http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

2. 添加缓存配置

在http块中添加缓存配置:

http {
    # 定义缓存路径和参数
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m use_temp_path=off;

    # 其他配置...
}

参数说明: - /var/cache/nginx:缓存文件存放路径 - levels=1:2:缓存目录层级结构 - keys_zone=my_cache:10m:定义共享内存区域名称和大小 - inactive=60m:60分钟内未被访问的缓存将被删除 - use_temp_path=off:禁用临时路径

3. 在server/location中启用缓存

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;

        # 启用缓存
        proxy_cache my_cache;

        # 缓存键设置
        proxy_cache_key "$scheme$request_method$host$request_uri";

        # 缓存有效时间
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;

        # 添加缓存状态头信息(调试用)
        add_header X-Proxy-Cache $upstream_cache_status;
    }
}

4. 高级缓存控制

location / {
    proxy_pass http://backend;

    # 缓存相关配置
    proxy_cache my_cache;
    proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
    proxy_cache_valid 200 302 1h;
    proxy_cache_valid 301 1d;
    proxy_cache_valid any 5m;

    # 缓存锁定(防止多个请求同时更新缓存)
    proxy_cache_lock on;
    proxy_cache_lock_timeout 5s;

    # 缓存更新策略
    proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;

    # 后端响应头控制
    proxy_ignore_headers Cache-Control;
    proxy_hide_header Set-Cookie;

    # 添加缓存状态头
    add_header X-Proxy-Cache $upstream_cache_status;
}

针对静态文件的优化配置

对于静态文件(如图片、CSS、JS等),可以单独配置更长的缓存时间:

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    proxy_pass http://backend;
    proxy_cache my_cache;
    proxy_cache_key "$scheme$host$request_uri";
    proxy_cache_valid 200 302 12h;
    proxy_cache_valid 404 1m;

    # 静态文件通常不需要cookie
    proxy_hide_header Set-Cookie;

    # 强制缓存(即使后端设置了no-cache)
    proxy_ignore_headers Cache-Control;
    proxy_ignore_headers Expires;

    add_header X-Proxy-Cache $upstream_cache_status;
}

缓存清理配置

可以设置缓存清理接口(需要ngx_cache_purge模块):

location ~ /purge(/.*) {
    allow 127.0.0.1;
    allow 192.168.1.0/24;
    deny all;

    proxy_cache_purge my_cache "$scheme$request_method$host$1";
}

性能调优参数

http {
    # 开启文件描述符缓存
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    # 优化缓冲区
    proxy_buffers 16 32k;
    proxy_buffer_size 64k;
    proxy_busy_buffers_size 128k;

    # 优化连接
    proxy_connect_timeout 5s;
    proxy_read_timeout 60s;
    proxy_send_timeout 60s;

    # 启用keepalive连接
    proxy_http_version 1.1;
    proxy_set_header Connection "";
}

完整示例配置

http {
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:100m inactive=24h use_temp_path=off;

    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
    }

    server {
        listen 80;
        server_name example.com;

        # 静态文件缓存
        location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg)$ {
            proxy_pass http://backend;
            proxy_cache my_cache;
            proxy_cache_key "$scheme$host$request_uri";
            proxy_cache_valid 200 302 24h;
            proxy_cache_valid 404 1h;
            proxy_ignore_headers Cache-Control Expires Set-Cookie;
            add_header X-Proxy-Cache $upstream_cache_status;
            expires 30d;
            access_log off;
        }

        # 动态内容缓存
        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;
            proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 301 1h;
            proxy_cache_valid any 5m;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            proxy_cache_lock on;
            proxy_cache_lock_timeout 5s;
            add_header X-Proxy-Cache $upstream_cache_status;
        }

        # 缓存清理接口
        location ~ /purge(/.*) {
            allow 127.0.0.1;
            allow 192.168.1.0/24;
            deny all;
            proxy_cache_purge my_cache "$scheme$request_method$host$1";
        }
    }
}

验证和监控

  1. 检查缓存是否生效:

    • 查看响应头中的X-Proxy-Cache字段
    • 第一次访问为MISS,后续访问为HIT
  2. 监控缓存目录大小:

    du -sh /var/cache/nginx
    
  3. 查看缓存统计信息(需要安装额外模块):

    location /cache-stats {
       proxy_cache_stats;
       allow 127.0.0.1;
       deny all;
    }
    

通过以上配置,Nginx可以有效地缓存静态内容,显著提高网站访问速度并降低后端服务器负载。