一、根据url中的参数来确定缓存的key

set_by_lua_block $dataArg {
local enc = ngx.req.get_uri_args()["enc"]
local key = ngx.req.get_uri_args()["key"]
local name = ngx.req.get_uri_args()["name"]
local str = tostring(enc)..tostring(key)..tostring(name)
return str
}
proxy_cache_key $host$uri$dataArg;

二、根据源站传过来的跨域头做判断

set $cors_origin "*";
if ($http_origin != ""){
set $cors_origin $http_origin;
}
more_set_headers "Access-Control-Allow-Origin:$cors_origin";

三、根据不同的运营商走不同的upstream

1、下层传递一个请求头:
location / {
proxy_buffer_size 128k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
proxy_set_header Host $host;
proxy_set_header CDN ctyun;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header isp cm; #传递一个请求头为cm(移动)
add_header Powered-By-ctcdn "$server_addr";
proxy_cache $cache_store;
proxy_pass $scheme://test-upstream_$scheme$server_port;
} 2、上层做判断
server {
listen 80;
server_name www.test.com;
resolver 8.8.8.8 114.114.114.114;
underscores_in_headers on; #nginx开启客户读取自定义头部的值 error_log /data/log/nginx/error.log;
access_log /data/log/nginx/access.log nginxlog; location / {
proxy_buffer_size 128k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
proxy_set_header Host $host;
proxy_set_header CDN test;
add_header Powered "$server_addr";
proxy_cache $cache_store; ##此处即下层如果有移动联通电信设备;在上层需要移动回移动的upstream;联通回联通的upstream;电信回电信的upstream;
if ($http_isp = "ct") {
proxy_pass $scheme://test-ct_$scheme$server_port;
}
if ($http_isp = "cn") {
proxy_pass $scheme://test-cn_$scheme$server_port;
}
if ($http_isp = "cm") {
proxy_pass $scheme://test-cm_$scheme$server_port;
}
} 3、上层upstream配置
upstream test-cm_http80 {
server 1.1.1.1:80;
} upstream test-cn_http80 {
server 2.2.2.2:80;
} upstream test-ct_http80 {
server 3.3.3.3:80;
}

四、proxy_cache_use_stale模块的作用

缓存过期,但是源站有问题,可以直接返回用户旧的内容,返回旧文件总比报错强,一般应用在源站有问题的情况;
Syntax: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...;
Default: proxy_cache_use_stale off;
Context: http, server, location
官网介绍:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_use_stale
测试过程:
upstream test_http80 {
server 1.1.1.1:8085;
}
server {
listen 80;
server_name www.test.com;
resolver 8.8.8.8 114.114.114.114;
proxy_cache $cache_store;
error_log /log/error.log;
access_log /log/access.log nginxlog; location ~*\.(txt|jpg|gif|png|bmp|ico)$ {
more_clear_headers -s '200 206 304' 'Set-Cookie' 'Server' 'X-Varnish' 'x-hits' 'X-Cache' 'Via' 'Age';
proxy_ignore_headers "Cache-Control" "Set-Cookie" "Expires" "Vary";
proxy_cache_valid 200 206 5s;
proxy_cache_key $host$uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header CDN test;
age_header;
expires 5s;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
add_header Powere-test "$upstream_cache_status from $ipadd";
proxy_pass "${scheme}://test_${scheme}${server_port}";
}
}

最终效果:当上游服务器1.1.1.1无法访问时,返回旧内容;

 五、当访问资源404时,对url做改写

server {
listen 80;
server_name www.zhide666.cn; location / {
proxy_pass http://www.upstream.com;
}
#当资源不在本地,但是访问该资源的时候资源不存在,当上游服务器返回404状态码时,也需要通过error_page rewrite到指定的url;
location ~/portal/(.*)$ {
proxy_intercept_errors on; #当error_page和proxy_proxy_pass同时存在时则需要加该配置;
#recursive_error_pages on;
proxy_pass http://www.upstream.com;
error_page 404 https://www.zhide666.cn/ketang/$1$is_args$args; #不加is_args和args会忽略url中的参数;
}
#当资源在本地,但是访问该资源的时候资源不存在,则通过error_page rewrite到指定的url;
location ~/movie/(.*)$ {
root /data/movie/;
error_page 404 https://www.zhide666.cn/ketang/$1$is_args$args;
}
}

p.p1 { margin: 0; font: 12px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
p.p1 { margin: 0; font: 12px Menlo; color: rgba(0, 0, 0, 1) }
p.p2 { margin: 0; font: 12px Menlo; color: rgba(0, 0, 0, 1); min-height: 14px }
span.s1 { font-variant-ligatures: no-common-ligatures }
span.s2 { font-variant-ligatures: no-common-ligatures; background-color: rgba(224, 228, 9, 1) }
p.p1 { margin: 0; font: 12px Menlo; color: rgba(0, 0, 0, 1) }
p.p2 { margin: 0; font: 12px Menlo; color: rgba(0, 0, 0, 1); min-height: 14px }
span.s1 { font-variant-ligatures: no-common-ligatures }
span.s2 { font-variant-ligatures: no-common-ligatures; background-color: rgba(224, 228, 9, 1) }
p.p1 { margin: 0; font: 12px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
span.s2 { font-variant-ligatures: no-common-ligatures; background-color: rgba(224, 228, 9, 1) }
p.p1 { margin: 0; font: 12px Menlo; color: rgba(0, 0, 0, 1) }
span.s1 { font-variant-ligatures: no-common-ligatures }
span.s2 { font-variant-ligatures: no-common-ligatures; color: rgba(180, 36, 25, 1) }

nginx的高级用法的更多相关文章

  1. nginx篇高级用法之基于TCP/UDP的四层调度

    nginx 从1.9版本开始支持基于TCP/UDP的四层调度,在编译nginx时使用--with-stream开启该模块 支持TCP/UDP调度时,支持给所有的软件做调度器,例如:nfs smb ft ...

  2. redis的Linux系统安装与配置、redis的api使用、高级用法之慢查询、pipline事物

    今日内容概要 redis 的linux安装和配置 redis 的api使用 高级用法之慢查询 pipline事务 内容详细 1.redis 的linux安装和配置 # redis 版本选择问题 -最新 ...

  3. Visual Studio 宏的高级用法

    因为自 Visual Studio 2012 开始,微软已经取消了对宏的支持,所以本篇文章所述内容只适用于 Visual Studio 2010 或更早期版本的 VS. 在上一篇中,我已经介绍了如何编 ...

  4. SolrNet高级用法(分页、Facet查询、任意分组)

    前言 如果你在系统中用到了Solr的话,那么肯定会碰到从Solr中反推数据的需求,基于数据库数据生产索引后,那么Solr索引的数据相对准确,在电商需求中经常会碰到菜单.导航分类(比如电脑.PC的话会有 ...

  5. sqlalchemy(二)高级用法

    sqlalchemy(二)高级用法 本文将介绍sqlalchemy的高级用法. 外键以及relationship 首先创建数据库,在这里一个user对应多个address,因此需要在address上增 ...

  6. Solr学习总结(六)SolrNet的高级用法(复杂查询,分页,高亮,Facet查询)

    上一篇,讲到了SolrNet的基本用法及CURD,这个算是SolrNet 的入门知识介绍吧,昨天写完之后,有朋友评论说,这些感觉都被写烂了.没错,这些基本的用法,在网上百度,资料肯定一大堆,有一些写的 ...

  7. 再谈Newtonsoft.Json高级用法

    上一篇Newtonsoft.Json高级用法发布以后收到挺多回复的,本篇将分享几点挺有用的知识点和最近项目中用到的一个新点进行说明,做为对上篇文章的补充. 阅读目录 动态改变属性序列化名称 枚举值序列 ...

  8. Jquery remove 高级用法

    Jquery remove 高级用法 html 代码 <div class="file-image">abc1111</div><div class= ...

  9. Newtonsoft.Json高级用法(转)

    手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...

随机推荐

  1. CSS常用技术

    1.处理图片底部 5px 间距 <style> body {background: #2d97db;} .imgBox { background: #fff; font-size: 0; ...

  2. NLP教程(5) - 语言模型、RNN、GRU与LSTM

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/36 本文地址:http://www.showmeai.tech/article-det ...

  3. linux下nginx软件的学习

    参考博客 1.nginx是什么 nginx是一个开源的,支持高性能,高并发的web服务和代理服务软件.它是开源的软件. nginx比它大哥apache性能改进许多,nginx占用的系统资源更少,支持更 ...

  4. python 通过线上API查询ip归属地

    API为国外API,频率限制1分钟45个ip 脚本如下 1 #!/usr/bin/env python3 2 #-*-coding:utf-8-*- 3 4 import requests,re,js ...

  5. 【Java面试】Zookeeper中的Watch机制的原理?

    一个工作了7年的粉丝,遇到了一个Zookeeper的问题. 因为接触过Zookeeper这个技术,不知道该怎么回答. 我说一个工作了7年的程序员,没有接触过主流技术,这不正常. 于是我问了他工资以后, ...

  6. 深入解读SQL的聚集函数

    摘要:本文从基本聚集操作入手,介绍常用的SQL语法,以及一些扩展的聚集功能,同时会讲到在GaussDB(DWS)里聚集相关的一些优化思路. 本文分享自华为云社区<GaussDB(DWS) SQL ...

  7. npm错误:Cannot find module ‘compression-webpack-plugin

    转自 (82条消息) 前端开发遇到Cannot find module 'compression-webpack-plugin'问题解决_brave_zhao的博客-CSDN博客 <div id ...

  8. 超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群

    前言 干货又来了,全程无废话,可先看目录了解. MySQL搭建集群最常见的是binlog方式,但还有一种方式是强一致性的,能保证集群节点的数据一定能够同步成功,这种方式就是pxc,本篇就使用图文方式一 ...

  9. 初步了解认识正则表达式(Regex)

    如果你感到这篇文章对您有所帮助,那请您给我一个免费的赞吧QWQ! 如果想要深入理解什么是正则表达式,请购买教材<形式语言与自动机>,相信学完它之后一定会让你更加理解正则表达式! 1.你的同 ...

  10. Spring Security:用户和Spring应用之间的安全屏障

    摘要:Spring Security是一个安全框架,作为Spring家族的一员. 本文分享自华为云社区<[云驻共创]深入浅出Spring Security>,作者:香菜聊游戏. 一.前言 ...