dsjojts9734 2014-09-19 15:34
浏览 36
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • doukang5966907 2014-09-22 08:41
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料