Search code examples
phpnginxfastcgi

Nginx send requests for missing images to backend


I'm working on a website that has copies of the same images in various sizes e.g.

/images/200x100/someimage.jpg

/images/400x200/someimage.jpg

etc.

The images are served directly by Nginx and only php requests get passed to the fastcgi.

If an image can't be found I would like to pass the request to fastcgi so that I can see if we can generate a version of the image in the correct size and then return that.

I just can't get it working - if the image is missing I can get it to call the script I want (instead of just returning a 404) BUT it is just returning the source of the php script.

This is the relevant part of my conf file:

location ~ (^|/)\. {
              return 404;
    }

    location /imgs {
          location ~ \.php$ {return 403;}
    }

    #Static Contents
    location ~* ^.+.(jpg|jpeg|png|gif|bmp)$ {
        try_files $uri  /$uri @backend;
        add_header Pragma "public";
        add_header Cache-Control "public";
        expires     1y;
        access_log  off;
        log_not_found off;
    }

    # Static Contents
    location ~* ^.+.(ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|eot|woff|svg|htc)$ {
        add_header Pragma "public";
        add_header Cache-Control "public";
        expires     1y;
        access_log  off;
        log_not_found off;
    }

    location / {
                # Check if a file exists, or route it to index.php.
                try_files $uri $uri/ /index.php?$query_string;
    }

    location ~\.php$ {
        try_files $uri =404;
                fastcgi_pass unix:/dev/shm/apache-php.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
    }

    location @backend {
        #return 410;
        rewrite  ^(.*)$ /image.php?url=$1;
        fastcgi_pass unix:/dev/shm/apache-php.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

A missing image will then pass into the location @backend but I can't pass the $uri to a script.

I've been through all kinds of variations what am I doing wrong? Any suggestions gratefully appreciated! Thanks.

Update

I've got this working in another VM perfectly (I copied the backend block and images blocks into the other conf file) - the only difference is that the one that works is using Apache instead of fastcgi.


Solution

  • The problem was a just a typo.

    I was trying to rewrite to /image.php it should have been /images.php

    The working version of the above conf file is as follows:

    location ~ (^|/)\. {
              return 404;
    }
    
    location /imgs {
          location ~ \.php$ {return 403;}
    }
    
    #Static Contents
    location ~* ^.+.(jpg|jpeg|png|gif|bmp)$ {
        try_files $uri  /$uri @backend;
        add_header Pragma "public";
        add_header Cache-Control "public";
        expires     1y;
        access_log  off;
        log_not_found off;
    }
    
    # Static Contents
    location ~* ^.+.(ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|eot|woff|svg|htc)$ {
        add_header Pragma "public";
        add_header Cache-Control "public";
        expires     1y;
        access_log  off;
        log_not_found off;
    }
    
    location / {
                # Check if a file exists, or route it to index.php.
                try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~\.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/dev/shm/apache-php.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
    
    location @backend {
    
        rewrite  ^(.*)$ /images.php?url=$1;
    }