龙之介大人

Nginx反向代理与缓存应用
Nginx 反向代理反向代理示例:#node1节点 server { listen ...
扫描右侧二维码阅读全文
27
2020/02

Nginx反向代理与缓存应用

Nginx 反向代理

  • 反向代理示例:
#node1节点
server {
    listen              80;
    server_name         node1.ngx.com;
    index index.html default.html;
    location / {
        root /www;
    }
    access_log          /www/log/ngx.com.access.log main;
}

#node2节点
server {
    listen              80;
    server_name         node2.ngx.com;
    index index.html default.html;
    location / {
        root /www;
    }
    access_log          /www/log/ngx.com.access.log main;
}

#调度节点配置
upstream nodes {
    server node2.ngx.com;
    server node1.ngx.com;
}
location / {
    proxy_pass http://nodes;
    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_connect_timeout   60s; #请求延迟
}

代理缓存

  • 缓存配置参数:
## 缓存路径
Syntax:    proxy_cache    zone    |    off;
Default:    proxy_cache    off;
Context:    http,    server,    location

Syntax:    proxy_cache_path    path    [levels=levels]
[use_temp_path=on|off]    keys_zone=name:size    [inactive=time]
[max_size=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

## 缓存过期周期
Syntax:    proxy_cache_valid    [code    ...]    time;
Default:    —
Context:    http,    server,    location
#示例例
proxy_cache_valid    200 302 10m; 
proxy_cache_valid    404 1m;

## 缓存维度
Syntax:    proxy_cache_key    string;
Default:                proxy_cache_key    $scheme$proxy_host$request_uri;
Context:    http,    server,    location
#示例
proxy_cache_key    "$host$request_uri    $cookie_user";
proxy_cache_key    $scheme$proxy_host$uri$is_args$args;
详细参数:http://nginx.org/en/docs/http/ngx_http_proxy_module.html
  • 配置示例
#缓存配置
proxy_cache_path        /cache/nginx/ levels=1:2 keys_zone=cache_zone:128m max_size=256m inactive=12h;

upstream nodes {
    server node2.ngx.com;
    server node1.ngx.com;
}

location / {
    proxy_pass http://nodes;
    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_connect_timeout   60s;
    proxy_cache             cache_zone;
    proxy_cache_valid       200 1h;
    proxy_cache_valid       301 302 10m;
    proxy_cache_valid       any 1m;
    proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
    add_header              Nginx-Cache     "$upstream_cache_status";
}

#测试
[root@study1 ~]# curl 10.10.1.109 -I
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 27 Feb 2020 06:15:42 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 9
Connection: keep-alive
Last-Modified: Thu, 27 Feb 2020 03:22:16 GMT
ETag: "5e5735e8-9"
PassNodeName: web-service-2
Nginx-Cache: MISS    #无缓存
Accept-Ranges: bytes

[root@study1 ~]# curl 10.10.1.109 -I
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 27 Feb 2020 06:15:43 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 9
Connection: keep-alive
Last-Modified: Thu, 27 Feb 2020 03:22:16 GMT
ETag: "5e5735e8-9"
PassNodeName: web-service-2
Nginx-Cache: HIT  有缓存
Accept-Ranges: bytes

#缓存目录
[root@study1 ~]# ll /cache/nginx/8/d6/
总用量 4
-rw------- 1 nginx nginx 638 2月  27 14:15 2737ba071ddd27c07ed36e5a155a7d68

最后修改:2020 年 02 月 27 日 02 : 28 PM

发表评论