• 重识Nginx - 11 使用ngx_http_proxy_module的proxy_cache搭建一个具备缓存功能的反向代理服务



    在这里插入图片描述


    官网说明 ngx_http_proxy_module

    https://nginx.org/en/docs/http/ngx_http_proxy_module.html

    在这里插入图片描述

    proxy_cache_path

    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
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    在这里插入图片描述


    在这里插入图片描述

    proxy_cache_key

    Syntax:	proxy_cache_key string;
    Default:	
    proxy_cache_key $scheme$proxy_host$request_uri;
    Context:	http, server, location
    
    • 1
    • 2
    • 3
    • 4

    Defines a key for caching, for example

    proxy_cache_key "$host$request_uri $cookie_user";
    
    • 1

    By default, the directive’s value is close to the string

    proxy_cache_key $scheme$proxy_host$uri$is_args$args;
    
    • 1

    proxy_cache_valid

    Syntax:	proxy_cache_valid [code ...] time;
    Default:	—
    Context:	http, server, location
    
    • 1
    • 2
    • 3

    Sets caching time for different response codes. For example, the following directives

    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404      1m;
    
    • 1
    • 2

    set 10 minutes of caching for responses with codes 200 and 302 and 1 minute for responses with code 404.

    If only caching time is specified

    proxy_cache_valid 5m;
    
    • 1

    then only 200, 301, and 302 responses are cached.


    In addition, the any parameter can be specified to cache any responses:

    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 301      1h;
    proxy_cache_valid any      1m;
    
    • 1
    • 2
    • 3

    Parameters of caching can also be set directly in the response header. This has higher priority than setting of caching time using the directive.

    • The “X-Accel-Expires” header field sets caching time of a response in seconds. The zero value disables caching for a response. If the value starts with the @ prefix, it sets an absolute time in seconds since Epoch, up to which the response may be cached.
    • If the header does not include the “X-Accel-Expires” field, parameters of caching may be set in the header fields “Expires” or “Cache-Control”.
    • If the header includes the “Set-Cookie” field, such a response will not be cached.
    • If the header includes the “Vary” field with the special value “*”, such a response will not be cached (1.7.7). If the header includes the “Vary” field with another value, such a response will be cached taking into account the corresponding request header fields (1.7.7).

    Processing of one or more of these response header fields can be disabled using the proxy_ignore_headers directive.

    在这里插入图片描述


    其他参数 按需

    在这里插入图片描述


    实操

    环境

    nginx 8888 作为反向代理 , 转发到 后端服务 9999 (用nginx模拟)

    在这里插入图片描述


    nginx 8888 反向代理配置

    
    [root@VM-0-7-centos conf]# cat nginx.conf
    
    user  root;
    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
    
        proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
    
        sendfile        on;
       
        keepalive_timeout  65;
    
        gzip  on;
        gzip_min_length 20;
        gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        
    
        upstream local {
           server 127.0.0.1:9999;  # 用nginx 模拟 后端的服务  
        }
    
        server {
            listen   8888;
            server_name  artisan.nginx.pub;
    
           
    
            location / {
               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_cache my_cache;
               proxy_cache_key $host$uri$is_args$args;
               proxy_cache_valid 200 304 302 1d;
    
               proxy_pass http://local;
            }
    
    
         
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    重点看

        proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
    
    • 1

      proxy_cache my_cache;
      proxy_cache_key $host$uri$is_args$args;
      proxy_cache_valid 200 304 302 1d;
    
    • 1
    • 2
    • 3

    重启服务

    [root@VM-0-7-centos artisan_ng]# pwd
    /root/ng/artisan_ng
    
    [root@VM-0-7-centos artisan_ng]# ./sbin/nginx -c ./conf/nginx.conf
    [root@VM-0-7-centos artisan_ng]#
    [root@VM-0-7-centos artisan_ng]# ps -ef|grep nginx
    root      841577       1  0 11:26 ?        00:00:00 nginx: master process ./sbin/nginx -c ./conf/nginx.conf
    root      841578  841577  0 11:26 ?        00:00:00 nginx: worker process
    root      841579  841577  0 11:26 ?        00:00:00 nginx: cache manager process
    root      841580  841577  0 11:26 ?        00:00:00 nginx: cache loader process
    root      841605  841385  0 11:26 pts/5    00:00:00 grep --color=auto nginx
    [root@VM-0-7-centos artisan_ng]#
    [root@VM-0-7-centos artisan_ng]#
    
    [root@VM-0-7-centos artisan_ng]#
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    查看nginx的进程,会发现 多了 两个进程 nginx: cache managernginx: cache loader


    nginx 9999 配置 (Mock后端服务)

    user  root;
    worker_processes  1;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
    
    
        sendfile        on;
    
        keepalive_timeout  65;
    
        gzip on;
        gzip_min_length 20;
        gzip_types *;
    
    
        server {
            listen    127.0.0.1:9999;  # 仅允许本地访问
            #listen     9999;
            server_name  localhost;
    
    
            location / {
               alias  document/;  # 模拟静态资源文件 
    
            }
      
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    重启 nginx

    [root@VM-0-7-centos ng]# cd  artisan_ng_backserver/
    [root@VM-0-7-centos artisan_ng_backserver]# ll
    total 40
    drwxr-xr-x 2 root root 4096 Oct  4 10:12 client_body_temp
    drwxr-xr-x 2 root root 4096 Oct  4 10:43 conf
    drwxr-xr-x 3 root root 4096 Oct  4 10:12 document
    drwxr-xr-x 2 root root 4096 Oct  4 10:12 fastcgi_temp
    drwxr-xr-x 2 root root 4096 Oct  4 10:12 html
    drwxr-xr-x 2 root root 4096 Oct  4 10:12 logs
    drwxr-xr-x 2 root root 4096 Oct  4 10:12 proxy_temp
    drwxr-xr-x 7 root root 4096 Oct  4 10:12 sbin
    drwxr-xr-x 2 root root 4096 Oct  4 10:12 scgi_temp
    drwxr-xr-x 2 root root 4096 Oct  4 10:12 uwsgi_temp
    [root@VM-0-7-centos artisan_ng_backserver]# ./sbin/nginx -c /root/ng/artisan_ng_backserver/conf/nginx.conf
    [root@VM-0-7-centos artisan_ng_backserver]#
    [root@VM-0-7-centos artisan_ng_backserver]#
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    验证

    
    
    [root@VM-0-7-centos tmp]# /root/ng/artisan_ng/sbin/nginx -c /root/ng/artisan_ng/conf/nginx.conf
    [root@VM-0-7-centos tmp]#
    [root@VM-0-7-centos tmp]#
    [root@VM-0-7-centos tmp]# /root/ng/artisan_ng_backserver/sbin/nginx -c /root/ng/artisan_ng_backserver/conf/nginx.conf
    [root@VM-0-7-centos tmp]#
    [root@VM-0-7-centos tmp]#
    [root@VM-0-7-centos tmp]#
    [root@VM-0-7-centos tmp]# ps -ef|grep nginx
    root      847458       1  0 12:04 ?        00:00:00 nginx: master process /root/ng/artisan_ng/sbin/nginx -c /root/ng/artisan_ng/conf/nginx.conf
    root      847459  847458  0 12:04 ?        00:00:00 nginx: worker process
    root      847460  847458  0 12:04 ?        00:00:00 nginx: cache manager process
    root      847461  847458  0 12:04 ?        00:00:00 nginx: cache loader process
    root      847512       1  0 12:05 ?        00:00:00 nginx: master process /root/ng/artisan_ng_backserver/sbin/nginx -c /root/ng/artisan_ng_backserver/conf/nginx.conf
    root      847513  847512  0 12:05 ?        00:00:00 nginx: worker process
    root      847533  841385  0 12:05 pts/5    00:00:00 grep --color=auto nginx
    [root@VM-0-7-centos tmp]#
    [root@VM-0-7-centos tmp]#
    [root@VM-0-7-centos tmp]# ll
    total 4
    drwx------ 2 root root 4096 Oct  4 12:04 nginxcache
    -rw-r--r-- 1 root root    0 Oct  4 12:04 stargate.lock
    [root@VM-0-7-centos tmp]#
    [root@VM-0-7-centos tmp]#
    [root@VM-0-7-centos tmp]#  启动后 就有 nginxcache 目录了,只不过为空  
    [root@VM-0-7-centos tmp]# cd nginxcache/
    [root@VM-0-7-centos nginxcache]# ll
    total 0
    [root@VM-0-7-centos nginxcache]#
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    我们看到 2个 进程都起好了 .

    访问 http://ip:8888/a.html

    在这里插入图片描述

    查看/tmp/nginxcache目录

    
    [root@VM-0-7-centos nginxcache]# ll
    total 4
    drwx------ 3 root root 4096 Oct  4 12:05 b
    [root@VM-0-7-centos nginxcache]#
    
    [root@VM-0-7-centos nginxcache]# tree b/
    b/
    └── d0
        └── a2621ddcb53455c06f238e9b4e5a1d0b
    
    1 directory, 1 file
    [root@VM-0-7-centos nginxcache]#
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    停掉 9999 后台服务

    
    [root@VM-0-7-centos nginxcache]# netstat -anp|grep 9999
    tcp        0      0 127.0.0.1:9999          0.0.0.0:*               LISTEN      847512/nginx: maste
    tcp        0      0 127.0.0.1:9999          127.0.0.1:45968         TIME_WAIT   -
    [root@VM-0-7-centos nginxcache]# kill  847512
    [root@VM-0-7-centos nginxcache]#
    [root@VM-0-7-centos nginxcache]#
    [root@VM-0-7-centos nginxcache]#
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    再此访问 http://ip:8888/a.html

    在这里插入图片描述

    如果删掉 b 这个目录呢 (后台服务也是停掉的)

    在这里插入图片描述

    再次启动9999 ,一切正常了,又重新缓存了

    
    [root@VM-0-7-centos nginxcache]# /root/ng/artisan_ng_backserver/sbin/nginx -c /root/ng/artisan_ng_backserver/conf/nginx.conf
    [root@VM-0-7-centos nginxcache]#
    [root@VM-0-7-centos nginxcache]#
    [root@VM-0-7-centos nginxcache]# netstat -anp|grep 9999
    tcp        0      0 127.0.0.1:9999          0.0.0.0:*               LISTEN      848053/nginx: maste
    [root@VM-0-7-centos nginxcache]#
    
    
    
    
    [root@VM-0-7-centos nginxcache]# ll
    total 4
    drwx------ 3 root root 4096 Oct  4 12:09 b
    [root@VM-0-7-centos nginxcache]#
    [root@VM-0-7-centos nginxcache]#
    [root@VM-0-7-centos nginxcache]#
    [root@VM-0-7-centos nginxcache]#
    [root@VM-0-7-centos nginxcache]# tree b/
    b/
    └── d0
        └── a2621ddcb53455c06f238e9b4e5a1d0b
    
    1 directory, 1 file
    [root@VM-0-7-centos nginxcache]#
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    又重新缓存了。

    在这里插入图片描述

  • 相关阅读:
    在java中类的继承原则有哪些?
    基于simulink的微电网虚拟同步发电机vsg控制系统仿真
    前端(十七)——gitee上开源一个移动端礼盒商城项目(前端+后台)
    封装了一个中间放大效果的iOS轮播视图
    静态变量及静态方法讲解
    linux常见命令以及jdk,tomcat环境搭建
    49-OpenCv深入分析轮廓
    印度政府发布 2022 年数字个人数据保护法案草案
    web前端期末大作业:JavaScript大作业——福五鼠动漫网页制作(6页)带轮播图效果 学生个人单页面网页作业 学生网页设计成品 静态HTML网页单页制作
    第九章 配置数据库(一)
  • 原文地址:https://blog.csdn.net/yangshangwei/article/details/127157362