若是要熟練使用nginx來實現文件的緩存,那下面的幾個指令你必需要牢記於心php
做用:設置緩存數據的相關信息 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 值: path:緩存目錄的位置 levels:指定使用幾級緩存目錄 keys_zone:指定緩存區域的名稱和緩存空間的大小 例子: proxy_cache_path /data/nginx/cache levels=1:4 keys_zone=mycache:10m; 說明 1:表示一級目錄能夠由1個字符來構成 4:表示二級目錄能夠由4個字符來構成 mycache:是這個緩存區域的名稱 10m:能夠緩存10M大小的數據 緩存結果 /data/nginx/cache/c/29ad/b7f54b2df7773722d382f4809d65029c 說明 /data/nginx/cache/:這裏是緩存目錄 c:由於一級目錄能夠由1個字符構成,全部這裏隨機出現一個c 29ad:二級目錄由4個隨機字符構成 b7f54b2df7773722d382f4809d65029c:緩存的數據
做用:調用緩存 Syntax: proxy_cache zone | off; Default: proxy_cache off; Context: http, server, location 注意: 該指令寫在不一樣的位置,緩存數據對象也不一樣
做用:指定一個文件至少須要被用戶訪問多少次之後,纔會被緩存,默認1 Syntax: proxy_cache_min_uses number; Default: proxy_cache_min_uses 1; Context: http, server, location
Syntax: proxy_cache_purge string ...; Default: — Context: http, server, location 使用場景:上游服務器中的資源發生了更改,可是緩存中的數據還沒有過去,這個時候就須要手動執行purge讓緩存中的數據過去 使用舉例: http { proxy_cache_path /data/nginx/cache levels=1:4 keys_zone=mycache:10m; server { listen 10.220.5.196:80; location / { proxy_pass http://10.220.5.180:80: proxy_cache mycache; .... .... } location = /cleanCache { allow= deny= proxy_cache_purge mycache; #這裏須要指定上面定義的緩存名稱 ... ... ... } } }
做用:定義緩存數據的有效期 Syntax: proxy_cache_valid [code ...] time; Default: — Context: http, server, location 例子: proxy_cache_valid 200 302 10m; proxy_cache_valid 301 1h; proxy_cache_valid any 1m;
做用:指定緩存的key的名稱 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 "$uri"
centos7.5html
NGINX服務器端IP:172.20.10.8/28node
HTTPD服務器端IP:172.20.10.7/28nginx
HTTPD服務器端IP:172.20.10.9/28web
客戶端IP:172.20.10.4/28vim
使用yum下載nginx須要使用網絡yum源,複製下面的代碼到你的yum倉庫便可下載centos
[ken] name=ken enabled=1 gpgcheck=0 baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/
下載nginx瀏覽器
[root@ken ~]# yum install nginx -y
配置nginx文件緩存
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. #include /etc/nginx/conf.d/*.conf; proxy_cache_path /ken levels=1:2 keys_zone=kenken:100m; add_header host $server_addr; add_header cachestatus $upstream_cache_status; #添加本條能夠在瀏覽器中看到是否命中緩存 server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /var/www/html; # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; location / { proxy_pass http://172.20.10.7:80; proxy_set_header host $host; proxy_set_header realip $remote_addr; proxy_cache kenken; proxy_cache_min_uses 3; proxy_cache_valid any 10m; } } }
建立緩存目錄服務器
[root@ken ~]# mkdir /ken
更改緩存目錄的屬主和屬組
[root@ken ~]# chown -R nginx.nginx /ken
啓動nignx
[root@ken ~]# systemctl start nginx [root@ken ~]# ss -tnl | grep 80 LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::*
下載httpd
[root@ken ~]# yum install httpd -y
準備測試文件
[root@ken ~]# echo "this is 172.20.10.7 for test">>/var/www/html/index.html
啓動httpd
[root@ken ~]# systemctl restart httpd
輸入nginx服務器端的IP地址
輸入172.20.10.8的地址成功訪問172.20.10.7的頁面
查看web服務器端的訪問日誌
[root@ken ~]# tail -f /var/log/httpd/access_log 172.20.10.8 - - [02/Oct/2018:22:40:43 +0800] "GET / HTTP/1.0" 200 29 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"
成功捕捉到來自172.20.10.8的訪問請求
咱們去查看nginx服務端是否已經有緩存產生
[root@ken /]# ls /ken/e/55/58be92261b4ffa2c4fe7e92be2f0255e
測試成功!
在nginx服務器端已經產生了緩存,再次刷新瀏覽器界面,在web服務器端都不會再產生訪問日誌,由於如今客戶請求是直接從緩存提取的,沒有再日後方節點來訪問文件,這樣能夠大大提升網站的負載和併發能力。
在完成了上面的靜態文件緩存以後,相信動態文件的緩存對你來講也是垂手可得了,下面咱們一氣呵成完成對動態文件的緩存吧。
centos7.5
NGINX服務器端IP:172.20.10.8/28
WEB服務器端IP:172.20.10.9/28
下載php
[root@ken ~]# yum install php php-fpm -y
配置nginx文件
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. #include /etc/nginx/conf.d/*.conf; fastcgi_cache_path /kenken levels=1:2 keys_zone=kenken:100m; add_header host $server_addr; add_header cachestatus $upstream_cache_status; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /var/www/html; index index.php; # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; location ~^/.*(\.php)$ { fastcgi_pass 172.20.10.9:9000; fastcgi_index index.php; include fastcgi.conf; fastcgi_cache kenken; fastcgi_cache_valid any 10m; fastcgi_cache_key $request_uri; } } }
建立緩存目錄,並修改權限
[root@ken ~]# mkdir /kenken
[root@ken ~]# chown -R nginx.nginx /kenken
nginx語法檢測
[root@ken ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
啓動nginx
[root@ken ~]# systemctl restart nginx
下載所需服務程序
[root@ken ~]# yum install httpd php-fpm php -y
配置php-fpm
[root@ken ~]# vim /etc/php-fpm.d/www.conf ... 10 ; '/path/to/unix/socket' - to listen on a unix socket. 11 ; Note: This value is mandatory. 12 listen = 172.20.10.9:9000 #修改成本機ip地址 13 14 ; Set listen(2) backlog. A value of '-1' means unlimited. 15 ; Default Value: -1 16 ;listen.backlog = -1 17 18 ; List of ipv4 addresses of FastCGI clients which are allowed to connect. 19 ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original 20 ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address 21 ; must be separated by a comma. If this value is left blank, connections will be 22 ; accepted from any ip address. 23 ; Default Value: any 24 listen.allowed_clients = 172.20.10.8 #修改成nginx服務端地址 ...
啓動php-fpm
[root@ken ~]# systemctl restart php-fpm [root@ken ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 172.20.10.9:9000 *:*
準備動態測試文件
[root@ken ~]# cd /var/www/html/ [root@ken html]# ls index.html [root@ken html]# vim index.php <?php phpinfo(); ?>
重啓nginx
[root@ken ~]# systemctl restart nginx
瀏覽器輸入nginx服務端ip地址進行測試
訪問成功,刷新幾回查看nginx是否已經產生緩存
[root@ken ~]# ls /kenken/ 1/ b/ e/ [root@ken ~]# ls /kenken/b/fe/c86156f7dcfecf44876ca30d1bac7feb
動態文件緩存成功!