nginx (四) Nginx緩存

Nginx緩存

緩存過期緩存文件刪除(inactive)

Nginx在接收代理服務器的數據後,會根據cache的配置將這些數據緩存到本地硬盤。當客戶端下次訪問相同的數據,Nginx服務器將直接從硬盤檢索到響應的數據返回給用戶,從而減少與被代理服務器交互的時間。
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

附上自己的 nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8388;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
	   #指定緩存位置、緩存名稱、內存中緩存內容元數據信息大小限制、緩存總大小限制。緩存位置是一個目錄應該先創建好,nginx並不會幫我們創建這個緩存目錄
	   #loader_threshold - 迭代的持續時間,以毫秒爲單位(默認爲200) 
	   #loader_files - 在一次迭代期間加載的最大項目數(默認爲100)  
	   #loader_sleeps - 迭代之間的延遲(以毫秒爲單位)(默認爲50)  
	   #max_size-設置緩存數據上限
	   #purger=on -該參數表示永久的遍歷所有緩存條目,並刪除與通配符相匹配的條目
	   #inactive=5m inactive的時間表示一個文件在指定時間內沒有被訪問過,就從存儲系統中移除,不管你proxy_cache_valid裏設置的時間是多少
	   proxy_cache_path D://windownginx/nginx-1.17.9/conf/cache  keys_zone=one:10m  inactive=5m  max_size=10g loader_threshold=300 loader_files=200;
	   
	   #設定負載均衡服務器列表
		upstream proxy.server{
			#後端服務器訪問規則
			#ip_hash;
			#weight參數表示權重值,權值越高被分配到的機率越大
			#server 10.11.12.116:80 weight=5;
			#PC_Local
			#server 10.11.12.116:80;
			#PC_Server
			#server 10.11.12.112:80;
			#Notebook
			#server 10.11.12.106:80;
			server 127.0.0.1:9097;
		}
    
		server {
			listen       9999 ssl;
			#攔截域名以abc開頭的  比如 https://abc.com:9999/mysql/getUserData
			server_name abc.*;
            # server_name  LJ.com;
			#緩存的名稱 和proxy_cache_path 中one一樣
			proxy_cache  one;
			ssl_certificate     test.crt; #這個是證書的crt文件所在目錄
			ssl_certificate_key  test.key; #這個是證書key文件所在目錄

			ssl_session_cache    shared:SSL:1m;
			ssl_session_timeout  5m;

			ssl_ciphers  HIGH:!aNULL:!MD5;
			ssl_prefer_server_ciphers  on;

			location / {
				#root   html;
				#index  index.html index.htm;
				
				#與upstream關聯,定義轉發後端負載服務器組
				proxy_pass http://proxy.server;
				  
				#固定寫法-------------
				#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
				#proxy_set_header X-Forwarded-Proto $scheme;
				#proxy_set_header X-Forwarded-Port $server_port;
				
				#緩存自定義key
				proxy_cache_key “hosthosthostrequest_uri$cookie_user”;
				
				#指定請求至少被髮送了多少次以上時才緩存,可以防止低頻請求被緩存
				proxy_cache_min_uses 5;
				
				#指定哪些方法的請求被緩存
				proxy_cache_methods GET HEAD POST;
				
				#響應狀態碼爲200 302時,緩存2分鐘有效,兩分鐘內請求不管請求變沒變數據任然是之前的   
				#proxy_cache_valid any 5m;如果想爲所有狀態碼定義相同緩存時間,就可以使用any作爲第一個參數
				proxy_cache_valid 200 302 2m;
			}
		}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章