http_proxy

proxy_pass

| Syntax: | proxy_pass URL;

| | —- | —- | | Default: | — | | Context: | location, if in location, limit_except

|

proxy_set_header

| Syntax: | proxy_set_header field value;

| | —- | —- | | Default: | proxy_set_header Host $proxy_host;
proxy_set_header Connection close; | | Context: | http, server, location |

proxy_redirect

| Syntax: | proxy_redirect default;
proxy_redirect off;
proxy_redirect redirect replacement;

| | —- | —- | | Default: | proxy_redirect default; | | Context: | http, server, location |

设置代理响应的 Location 和 Refresh

default
将 location 的 uri 变量作为 replacement
将 proxy_pass 变量作为 redirect 替换
off
关闭此功能

proxy_buffering

| Syntax: | proxy_buffering on | off;

| | —- | —- | | Default: | proxy_buffering on; | | Context: | http, server, location |

开启/关闭 服务端响应的缓冲区

proxy_buffers

| Syntax: | proxy_buffers number size;

| | —- | —- | | Default: | proxy_buffers 8 4k|8k; | | Context: | http, server, location |

对于服务端的一个响应,单个连接 的缓冲 个数 和 大小
总大小就是 number * size

proxy_buffer_size

| Syntax: | proxy_buffer_size size;

| | —- | —- | | Default: | proxy_buffer_size 4k|8k; | | Context: | http, server, location |

从服务端获取的第一部分响应数据的大小

proxy_busy_buffers_size

| Syntax: | proxy_busy_buffers_size size;

| | —- | —- | | Default: | proxy_busy_buffers_size 8k|16k; | | Context: | http, server, location |

proxy_temp_path

| Syntax: | proxy_temp_path path [level1 [level2 [level3]]];

| | —- | —- | | Default: | proxy_temp_path proxy_temp; | | Context: | http, server, location |

缓冲区满后,仍未被nginx处理的响应数据临时文件的存放目录

proxy_temp_file_write_size

| Syntax: | proxy_temp_file_write_size size;

| | —- | —- | | Default: | proxy_temp_file_write_size 8k|16k; | | Context: | http, server, location |

临时缓冲文件的大小

注意点

proxy_pass 末尾加不加 /

location /server {
proxy_pass http://x.x.x.x; # 无 / http://x.x.x.x/server/index.html
proxy_pass http://x.x.x.x/; # 有 / http://x.x.x.x/index.html
}

如果是 localtion / { } , 结尾有没有 / 都一样

获取代理端的 http请求头字段

代理端
location / {
proxypass http://x.x.x.x;
proxy_set_header aaa 123456;
}
后端
default_type text/plain;
return 200 “$http_aaa”; # 以 $http
头变量名 获取

缓存

流程

把 url及相关组合 当作key ,使用 32 位md5 进行哈希 , 将内容存储到缓存目录中

优缺点

优
减少数据传输,节省网络流量,加快响应速度,提升用户体验
减轻服务端压力
提高部分可用性

缺
数据不一致
增加成本

proxy_cache_path

| Syntax: | proxy_cache_path path [levels=levels] [usetemp_path=on|off] keys_zone=_name:size [inactive=time] [maxsize=_size] [minfree=_size] [managerfiles=_number] [managersleep=_time] [managerthreshold=_time] [loaderfiles=_number] [loadersleep=_time] [loaderthreshold=_time] [purger=on|off] [purgerfiles=_number] [purgersleep=_time] [purgerthreshold=_time];

| | —- | —- | | Default: | — | | Context: | http |

path 指定缓存目录
levels 指定缓存目录层级,最多三层,每层取值为 1 或 2
image.png
MD5值 为 b7f54b2df7773722d382f4809d65029c
levels=1:2 两层目录,一层 1个字母 , 二层 2个字母
path/c/29/b7f54b2df7773722d382f4809d65029c
levels=2:1:2 path/9c/2/50/b7f54b2df7773722d382f4809d65029c
levels=2:2:2 path/9c/02/65/b7f54b2df7773722d382f4809d65029c
keys_zone 缓存区名称及大小
keys_zone=z1:200m 缓存区名为 z1 ,大小 200m, 1m大约存储 8000 key
inactive 缓存数据多久没有访问就删除
max_size 缓存空间最大值,存满会覆盖缓冲时间最长的资源

proxy_cache

| Syntax: | proxy_cache zone | off;

| | —- | —- | | Default: | proxy_cache off; | | Context: | http, server, location |

启用指定缓存区名字
proxy_cache_path 中的 keys_zone

proxy_cache_key

| Syntax: | proxy_cache_key string;

| | —- | —- | | Default: | proxy_cache_key $scheme$proxy_host$request_uri; | | Context: | http, server, location |

定义缓存的 key , nginx 根据此 key 的 MD5 值哈希缓存

proxy_cache_valid

| Syntax: | proxy_cache_valid [code …] time;

| | —- | —- | | Default: | — | | Context: | http, server, location |

不同的响应码设置不同的缓存时间
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid 5m; # 不指定响应码只缓存 200, 301, 302
proxy_cache_valid any 1m; # any 代指所有响应码

proxy_cache_min_uses

| Syntax: | proxy_cache_min_uses number;

| | —- | —- | | Default: | proxy_cache_min_uses 1; | | Context: | http, server, location |

访问多少次 会缓存

proxy_cache_methods

| Syntax: | proxy_cache_methods GET | HEAD | POST …;

| | —- | —- | | Default: | proxy_cache_methods GET HEAD; | | Context: | http, server, location |

哪些 http 方法 会缓存

示例

proxy_cache_path /tmp/proxy_cache levels=2:1:2 keys_zone=z1:200m inactive=1d max_size=2g;
proxy_cache z1;
proxy_cache_valid any 10m;
proxy_cache_min_uses 5;
proxy_cache_key k1;
add_header nginx-cache “$upstream_cache_status”;

ngx_cache_purge

server {
location ~ /clean(/.*) {
proxy_cache_purge z1 k1; # 确保 zone_name key 一致
}

http://xxx/clean/xxx

不缓存

proxy_no_cache

| Syntax: | proxy_no_cache string …;

| | —- | —- | | Default: | — | | Context: | http, server, location |

条件不为空或者不为0 的 不缓存
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;

proxy_cache_bypass

| Syntax: | proxy_cache_bypass string …;

| | —- | —- | | Default: | — | | Context: | http, server, location |

条件不为空或者不为0 的 不从缓存中获取数据
proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
image.png

相关变量

$cookie_nocache
请求中的 cookie 中的 nocache 的值
$arg_nocache
请求参数中的 nocache 和 comment 的值

if ($request_uri ~ .*.png) {
set $nocache 1;
}