×

nginx 配置反向代理缓存,手机和桌面分别缓存,WP登录用户跳过缓存

Falcon 2024-06-04 views:
摘要

正在生成中……

nginx反向代理配置文件如下:

server {
    listen 443 ssl;
    server_name foo.com;

    ssl_certificate /path/to/your/cert.pem;
    ssl_certificate_key /path/to/your/cert.key;

    location /hk/ {
        proxy_pass https://192.168.0.14/hk/;
        proxy_ssl_verify off;

        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;

        proxy_connect_timeout 60s;
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;
    }
}

我需要给这个反向代理加上缓存,并且移动设备访问和pc访问分别建立不同的缓存,而且因为源站是wordpress的,检测到用户登录时要跳过缓存。请问应该如何修改配置?


在 Nginx 配置中添加缓存,并为移动设备和 PC 分别建立不同的缓存,还要在检测到用户登录时跳过缓存,这是一个复杂的任务。以下是一个详细的配置示例:

  1. 启用缓存:在 Nginx 配置文件中添加缓存路径和缓存配置。
  2. 为移动设备和 PC 分别建立不同的缓存:使用 map 指令区分移动设备和 PC。
  3. 跳过已登录用户的缓存:通过检测 Cookie 中的登录状态来实现。

配置示例

编辑你的 Nginx 配置文件,添加缓存配置并更新反向代理设置。

步骤 1: 配置缓存路径

http 块中添加缓存路径和缓存键:

http {
    # 缓存路径配置
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache_zone:10m inactive=60m use_temp_path=off;

    # 缓存键配置
    map $http_user_agent $device_type {
        default desktop;
        "~*android|bb\d+|meego|iphone|ipad|ipod|opera mini|operamobi|iemobile|mobile|tablet|kindle|silk|fennec" mobile;
    }

    map $cookie_wp_logged_in $cache_bypass {
        default 0;
        "~*wordpress_logged_in" 1;
    }

    # 其他http块中的配置...
}

步骤 2: 配置反向代理和缓存设置

server 块中添加缓存配置,并更新 location 块:

server {
    listen 443 ssl;
    server_name foo.com;

    ssl_certificate /path/to/your/cert.pem;
    ssl_certificate_key /path/to/your/cert.key;

    location /hk/ {
        proxy_pass https://192.168.0.14/hk/;
        proxy_ssl_verify off;

        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;

        proxy_connect_timeout 60s;
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;

        # 缓存设置
        proxy_cache cache_zone;
        proxy_cache_key $scheme$proxy_host$uri$is_args$args$device_type;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        proxy_cache_bypass $cache_bypass;
        proxy_no_cache $cache_bypass;

        add_header X-Proxy-Cache $upstream_cache_status;
    }

    # 其他server块中的配置...
}

解释

  1. 缓存路径配置

    • proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache_zone:10m inactive=60m use_temp_path=off;:定义缓存路径、缓存区和缓存的参数。
    • levels=1:2:定义缓存文件的层次结构。
    • keys_zone=cache_zone:10m:定义缓存区的名称和大小。
    • inactive=60m:设置缓存的非活动超时时间。
    • use_temp_path=off:禁用临时路径,以提高性能。
  2. 缓存键配置

    • map $http_user_agent $device_type {}:根据 User-Agent 头区分移动设备和 PC。
    • map $cookie_wp_logged_in $cache_bypass {}:根据 Cookie 中的 wordpress_logged_in 字段决定是否绕过缓存。
  3. 反向代理缓存设置

    • proxy_cache cache_zone;:启用缓存,并指定缓存区。
    • proxy_cache_key $scheme$proxy_host$uri$is_args$args$device_type;:根据请求的 URL 和设备类型生成缓存键。
    • proxy_cache_valid:定义缓存的有效时间。
    • proxy_cache_bypass $cache_bypass;proxy_no_cache $cache_bypass;:当检测到用户登录时跳过缓存。
    • add_header X-Proxy-Cache $upstream_cache_status;:在响应头中添加缓存状态标志,用于调试和监控。

其他注意事项

  • 缓存目录权限:确保 Nginx 进程对 /var/cache/nginx 目录具有读写权限。
  • 测试配置:在生产环境中使用前,请务必在测试环境中验证配置的正确性。
  • 缓存策略调整:根据实际应用需求调整缓存策略和时间。

通过这些配置,可以实现移动设备和 PC 分别建立不同的缓存,并在检测到用户登录时跳过缓存。

本文收录于