Cache
https://xie.infoq.cn/article/373f75d884a28aff8f32c3d9a
什么是缓存?
缓存是将请求的结果存储在与原始存储位置或临时存储位置不同的位置的过程,这样我们就可以避免重复执行相同的操作。基本上,缓存是文件和数据的临时存储,从这个新位置访问数据会更快。
例子
Nginx architect
https://www.zhihu.com/people/ji-fo-74
在Nginx架构中,本身的 proxy cache 和 memcache 为缓存组件。
- Nginx模块结构:
Nginx - browser cache for static file
https://stackoverflow.com/questions/19515132/Nginx-cache-static-files
通过http报文头,指令客户端缓存30天。
## All static files will be served directly. location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|woff2|svg)$ { access_log off; expires 30d; add_header Cache-Control public; ## No need to bleed constant updates. Send the all shebang in one ## fell swoop. tcp_nodelay off; ## Set the OS file cache. open_file_cache max=3000 inactive=120s; open_file_cache_valid 45s; open_file_cache_min_uses 2; open_file_cache_errors off; }
Nginx - proxy cache for static file
https://docs.Nginx.com/Nginx/admin-guide/content-cache/content-caching/
代理缓存规则
http { # ... proxy_cache_path /data/Nginx/cache keys_zone=one:10m; server { proxy_cache mycache; location / { proxy_pass http://localhost:8000; } } }
https://developpaper.com/using-Nginx-to-cache-static-files-on-the-server/
server { listen 80 default_server; server_name localhost; root /mnt/blog/; location / { } #To cache the suffix of a file, you can set it below. location ~ .*.(gif|jpg|png|css|js)(.*) { proxy_ pass http://ip Address: 90; proxy_redirect off; proxy_set_header Host $host; proxy_cache cache_one; proxy_cache_valid 200 302 24h; proxy_cache_valid 301 30d; proxy_cache_valid any 5m; expires 90d; add_header wall "hey!guys!give me a star."; } }
Nginx - proxy for static file
https://belvg.com/blog/static-content-caching-with-Nginx.html
静态文件特殊响应,
server { listen 80; server_name www.example.com; root /var/www/html/example.com/; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $host; location ~* .(ico|jpg|png|gif|jpeg|css|swf|js|woff)$ { access_log off; gzip_static on; gzip_comp_level 5; expires 1M; add_header Cache-Control private; # add_header Cache-Control public; try_files $uri @proxy; } location @proxy { proxy_pass http://www.example.com:8080; } location ~ { proxy_pass http://www.example.com:8080; } }
Nginx memcache for URL - response
https://Nginx.org/en/docs/http/ngx_http_memcached_module.html
先向缓存中查询response, 如果缓存中不存在, 则向后台请求。
server { location / { set $memcached_key "$uri?$args"; memcached_pass host:11211; error_page 404 502 504 = @fallback; } location @fallback { proxy_pass http://backend; } }
https://juejin.cn/post/6844903556013785096
主要使用了
Nginx
的memcached_module
模块,直接从Memcache
服务器中读取并输出。如若不存在,则执行相应程序,并将结果写入
Memcahce
。结构图:
主要流程是:
用户的请求进来,
Nginx
向Memcache
获取数据,如若成功,则直接返回给客服端。如若失败,则Nginx
会报not found
错误,这个时候,需要rewirte
执行相关应用程序,在页面渲染结束后,将结果写入Memcache
。那么下次请求,将直接从Memcache
获取。
upstream memcacheds { server 127.0.0.1:11211; } server { ... # 这里的配置不变 location @rewrite { rewrite ^/(.*)$ /index.PHP?/$1 last; } set $memcached_key 0; # 初始化一下$memcached_key location ~ /(articles) { set $memcached_key $host$uri; # 用url作为标识 add_header X-IMJCW-Key $memcached_key; # 加到header里,方便管理 default_type text/html; memcached_connect_timeout 1s; memcached_read_timeout 2s; memcached_send_timeout 2s; memcached_pass memcacheds; memcached_gzip_flag 2; error_page 404 502 504 = @rewrite; } ... # 这里的配置不变 location ~ \.PHP$ { ... # 这里的配置不变 fastcgi_param X-MEMCACHE-KEY $memcached_key; # 设置参数,为程序是否需要缓存页面做判断 ... # 这里的配置不变 } ... # 这里的配置不变 }
https://learnku.com/articles/42113
memcached_pass 直接指明 host
server{ location / { set $memcached_key "$uri"; #192.168.1.200/1.html 会把 /1.html 当作 key 去判断 mem中有没有缓存。 memcached_pass 127.0.0.1:11211; error_page 404 /callback.PHP;#捕捉 404 信息 ,就回调 callback 页面,在根目录 html 目录下 } }
Nginx dynamic route based on redis
https://openresty.org/en/dynamic-routing-based-on-redis.html
worker_processes 2; error_log logs/error.log info; events { worker_connections 1024; } http { server { listen 8080; location / { resolver 8.8.4.4; # use Google's open DNS server set $target ''; access_by_lua ' local key = ngx.var.http_user_agent if not key then ngx.log(ngx.ERR, "no user-agent found") return ngx.exit(400) end local redis = require "resty.redis" local red = redis:new() red:set_timeout(1000) -- 1 second local ok, err = red:connect("127.0.0.1", 6379) if not ok then ngx.log(ngx.ERR, "Failed to connect to redis: ", err) return ngx.exit(500) end local host, err = red:get(key) if not host then ngx.log(ngx.ERR, "Failed to get redis key: ", err) return ngx.exit(500) end if host == ngx.null then ngx.log(ngx.ERR, "no host found for key ", key) return ngx.exit(400) end ngx.var.target = host '; proxy_pass http://$target; } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。