一、根据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. vuepress搭建UI组件库文档踩坑篇

    为了实现组件效果预览及代码展示可折叠功能,使用了插件vuepress-plugin-demo-container 相关配置可参考官网说明文档 第一步 安装插件 npm i - D vuepress-p ...

  2. crontab和cron表达式详解

    引言 我们在定时任务中经常能接触到cron表达式,但是在写cron表达式的时候我们会遇到各种各样版本的cron表达式,比如我遇到过5位.6位甚至7位的cron表达式,导致我一度搞混这些表达式.更严重的 ...

  3. 白嫖Azure与体验GoLand远程开发

    前言 近期因为有本地开发远程使用Linux编译部署的需求,而虚拟机的性能实在是不敢恭维,WSL的坑之前也踩过(没有systemd等),故考虑使用SSH连接云服务器开发. 目前VSCode提出了Remo ...

  4. 管理订单状态,该上状态机吗?轻量级状态机COLA StateMachine保姆级入门教程

    前言 在平常的后端项目开发中,状态机模式的使用其实没有大家想象中那么常见,笔者之前由于不在电商领域工作,很少在业务代码中用状态机来管理各种状态,一般都是手动get/set状态值.去年笔者进入了电商领域 ...

  5. 树莓派开发笔记(十五):树莓派4B+从源码编译安装mysql数据库

    前言   树莓派使用数据库时,优先选择sqlite数据库,但是sqlite是文件数据库同时仅针对于单用户的情况,考虑到多用户的情况,在树莓派上部署安装mysql服务,通过读写锁事务等使用,可以实现多进 ...

  6. 接口测试postman深度挖掘应用③--postman终结篇

    上一章节我们介绍了postman的变量测试以及导入数据测试基本上技术性的东西已经差不过了,这篇文章再系统性的介绍一下. 一.下载 官网:https://www.postman.com 1.选择需要下载 ...

  7. SpringCloud微服务实战——搭建企业级开发框架(四十一):扩展JustAuth+SpringSecurity+Vue实现多租户系统微信扫码、钉钉扫码等第三方登录

      前面我们详细介绍了SSO.OAuth2的定义和实现原理,也举例说明了如何在微服务框架中使用spring-security-oauth2实现单点登录授权服务器和单点登录客户端.目前很多平台都提供了单 ...

  8. 最短路之 Dijkstra 算法

    普通的 Dijkstra 这是一种运用贪心的单源最短路算法,就是求从一个节点出发,到任意一个点的最短距离 首先我们要一个图 假设要求从 1 开始的单源最短路 dis[] 表示最短路数组, vis[] ...

  9. 使用PowerShell压缩和解压ZIP包

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月13日. 解压ZIP包 使用PowerShell的Expand-Archive命令.PowerShell官方文档地址. 命令格式: ...

  10. spring boot用ide新建项目遇到的restcontroller不能导入的问题

    才开始学习spring boot,第一个程序helloworld就碰到@RestController和@RequestMapping(/hello)的注解都会报错的问题. 我个人的解决方法: 1.sp ...