I have an application which knows how to prepare some special images and its running on a separate machine (i'm trying to use it as image server).

When these images are prepared and served by application, also saving psychically into a publicly accessible path like /image/files/medium/562.jpg to prevent running application again and again on further requests.

My application implements front controller pattern, so i need to sent request to my app as /thumbnail/process/?qs=1&qs=2 not /thumbnail.php or something like that.

Also i need to mask my real thumbnail processor url from end users.

At this point flow like below:

  1. End user makes an image request like /picture/small/518.jpg
  2. Nginx catches it and rewrites to /image/files/small/518.jpg because there is not a picture folder actually.
  3. If /image/files/small/518.jpg is exist, nginx should serve it directly instead of passing request to the app.
  4. If its not (image doesnt exists), nginx have to rewrite (or redirect) again and pass request to app by custom uri like /thumbnail/process/?size=small&model=866
  5. Also ngnix should't accept other requests if it's not an image request and should return a 444 immediately.

So, i'm tried to solve these problems by writing custom rules for nginx but it doesn't work. There is no problem with already processed images but its producing 404 not found responses instead of passing request to the application.

I can't find what is wrong with this configuration and what is the point which i'm missing.

server {
  server_name image.mydomain.com;
  root        /var/www/public_html;

  location / {
     # I know if is evil.
     # Don't pass request to the application if it's not a image request!
     if ( $uri !~ ^/(image|picture|thumbnail)/(.*)$ ) {
        return 444;

  # Real request uri for image.
  location ~ "^/picture/(small|medium|large)/([a-zA-Z0-9]).jpg$" {
     set $size $1;
     set $model $2;
     rewrite ^ /image/files/$size/$model.jpg last;

   location ~ "^/image/files/(small|medium|large)/([a-zA-Z0-9]).jpg$" {
       try_files $uri /thumbnail/process/?size=$1&model=$2;

   location ~ ^/thumbnail/process/(.*)$ {
        index index.php;
        try_files $uri $uri/ /index.php?$args;

   include fastcgi_config.conf;

And fascgi_config.conf contents:

fastcgi_intercept_errors on;

location ~ .php$ {
    fastcgi_pass  my-www-pool;
    fastcgi_index index.php; 

    fastcgi_split_path_info ^(.+.php)(/.+)$;

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    #fastcgi_param  REQUEST_URI       $uri?$args;

    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  HTTPS              $https if_not_empty;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    fastcgi_param PATH_INFO           $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED     $document_root$fastcgi_path_info;

fastcgi_param  REDIRECT_STATUS    200;

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.