我们正在尝试将对图像的请求缓存在我们的 nginx 服务器上。即使图像是动态的,也会在短时间内频繁请求同一图像。目前,我们正在尝试使用 Nginx 缓存 x-accel 请求来解决这个问题。
我们必须使用内部代理才能启用此功能吗?这可能吗,还是我们需要使用清漆?如果可能,我们做错了什么?
我们的php代码:
$image = $images[$randomNumber];
header("Content-Type: application/octet-stream");
header('X-Accel-Expires: '. 'max');
header('Content-Disposition: attachment; filename='.$image.'');
header('X-Accel-Redirect: /protected_files/'. $image);
我们的站点启用配置文件:
proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:1000m inactive=60m;
proxy_cache_key "$request_uri";
server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.php index.html index.htm;
charset utf-8;
location / {
proxy_cache my_zone;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /protected_files {
gzip off;
internal;
expires max;
alias /xxx;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
我们的默认配置文件:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log off;
error_log on;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}