Nginx向后端发送丢失图像的请求

时间:2014-09-19 15:34:21

标签: php nginx fastcgi

我正在一个网站上工作,该网站上有各种尺寸的相同图像的副本,例如

/images/200x100/someimage.jpg

/images/400x200/someimage.jpg

图像由Nginx直接提供,只有php请求传递给fastcgi。

如果找不到图像,我想将请求传递给fastcgi,以便我可以看到我们是否可以生成正确大小的图像版本然后返回。

我无法让它工作 - 如果图像丢失我可以让它调用我想要的脚本(而不是只返回404)但是它只是返回php脚本的源代码。 / p>

这是我的conf文件的相关部分:

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

然后,丢失的图像将传递到location @backend,但我无法将$ uri传递给脚本。

我经历过各种各样的变化,我做错了什么?任何建议感激不尽!感谢。

更新

我已经在另一个虚拟机中完美地工作了(我将后端块和图像块复制到另一个配置文件中) - 唯一的区别是工作的是使用Apache而不是fastcgi。

1 个答案:

答案 0 :(得分:3)

问题只是一个错字。

我试图重写为/image.php它应该是/images.php

上述conf文件的工作版本如下:

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