round-robin 算法
Syntax: upstream name { ... }
Default: —
Context: http
Syntax: server address [parameters];
Default: —
Context: upstream
ex:
upstream backend {
server backend1.example.com weight=5;
server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
server backup1.example.com backup;
}
- round-robin加权轮训的方式访问server指令中的上游服务
对上游使用keep-alive
proxy_http_version 1.1;
proxy_set_header Connection "";
ip_hash 与hash
```shell script upstream h5 { ip_hash; server 192.168.100.104:9080; server 192.168.100.105:9080; }
ip_hash ipv4 使用前3个字节,ipv6 使用全部字节
Syntax: hash key [consistent]; Default: — Context: upstream
可以使用一致性hash算法
# least_conn
优先选择连接数最少的上游服务
# upstream 模块变量
# proxy

# proxy_pass
```shell script
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
ex:
proxy_pass http://localhost:8000/uri/;
proxy_pass http://unix:/tmp/backend.socket:/uri/;
Syntax: proxy_http_version 1.0 | 1.1;
Default:
proxy_http_version 1.0;
Context: http, server, location
设置HTTP版本号,默认1.0 ,当使用keep-alive时必须设置成1.1
Syntax: proxy_method method;
Default: —
Context: http, server, location
使用的请求方法。POST,GET 等
Syntax: proxy_set_header field value;
Default:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context: http, server, location
ex:
proxy_set_header Host $proxy_host;
设置http 请求头
Syntax: proxy_pass_request_headers on | off;
Default:
proxy_pass_request_headers on;
Context: http, server, location
是否转发http 头
proxy_request_buffering
```shell script Syntax: proxy_request_buffering on | off; Default: proxy_request_buffering on; Context: http, server, location
接收到客户端的body 时,是收到就转发还是接受完后再转发
# 与上游服务建立连接
```shell script
Syntax: proxy_connect_timeout time;
Default:
proxy_connect_timeout 60s;
Context: http, server, location
建立连接的时间限制,超时向客户端返回502
```shell script Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off …; Default: proxy_next_upstream error timeout; Context: http, server, location
在向客户端返回之前,遇到错误更换upstream
```shell script
Syntax: proxy_intercept_errors on | off;
Default:
proxy_intercept_errors off;
Context: http, server, location
当上游服务返回值大于300时,返回error_page 配置值
```shell script Syntax: keepalive connections; Default: — Context: upstream 使用http keepalive
```shell script
Syntax: keepalive_requests number;
Default:
keepalive_requests 100;
Context: upstream
This directive appeared in version 1.15.3.
每个连接最多发送多少请求
接受上游响应
```shell script Syntax: proxy_buffer_size size; Default: proxy_buffer_size 4k|8k; Context: http, server, location http response 中head的最大值
```shell script
Syntax: proxy_buffering on | off;
Default:
proxy_buffering on;
Context: http, server, location
是否接受完整的包体后再转发给客户端
```shell script Syntax: proxy_store_access users:permissions …; Default: proxy_store_access user:rw; Context: http, server, location
上游包体持久化
# 处理上游响应头
```shell script
Syntax: proxy_intercept_errors on | off;
Default:
proxy_intercept_errors off;
Context: http, server, location
禁用上游响应中的HTTP头。
If not disabled, processing of these header fields has the following effect:
“X-Accel-Expires”, “Expires”, “Cache-Control”, “Set-Cookie”, and “Vary” set the parameters of response caching;
“X-Accel-Redirect” performs an internal redirect to the specified URI;
“X-Accel-Limit-Rate” sets the rate limit for transmission of a response to a client;
“X-Accel-Buffering” enables or disables buffering of a response;
“X-Accel-Charset” sets the desired charset of a response.
```shell script Syntax: proxy_hide_header field; Default: — Context: http, server, location
屏蔽掉某些头
# 对上游使用ssl
# 浏览器缓存

# expires 模块
```shell script
Syntax: expires [modified] time;
expires epoch | max | off;
Default:
expires off;
Context: http, server, location, if in location
ex:
expires 24h;
expires modified +24h;
expires @24h;
expires 0;
expires -1;
expires epoch;
expires $expires;
add_header Cache-Control private;
proxy_cache
```shell script Syntax: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [min_free=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; Default: — Context: http
ex: proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
```shell script
Syntax: proxy_cache zone | off;
Default:
proxy_cache off;
Context: http, server, location