1

I have .php file which is loading images to hide their location. Every images are correctly caching through this directive:

location ^~ \.(jpg|jpeg|png|gif|ico|css|js)$ {
            expires max;
            valid_referers server_names blocked mysiteaddresishere;
            if ($invalid_referer) {
               return   403;
            }
}

by the way, valid referrers doesn't work, I don't know why.

I've added ^ before ~, someone told that this should look at the longest regex, maybe it do but not with php files.

I have in my vhost something like this:

location ~ \.php$ {
    try_files /a30bc49b5ff24bc50d55ba4306d73470.htm @php;
}

location @php {
    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9010;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
}

Dunno if this is blocking caching on my php image reader, I can't figure out how to add into this expires for images.

I've found this site: en.dklab.ru /lib/HTTP_ImageResizer/, so I've tried like this:

location /imagehi.php {
        fastcgi_cache MYAPP;
        fastcgi_cache_valid 200 304 404 240h;
        fastcgi_cache_key "method=$request_method|ims=$http_if_modified_since|inm=$http_if_none_match|host=$host|uri=$request_uri";
        fastcgi_hide_header "Set-Cookie";
        fastcgi_ignore_headers "Cache-Control" "Expires";

    # or use proxy_* commands if you use Apache, not FastCGI PHP
}

But it still doesn't work. Any ideas what am I missing ?

Nginx conf

fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {
    listen *:80;


    server_name mysite.com www.mysite.com;

    root   /var/www/mysite.com/web;



    index index.html index.htm index.php index.cgi index.pl index.xhtml;


    error_page 400 /error/400.html;
    error_page 401 /error/401.html;
    error_page 403 /error/403.html;
    error_page 404 /error/404.html;
    error_page 405 /error/405.html;
    error_page 500 /error/500.html;
    error_page 502 /error/502.html;
    error_page 503 /error/503.html;
    recursive_error_pages on;
    location = /error/400.html {

        internal;
    }
    location = /error/401.html {

        internal;
    }
    location = /error/403.html {

        internal;
    }
    location = /error/404.html {

        internal;
        }
    location = /error/405.html {

        internal;
    }
    location = /error/500.html {

        internal;
    }
    location = /error/502.html {

        internal;
    }
    location = /error/503.html {

        internal;
    }

    error_log /var/log/ispconfig/httpd/mysite.com/error.log;
    access_log /var/log/ispconfig/httpd/mysite.com/access.log combined;

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location /stats {

        index index.html index.php;
        auth_basic "Members Only";
        auth_basic_user_file /var/www/clients/client0/web1/web/stats/.htpasswd_stats;
    }

    location ^~ /awstats-icon {
        alias /usr/share/awstats/icon;
    }

    location ~ \.php$ {
        try_files /a30bc49b5ff24bc50d55ba4306d73470.htm @php;
    }

    location @php {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9010;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
    }

    location /imagehi\.php\?=.+\.(jpg|jpeg|png|gif) {
        fastcgi_cache MYAPP;
        fastcgi_cache_valid 200 60m;
        # or use proxy_* commands if you use Apache, not FastCGI PHP
    }

    location /imagehi.php\?=([A-Z]|[0-9]|[a-z]|&)+\.(jpg|jpeg|png|gif)$ {
                 expires max;
    }
    location ~ (imagehi\.php\?=.+\.(jpg|jpeg|png|gif))${ {
                 expires max;
    }
    location ^~ \.(jpg|jpeg|png|gif|ico|css|js)$ {
                 expires max;
    }
masegaloeh
  • 17,978
  • 9
  • 56
  • 104
Slupek
  • 11
  • 3
  • 1
    `location ^~ \.(jpg|jpeg|png|gif|ico|css|js)$ {` is perfectly useless. `^~` modificator [**forbids**](http://nginx.org/en/docs/http/ngx_http_core_module.html#location) regexp matching so this location could only matches file with weird name `\.(jpg|jpeg|png|gif|ico|css|js)$`, but even this is impossible because every location starts with `/`. – Alexey Ten Mar 08 '14 at 17:44

1 Answers1

0

Convert self answered question to CW

I solved it in php file image.php not in nginx. Solution was in headers i just followed this blog pages

// Return the requested graphic file to the browser
// or a 304 code to use the cached browser copy
function displayGraphicFile ($graphicFileName, $fileType='jpeg') {
    $fileModTime = filemtime($graphicFileName);
    // Getting headers sent by the client.
    $headers = getRequestHeaders();
    // Checking if the client is validating his cache and if it is current.
    if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $fileModTime)) {

        // Client's cache IS current, so we just respond '304 Not Modified'.
        header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fileModTime).' GMT', true, 304);
    } else {
        // Image not cached or cache outdated, we respond '200 OK' and output the image.
        header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fileModTime).' GMT', true, 200);
        header('Content-type: image/'.$fileType);
        header('Content-transfer-encoding: binary');
        header('Content-length: '.filesize($graphicFileName));
        readfile($graphicFileName);
    }
}
masegaloeh
  • 17,978
  • 9
  • 56
  • 104