nginx本身就有缓存功能,能够缓存静态对象,比如图片、CSS、JS等内容直接缓存到本地,下次访问相同对象时,直接从缓存即可,无需访问后端静态服务器以及存储存储服务器,当然现在企业中大多数也是以redis作为缓存来使用,所以这里简单记录一下
现在准备一台web服务器,一台nginx代理服务器,现在我们先配制好代理服务器
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;
proxy_cache_path /tmp/nginxcache levels=1:2 keys_zone=my_cache:10m max_size=10g
inactive=60m use_temp_path=off;
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;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_cache my_cache;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 304 302 1d;
proxy_pass http://192.168.50.132;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
在http段里面配置好缓存的一些配置,比如配置存储位置,缓存文件大小等等。然后在location规则配置关于proxy_cache的相关配置。保存好,并reload一下。
把web服务器也配置好,现在我们可以访问一下代理服务器
第一次访问之后会把这些内容缓存起来,然后现在我们把web服务器关闭掉,再次访问网页,会发现依然可以访问。因为现在访问是从缓存里面拿取得数据。这就是缓存的作用。
原文:https://www.cnblogs.com/FengGeBlog/p/13554026.html