2

We are trying to cache the request to images on our nginx servers. Even though the images are dynamic, the same image is requested frequently in a short time-span. Currently we are trying to solve this with Nginx caching the x-accel request.

Do we have to internal proxy to enable this? Is this even possible or do we need to use varnish? If it is possible, what are we doing wrong?

Our php code:

$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);

Our sites-enabled config file:

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;
}
}

our default config file:

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/*;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.