• Nginx 静态资源缓存配置


    Nginx上可以缓存一些不常更新的静态资源配置来节约访问带宽.

    没缓存前(不走nginx):

     没缓存(走nginx)

     配置走nginx缓存

    user www www;
    worker_processes 2; #设置值和CPU核心数一致
    error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
    #access_log /usr/local/webserver/nginx/logs/access.log crit;
    pid /usr/local/webserver/nginx/nginx.pid;
    #Specifies the value for maximum file descriptors that can be opened by this process.
    worker_rlimit_nofile 65535;
    events
    {
      use epoll;
      worker_connections 65535;
    }
    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';
      
    #charset gb2312;
         
      server_names_hash_bucket_size 128;
      client_header_buffer_size 32k;
      large_client_header_buffers 4 32k;
      client_max_body_size 8m;
         
      sendfile on;
      tcp_nopush on;
      keepalive_timeout 60;
      tcp_nodelay on;
      fastcgi_connect_timeout 300;
      fastcgi_send_timeout 300;
      fastcgi_read_timeout 300;
      fastcgi_buffer_size 64k;
      fastcgi_buffers 4 64k;
      fastcgi_busy_buffers_size 128k;
      fastcgi_temp_file_write_size 128k;
      gzip on; 
      gzip_min_length 1k;
      gzip_buffers 4 16k;
      gzip_http_version 1.0;
      gzip_comp_level 2;
      gzip_types text/plain application/x-javascript text/css application/xml;
      gzip_vary on;
     
      ##cache##
      proxy_buffer_size 16k;
      proxy_buffers 4 64k;
      proxy_busy_buffers_size 128k;
      proxy_temp_file_write_size 128k;
      proxy_temp_path /usr/local/webserver/nginx/temp;
      proxy_cache_path /usr/local/webserver/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
      ##end##
    
      #limit_zone crawler $binary_remote_addr 10m;
     #下面是server虚拟主机的配置
    
        upstream web_back{  
            server 127.0.0.1:8080; #并且可以分配权重weight,这样来配置集群服务器的访问优先权  
        } 
    
    
     server
      {
        listen 8060;#监听端口
    
        #缓存相应的文件(静态文件)  
        location ~ .*.(gif|jpg|jpeg|png|css|js|ico)$ {  
            proxy_pass http://web_back;         #如果没有缓存则通过proxy_pass转向请求  
            proxy_redirect off;  
            access_log off;# 关闭日志
            proxy_set_header Host $host;  
            proxy_cache cache_one;  
            proxy_cache_valid 200 302 24h;                              #对不同的HTTP状态码设置不同的缓存时间,h小时,d天数  
            proxy_cache_valid 301 1d;  
            proxy_cache_valid any 1m;  
            expires 30d;
            add_header wall "cache-file";
        }  
    
    	#web 使用
    	location /cpeducloud {
                proxy_pass http://localhost:8080/cpeducloud;
    	    proxy_redirect http:// https://;
    	    sendfile off;	
                proxy_set_header   Host             $host:$server_port;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_set_header   Upgrade $http_upgrade;    
                proxy_set_header   Connection "Upgrade";
    	    proxy_max_temp_file_size 0;
    
                #this is the maximum upload size
                client_max_body_size       0;
                client_body_buffer_size    128k;
    
                proxy_connect_timeout      90;
                proxy_send_timeout         180;
                proxy_read_timeout         180;
    
                proxy_temp_file_write_size 64k;
                # Required for new HTTP-based CLI
                proxy_http_version 1.1;
               
    		
        }
    	location /download {
    	    proxy_pass http://localhost:8080/cpeducloud;
    	    proxy_redirect http:// https://;
    		
    		#下载速度限制
    		#limit_rate_after 10m; 
    		limit_rate 5k;
    	}
    	
    	access_log logs/cpeducloud.log main;
       
      }
      
    }
    

      现在第一次请求会放到缓存中,第二次就直接走的缓存

     看请求的时间,节约下来了很多

     可以看出效果还是比较明显的

  • 相关阅读:
    Reactive(1) 从响应式编程到"好莱坞"
    [动图演示]Redis 持久化 RDB/AOF 详解与实践
    补习系列(22)-全面解读 Spring Profile 的用法
    Android手机打造你的Python&Java开发工具!
    人工神经网络模型种类
    最小二乘拟合
    LDA主体模型
    Logistic Regression求解classification问题
    batch gradient descent(批量梯度下降) 和 stochastic gradient descent(随机梯度下降)
    SVM实验
  • 原文地址:https://www.cnblogs.com/sunxun/p/14097118.html
Copyright © 2020-2023  润新知