![Nginx和Apache,如何配置proxy_cache?](https://linux22.com/image/617203/Nginx%E5%92%8CApache%EF%BC%8C%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AEproxy_cache%EF%BC%9F.png)
我安装了 Nginx 作为 Apache 的前端代理,其中 Apache 为 PHP 提供服务,而 Nginx 为静态文件提供服务。我在配置 Nginx 以将 PHP 的输出缓存到静态文件中时遇到了问题。我尝试使用 proxy_cache,但显然我做错了。
这是我的基本配置:
server {
listen 80;
root /var/www/web;
index index.php index.html index.htm;
server_name web.com;
location / {
try_files $uri $uri/ /index.php;
}
# cache static files
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
access_log off;
add_header Cache-Control public;
}
location ~ \.php$ {
proxy_pass http://127.0.0.1:8080;
# Set header to be what we requested
proxy_set_header Host $host;
# Set proxy_cache expiry time
proxy_cache_valid 200 302 5m;
proxy_cache_valid 404 1m;
proxy_cache_valid 301 1h;
# Need this for snooping with tcpdump (turns off upstream compression)
proxy_set_header Accept-Encoding "";
# Set real IP header (needed for client IP detection in apache)
proxy_set_header X-Real-IP $remote_addr;
# Explicitly allow critical headers
proxy_pass_header Set-Cookie;
# Prevent 304 responses being returned from Apache and cached by nginx
proxy_set_header If-None-Match "";
}
location ~ /\.ht {
deny all;
}
}
答案1
放proxy_cache_path在您的 http{} - 上下文中定义您的区域和参数
http {
...
proxy_cache_path /tmp/cache/blue blue:100m ...;
proxy_cache_path /tmp/cache/white white:100m ...;
...
}
放代理缓存在相应的服务器/位置上下文中设置 zone/off 以及您的选项
例如,全局定义缓存路径并针对每个服务器/位置激活/停用
location /blue {
proxy_cache blue;
proxy_cache_...
}
location /white {
proxy_cache white;
proxy_cache_...
}
location /red {
proxy_cache off;
proxy_cache_...
}
请注意,您的缓存文件将采用二进制格式