Nginx 静态资源缓存配置

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

 

 

没缓存前(不走nginx):

 

 

 没缓存(走nginx)

 

 配置走nginx缓存

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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;
    
  }
   
}

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

 

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

 

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

posted on   手撕高达的村长  阅读(2291)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2015-12-07 PHP 发邮件不换行

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示