反向代理
將nginx作爲反向代理後,可以根據負載均衡算法分散到多臺上游服務器,這樣就實現了架構上的水平擴展,讓用戶無感知的情況下,添加更多的服務器,來提升性能
- 緩存的使用方法則是,在需要進行緩存url 路徑下,添加proxy_cache、proxy_cache_key、proxy_cache_valid。
- proxy_cache my_cache:指定緩存共享內存的命名
- proxy_cahce_key $host$uri$is_args$args:在共享內存中設置的 key 的值,這裏將 host,uri 等作爲 key 值
- Proxy_cache_valid 200 304 302 1d :指定的響應不返回緩存
配置文件
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream web_group {
server 127.0.0.1:8081 weight=2 max_fails=2 fail_timeout=2s;
}
proxy_cache_path /home/ubuntu/nginx/logs/cache levels=1:2 keys_zone=cache_one:20m max_size=1g;
server {
listen 127.0.0.1:80;
root html/;
index index.html;
}
server {
listen 80;
# 在響應報文中添加緩存首部字段
add_header X-Cache "$upstream_cache_status from $server_addr";
location / {
proxy_pass http://web_group;
proxy_cache cache_one;
proxy_cache_valid 200 1h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 5m;
}
}
server {
listen 8081;
location / {
root html/;
index index.html;
}
}
}