使用nginx参数 网上一大把 我只记录 使用方法
1 设置缓存目录 需要在nginx 配置文件设置以下数据
(1)pc缓存目录
proxy_cache_path /www/wwwroot/xxxx/nginxcachepc levels=1:2 keys_zone=pc_cache:100m inactive=1d max_size=10g;
(2) 手机端缓存目录
proxy_cache_path /www/wwwroot/xxx/nginxcachewap levels=1:2 keys_zone=mobile_cache:100m inactive=1d max_size=10g;
2. 站点设置
proxy_pass http://backend;
set $is_mobile 0;
set $modldatas pc_cache;
if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
set $is_mobile 1;
}
# PC端
if ($is_mobile = 0) {
set $modldatas pc_cache;
}
# 移动端
if ($is_mobile = 1) {
set $modldatas mobile_cache;
}
proxy_cache $modldatas;
proxy_cache_key "$scheme$request_method$host$uri";
proxy_cache_valid 200 302 1d;
proxy_cache_valid 404 1m;
proxy_cache_valid any 10s;
proxy_cache_min_uses 1;
expires 1d;
add_header Nginx-Cache $upstream_cache_status;
proxy_cache_bypass $http_pragma;
proxy_cache_revalidate on;
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;
# 以$arg开头的参数表示url中的对应的参数。 该指令配置不走缓存响应的条件,如果条件命中,则不走缓存。可与proxy_no_cache配合使用
proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment;
# 以$arg开头的参数表示url中的对应的参数。
proxy_no_cache $cookie_nocache $arg_nocache$arg_comment;